/* 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 "usart.h" // basic USART 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-dB-D][0-7] read value from pin\n"; const char help_04[] PROGMEM = "\twrite [b-dB-D][0-7] 0|1 write value on pin\n"; const char help_05[] PROGMEM = "\t[b-dB-D][0-7][0|1] read/write value from/on pin\n"; PGM_P const help_table[] PROGMEM = { help_00, help_01, help_02, help_03, help_04, help_05 }; /* global variables */ char usart_in[32] = {0}; // user input from USART volatile uint8_t usart_in_i = 0; // how many USART characters have been input /* flags, set in the interrupts and handled in the main program */ volatile bool usart_flag = false; // USART input data is available /* switch off LED */ void led_off(void) { LED_PORT &= ~(1<='b' && port<='d') || (port>='B' && port<='D')) && pin>='0' && pin<='7' && value>='0' && value<='1') { uint8_t ppin = pin-'0'; volatile uint8_t * pport = NULL; volatile uint8_t * ddr = NULL; switch (port) { case 'b': case 'B': pport = &PORTB; ddr = &DDRB; break; case 'c': case 'C': pport = &PORTC; ddr = &DDRC; break; case 'd': case 'D': pport = &PORTD; ddr = &DDRD; break; default: return -1; break; } *ddr |= (1<='b' && port<='d') || (port>='B' && port<='D')) && pin>='0' && pin<='7') { uint8_t ppin = pin-'0'; volatile uint8_t * pport = NULL; volatile uint8_t * ddr = NULL; switch (port) { case 'b': case 'B': pport = &PINB; ddr = &DDRB; break; case 'c': case 'C': pport = &PINC; ddr = &DDRC; break; case 'd': case 'D': pport = &PIND; ddr = &DDRD; break; default: return -1; break; } *ddr &= ~(1<>ppin)&0x1); } else { return -1; } } void help(void) { char* str; for (uint8_t i=0; i=0) { printf("%s: %d\n", word, value); } else { goto error; } } else if (0==strcmp(word,"write")) { /* expecting pin */ char * pin = strtok(NULL,delimiter); if (!pin || strlen(pin)!=2) { goto error; } /* expecting value */ char* value = strtok(NULL,delimiter); if (!value || strlen(value)!=1) { goto error; } int8_t rc = write_pin(pin[0],pin[1],value[0]); if (rc>=0) { printf("%s: %s\n", pin, value); } else { goto error; } } else if (2==strlen(word)) { // read value int8_t value = read_pin(word[0],word[1]); if (value>=0) { printf("%s: %d\n", word, value); } else { goto error; } } else if (3==strlen(word)) { // write value int8_t value = write_pin(word[0],word[1],word[2]); if (value>=0) { printf("%c%c: %d\n", word[0],word[1],value); } else { goto error; } } else { goto error; } return; error: printf(PSTR("command not recognized\n")); return; } int main(void) { uint8_t usart_out_i = 0; // how many UART input characters have been processed io_init(); // initialize IOs printf(PSTR("welcome to the demo code\n")); while (true) { // endless loop for micro-controller while (usart_flag) { // new UART input usart_flag = false; while (usart_out_i1) { // ignore empty lines command_action(); // process command } usart_out_i = 0; // reset output usart_in_i = 0; // reset input } } /* go to sleep and wait for next interrupt */ set_sleep_mode(SLEEP_MODE_IDLE); sleep_mode(); } return 0; }