diff --git a/main.c b/main.c index 0c3a470..07ee024 100644 --- a/main.c +++ b/main.c @@ -29,6 +29,27 @@ #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) { @@ -47,6 +68,27 @@ void led_toggle(void) LED_PIN |= (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; }