replace blinking example with pin RW console
This commit is contained in:
parent
5bba253d65
commit
85d311a752
176
main.c
176
main.c
|
@ -29,6 +29,27 @@
|
||||||
#include "uart.h" // basic UART functions
|
#include "uart.h" // basic UART functions
|
||||||
#include "main.h" // main definitions
|
#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 */
|
/* switch off LED */
|
||||||
void led_off(void)
|
void led_off(void)
|
||||||
{
|
{
|
||||||
|
@ -47,6 +68,27 @@ void led_toggle(void)
|
||||||
LED_PIN |= (1<<LED_IO);
|
LED_PIN |= (1<<LED_IO);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void help(void)
|
||||||
|
{
|
||||||
|
char* str;
|
||||||
|
for (uint8_t i=0; i<sizeof(help_table)/sizeof(PGM_P); i++) { /* display all help lines */
|
||||||
|
str = malloc(strlen_PF((uint_farptr_t)pgm_read_word(&(help_table[i]))));
|
||||||
|
strcpy_PF(str, (uint_farptr_t)pgm_read_word(&(help_table[i])));
|
||||||
|
printf(str);
|
||||||
|
free(str);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* save the UART input into a string */
|
||||||
|
ISR(USART_RX_vect) { /* UART receive interrupt */
|
||||||
|
uart_in[uart_in_i] = getchar(); // save input
|
||||||
|
if (uart_in_i<sizeof(uart_in)-1) { // go to next if there is space left (else overwrite last)
|
||||||
|
uart_in_i++; // prepare for next character
|
||||||
|
uart_in[uart_in_i] = 0; // always end the string
|
||||||
|
}
|
||||||
|
uart_flag = true; // notify new character is there
|
||||||
|
}
|
||||||
|
|
||||||
/* disable watchdog when booting */
|
/* disable watchdog when booting */
|
||||||
void wdt_init(void) __attribute__((naked)) __attribute__((section(".init3")));
|
void wdt_init(void) __attribute__((naked)) __attribute__((section(".init3")));
|
||||||
void wdt_init(void)
|
void wdt_init(void)
|
||||||
|
@ -73,14 +115,140 @@ void io_init(void)
|
||||||
sei(); /* enable interrupts */
|
sei(); /* enable interrupts */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* process command */
|
||||||
|
void command_action()
|
||||||
|
{
|
||||||
|
/* split command */
|
||||||
|
const char* delimiter = " ";
|
||||||
|
char* word = strtok(uart_in,delimiter);
|
||||||
|
if (!word) {
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
|
/* parse command */
|
||||||
|
if (0==strcmp(word,"help")) {
|
||||||
|
help();
|
||||||
|
} else if (0==strcmp(word,"led")) {
|
||||||
|
/* expecting state */
|
||||||
|
word = strtok(NULL,delimiter);
|
||||||
|
if (!word) {
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
|
if (0==strcmp(word,"on")) {
|
||||||
|
led_on();
|
||||||
|
} else if (0==strcmp(word,"off")) {
|
||||||
|
led_off();
|
||||||
|
} else if (0==strcmp(word,"toggle")) {
|
||||||
|
led_toggle();
|
||||||
|
} else {
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
|
} else if (0==strcmp(word,"read")) {
|
||||||
|
/* 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') {
|
||||||
|
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<<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;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
error:
|
||||||
|
printf(PSTR("command not recognized\n"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
io_init(); // initialize IOs
|
uint8_t uart_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 (true) { // endless loop for micro-controller
|
||||||
/* blinking LED example */
|
while (uart_flag) { // new UART input
|
||||||
led_toggle();
|
uart_flag = false;
|
||||||
_delay_ms(500);
|
while (uart_out_i<uart_in_i) {
|
||||||
|
putchar(uart_in[uart_out_i++]); // print current character
|
||||||
|
}
|
||||||
|
if ('\n'==uart_in[uart_in_i-1] || '\r'==uart_in[uart_in_i-1]) { // end of line detected
|
||||||
|
if ('\r'==uart_in[uart_in_i-1]) { // be sure to print a newline (for windows and mac EOL)
|
||||||
|
putchar('\n');
|
||||||
|
}
|
||||||
|
uart_in[uart_in_i-1] = 0; // remove end of line
|
||||||
|
if (uart_in_i>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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue