factorize pin operation and add short command

This commit is contained in:
King Kévin 2015-08-03 09:14:53 +02:00
parent 85d311a752
commit a06dc20d04
1 changed files with 114 additions and 57 deletions

171
main.c
View File

@ -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<<LED_IO);
}
/* write value on pin from port
* all parameters are ASCII encoded
* return -1 if failed, else value
*/
int8_t write_pin(char port, char pin, char value) {
if (((port>='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<<ppin); // set as output
/* write value */
if ('0'==value) {
*pport &= ~(1<<ppin);
return 0;
} else if ('1'==value) {
*pport |= (1<<ppin);
return 1;
} else {
return -1;
}
} else {
return -1;
}
}
/* read value from pin from port
* all parameters are ASCII encoded
* return -1 if failed, else value
*/
int8_t read_pin(char port, char pin) {
if (((port>='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); // set as input
return ((*pport>>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<<pin); // set as output
/* expecting value */
word = strtok(NULL,delimiter);
if (!word || strlen(word)!=1) {
goto error;
}
/* write value */
if ('0'==word[0]) {
*port &= ~(1<<pin);
} else if ('1'==word[0]) {
*port |= (1<<pin);
} else {
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;
}