diff --git a/main.c b/main.c index 07ee024..0e54b4d 100644 --- a/main.c +++ b/main.c @@ -33,14 +33,16 @@ 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"; +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_04, + help_05 }; /* global variables */ @@ -68,6 +70,87 @@ void led_toggle(void) LED_PIN |= (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; @@ -145,69 +228,43 @@ void command_action() } else if (0==strcmp(word,"read")) { /* expecting pin */ word = strtok(NULL,delimiter); - if (!word) { + if (!word || strlen(word)!=2) { goto error; } - if (2==strlen(word) && word[0]>='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 + int8_t value = read_pin(word[0],word[1]); + if (value>=0) { + printf("%s: %d\n", word, value); } else { goto error; } } else if (0==strcmp(word,"write")) { /* expecting pin */ - word = strtok(NULL,delimiter); - if (!word) { + char * pin = strtok(NULL,delimiter); + if (!pin || strlen(pin)!=2) { 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<=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; }