src/hello-dmx.c
changeset 79 076657910c0a
parent 77 975a2dffdcda
child 98 e743c905cbf5
equal deleted inserted replaced
78:504c2310cddb 79:076657910c0a
    45 static struct dmx_state {
    45 static struct dmx_state {
    46     byte out[DMX_COUNT];
    46     byte out[DMX_COUNT];
    47     byte count;
    47     byte count;
    48 } dmx;
    48 } dmx;
    49 
    49 
       
    50 /*
       
    51  * Tick output state
       
    52  */
       
    53 void update ()
       
    54 {
       
    55     led_toggle();
       
    56     dmx_packet(dmx.count, dmx.out);
       
    57 }
       
    58 
       
    59 /*
       
    60  * Console input state.
       
    61  */
    50 enum state {
    62 enum state {
    51     START       = '\n',
    63     START       = '\n',
    52     CMD         = ';',
    64     CMD         = ';',
    53     ARG         = ',',
    65     ARG         = ',',
    54     ERROR       = '!',
    66     ERROR       = '!',
    55 };
    67 };
    56 
    68 
    57 #define CONSOLE_ARGS 8
    69 #define CONSOLE_ARGS 8
    58 
    70 
    59 /*
       
    60  * Console input state.
       
    61  */
       
    62 static struct console {
    71 static struct console {
    63     byte state;
    72     byte state;
    64     byte cmd;
    73     byte cmd;
    65     byte argc;
    74     byte argc;
    66     byte argv[CONSOLE_ARGS];
    75     byte argv[CONSOLE_ARGS];
   264 
   273 
   265         // command
   274         // command
   266         if ((ret = command(console.cmd))) {
   275         if ((ret = command(console.cmd))) {
   267 
   276 
   268         } else {
   277         } else {
       
   278             // success
   269             ret = '\n';
   279             ret = '\n';
       
   280 
       
   281             // commit changes
       
   282             update();
   270         }
   283         }
   271 
   284 
   272         // return to START with response
   285         // return to START with response
   273         console.state = START;
   286         console.state = START;
   274         return '\n'; // XXX: required to delimit command reponses..
   287         return '\n'; // XXX: required to delimit command reponses..
   315     // reject
   328     // reject
   316     console.state = ERROR;
   329     console.state = ERROR;
   317     return ERROR;
   330     return ERROR;
   318 }
   331 }
   319 
   332 
   320 /*
       
   321  * Tick output state
       
   322  */
       
   323 void update ()
       
   324 {
       
   325     dmx_packet(dmx.count, dmx.out);
       
   326 }
       
   327 
       
   328 void main ()
   333 void main ()
   329 {
   334 {
   330     led_init();
   335     led_init();
   331     timer_init();
   336     timer_init();
   332     serial_init();
   337     serial_init();
   333     dmx_init();
   338     dmx_init();
   334 
   339 
   335     // mainloop
   340     // mainloop
   336     char c = '>';
   341     char c = '>';
   337     unsigned timeout = 8000; // 2Hz
   342     unsigned interval = 8000; // 2Hz
   338 
   343 
   339     // start
   344     // start
   340     sei();
   345     sei();
   341     serial_write(c);
   346     serial_write(c);
       
   347     timer1_start(interval);
   342 
   348 
   343     while (true) {
   349     while (true) {
   344         // sleep
   350         // interval sleep
   345         //led_on();
   351         if (timer_sleep(0)) {
   346         if (timer_sleep(timeout)) {
   352             // output
   347             led_toggle();
   353             update();
   348         }
   354         }
   349 
   355 
   350         // input
   356         // input
   351         while ((c = serial_read())) {
   357         while ((c = serial_read())) {
   352             serial_write(input(c));
   358             serial_write(input(c));
   353         }
   359         }
   354 
   360     }
   355         //led_off();
   361 }
   356 
       
   357         // output
       
   358         update();
       
   359     }
       
   360 }