command interface for remote control
[digitaldcpower] / main.c
diff --git a/main.c b/main.c
index 1f05d91..39f3f5c 100644 (file)
--- a/main.c
+++ b/main.c
@@ -24,7 +24,7 @@
 #include "hardware_settings.h"
 
 // change this when you compile:
-#define SWVERSION "ver: ddcp-0.6.1"
+#define SWVERSION "ver: ddcp-0.6.2"
 //#define DEBUGDISP 1
 
 //debug LED:
@@ -43,16 +43,23 @@ static int16_t set_val[2];
 // the set values but converted to ADC steps
 static int16_t set_val_adcUnits[2]; 
 static uint8_t bpress=0;
+// comment this out to use a debug LED on PD0 (RXD):
+#define USE_UART 1
+//
+#ifdef USE_UART
 #define UARTSTRLEN 8
 static char uartstr[UARTSTRLEN+1];
 static uint8_t uartstrpos=0;
 static uint8_t uart_has_one_line=0;
+#endif
 
 void delay_ms_uartcheck(uint8_t ms)
 // delay for a minimum of <ms> 
 {
+       int ist_start_of_line=1;
         while(ms){
                 _delay_ms(0.85);
+#ifdef USE_UART
                if(uart_has_one_line==0 && uart_getchar_noblock(&uartstr[uartstrpos])){
                        uart_sendchar(uartstr[uartstrpos]); // echo back
                        if (uartstr[uartstrpos]=='\n'||uartstr[uartstrpos]=='\r'){
@@ -60,7 +67,16 @@ void delay_ms_uartcheck(uint8_t ms)
                                uart_has_one_line=1;
                                uart_sendchar('\n'); // the echo back puts a \r
                        }
-                       uartstrpos++;
+                       // ignore leading white space on the line:
+                       if (!(uartstr[uartstrpos]==' ' || uartstr[uartstrpos]=='\t')){
+                               ist_start_of_line=0;
+                               uartstrpos++;
+                       }else{
+                               // white space
+                               if (ist_start_of_line==0){
+                                       uartstrpos++;
+                               }
+                       }
                        if (uartstrpos>UARTSTRLEN){
                                uart_sendstr_P("\r\nERROR\r\n");
                                uartstrpos=0; // empty buffer
@@ -68,6 +84,7 @@ void delay_ms_uartcheck(uint8_t ms)
                                uart_has_one_line=1; 
                        }
                }
+#endif
                 ms--;
         }
 }
@@ -175,9 +192,62 @@ static void store_permanent(void){
 
 // check the keyboard
 static uint8_t check_buttons(void){
+       uint8_t uartprint_ok=0;
+       uint8_t cmdok=0;
+#ifdef USE_UART
        char buf[21];
+#endif
+       //
+#ifdef USE_UART
        if (uart_has_one_line){
-       //      if (strncmp("?",uartstr,1)==0){
+                        if (uartstr[0]=='i' && uartstr[1]=='=' && uartstr[2]!='\0'){
+                                set_val[0]=atoi(&uartstr[2]);
+                                if(set_val[0]>I_MAX){
+                                        set_val[0]=I_MAX;
+                                }
+                                if(set_val[0]<0){
+                                        set_val[0]=0;
+                                }
+                                uartprint_ok=1;
+                        }
+                       // version
+                       if (uartstr[0]=='v' && uartstr[1]=='e'){
+                               uart_sendstr_p(P("  "));
+                               uart_sendstr_p(P(SWVERSION));
+                               uart_sendstr_p(P("\r\n"));
+                               cmdok=1;
+                       }
+                       // store
+                       if (uartstr[0]=='s' && uartstr[1]=='t'){
+                               store_permanent();
+                                uartprint_ok=1;
+                       }
+                        if (uartstr[0]=='u' && uartstr[1]=='=' && uartstr[2]!='\0'){
+                                set_val[1]=atoi(&uartstr[2]);
+                                if(set_val[1]>U_MAX){
+                                        set_val[1]=U_MAX;
+                                }
+                                if(set_val[1]<0){
+                                        set_val[1]=0;
+                                }
+                                uartprint_ok=1;
+                        }
+                       // help
+                       if (uartstr[0]=='h' || uartstr[0]=='H'){
+                               uart_sendstr_p(P("  Usage: u=V*10|i=mA/10|store|help|version\r\n"));
+                               uart_sendstr_p(P("  Examples:\r\n"));
+                               uart_sendstr_p(P("  set 6V: u=60\r\n"));
+                               uart_sendstr_p(P("  max 200mA: i=20\r\n"));
+                               cmdok=1;
+                       }
+                       if (uartprint_ok){
+                               cmdok=1;
+                               uart_sendstr_p(P("  ok\r\n"));
+                       }
+                       if (uartstr[0]!='\0' && cmdok==0){
+                               uart_sendstr_p(P("  command unknown\r\n"));
+                       }
+
                        int_to_dispstr(measured_val[1],buf,1);
                        uart_sendstr(buf);
                        uart_sendchar('V');
@@ -187,7 +257,6 @@ static uint8_t check_buttons(void){
                        uart_sendstr(buf);
                        uart_sendchar(']');
                        uart_sendchar(',');
-                       uart_sendchar(' ');
                        int_to_dispstr(measured_val[0],buf,2);
                        uart_sendstr(buf);
                        uart_sendchar('A');
@@ -196,17 +265,16 @@ static uint8_t check_buttons(void){
                        int_to_dispstr(set_val[0],buf,2);
                        uart_sendstr(buf);
                        uart_sendchar(']');
-                       uart_sendchar(' ');
                        if (is_current_limit()){
                                uart_sendchar('I');
                        }else{
                                uart_sendchar('U');
                        }
                        uart_sendchar('>');
-       //      }
                uart_has_one_line=0;
                uartstrpos=0;
        }
+#endif
        if (check_u_button(&(set_val[1]))){
                if(set_val[1]>U_MAX){
                        set_val[1]=U_MAX;
@@ -231,9 +299,12 @@ int main(void)
        char out_buf[21];
        uint8_t i=0;
        uint8_t ilimit=0;
+
+#ifndef USE_UART
        // debug led, you can not have an LED if you use the uart
-       //DDRD|= (1<<DDD0); // LED, enable PD0, LED as output
-       //LEDOFF;
+       DDRD|= (1<<DDD0); // LED, enable PD0, LED as output
+       LEDOFF;
+#endif
 
        init_dac();
        lcd_init();
@@ -244,7 +315,9 @@ int main(void)
                set_val[1]=eeprom_read_word((uint16_t *)0x04);
                set_val[0]=eeprom_read_word((uint16_t *)0x02);
        }
+#ifdef USE_UART
        uart_init();
+#endif
        sei();
        init_analog();
        while (1) {