/* This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * */ #include // Standard Integer Types #include // Standard IO facilities #include // General utilities #include // Boolean #include // Strings #include // AVR device-specific IO definitions #include // Convenience functions for busy-wait delay loops #include // Interrupts #include // Watchdog timer handling #include // Program Space Utilities #include // Power Management and Sleep Modes #include "uart.h" // basic UART functions #include "main.h" // main definitions /* help strings */ const char help_00[] PROGMEM = "commands:\n"; const char help_01[] PROGMEM = "\thelp display this help\n"; const char help_02[] PROGMEM = "\tled on|off|toggle change LED state\n"; const char help_03[] PROGMEM = "\tread [B-D][0-7] read value from pin\n"; const char help_04[] PROGMEM = "\twrite [B-D][0-7] 0|1 write value on pin\n"; PGM_P const help_table[] PROGMEM = { help_00, help_01, help_02, help_03, help_04 }; /* global variables */ char uart_in[32] = {0}; // user input from USART volatile uint8_t uart_in_i = 0; // how many UART characters have been input /* flags, set in the interrupts and handled in the main program */ volatile bool uart_flag = false; // UART input data is available /* switch off LED */ void led_off(void) { LED_PORT &= ~(1<='B' && word[0]<='D' && word[1]>='0' && word[1]<='7') { volatile uint8_t * pin = NULL; volatile uint8_t * ddr = NULL; switch (word[0]) { case 'B': pin = &PINB; ddr = &DDRB; break; case 'C': pin = &PINC; ddr = &DDRC; break; case 'D': pin = &PIND; ddr = &DDRD; break; } *ddr &= ~(1<<(word[1]-'0')); // set as input printf("%s: %u\n", word, (*pin>>(word[1]-'0'))&0x1); // print value } else { goto error; } } else if (0==strcmp(word,"write")) { /* expecting pin */ word = strtok(NULL,delimiter); if (!word) { goto error; } if (2==strlen(word) && word[0]>='B' && word[0]<='D' && word[1]>='0' && word[1]<='7') { uint8_t pin = word[1]-'0'; volatile uint8_t * port = NULL; volatile uint8_t * ddr = NULL; switch (word[0]) { case 'B': port = &PORTB; ddr = &DDRB; break; case 'C': port = &PORTC; ddr = &DDRC; break; case 'D': port = &PORTD; ddr = &DDRD; break; } *ddr |= (1<1) { // ignore empty lines command_action(); // process command } uart_out_i = 0; // reset output uart_in_i = 0; // reset input } } /* go to sleep and wait for next interrupt */ set_sleep_mode(SLEEP_MODE_IDLE); sleep_mode(); } return 0; }