src/hello-dmx.c
changeset 66 0cf14786b909
parent 65 625f34328820
child 67 53743ecc9150
equal deleted inserted replaced
65:625f34328820 66:0cf14786b909
    35 {
    35 {
    36     xbi(&DEBUG_PORT, DEBUG_LED);
    36     xbi(&DEBUG_PORT, DEBUG_LED);
    37 }
    37 }
    38 
    38 
    39 // dmx
    39 // dmx
       
    40 #define DMX_COUNT 255
       
    41 
    40 /*
    42 /*
    41  * DMX state
    43  * DMX state
    42  */
    44  */
    43 static struct dmx_state {
    45 static struct dmx_state {
    44     byte out[256];
    46     byte out[DMX_COUNT];
    45     byte count;
    47     byte count;
    46 } dmx;
    48 } dmx;
    47 
       
    48 
    49 
    49 enum state {
    50 enum state {
    50     START       = '\n',
    51     START       = '\n',
    51     CMD         = ';',
    52     CMD         = ';',
    52     ARG         = ',',
    53     ARG         = ',',
    53     ERROR       = '!',
    54     ERROR       = '!',
    54 };
    55 };
    55 
    56 
    56 enum cmd {
       
    57     CMD_
       
    58 };
       
    59 
       
    60 #define CONSOLE_ARGS 8
    57 #define CONSOLE_ARGS 8
    61 
    58 
    62 /*
    59 /*
    63  * Console input state.
    60  * Console input state.
    64  */
    61  */
    65 static struct console {
    62 static struct console {
    66     enum state state;
    63     byte state;
    67 
    64     byte cmd;
    68     enum cmd cmd;
    65     byte argc;
    69     char argc;
    66     byte argv[CONSOLE_ARGS];
    70     char argv[CONSOLE_ARGS];
       
    71 } console;
    67 } console;
    72 
    68 
    73 /*
    69 /*
       
    70  * Clear output (no state).
       
    71  */
       
    72 int cmd_clear ()
       
    73 {
       
    74     dmx.count = 0;
       
    75 
       
    76     return 0;
       
    77 }
       
    78 
       
    79 /*
       
    80  * Set output to given sequence.
       
    81  */
       
    82 int cmd_out ()
       
    83 {
       
    84     byte i;
       
    85 
       
    86     for (i = 0; i < console.argc; i++) {
       
    87         dmx.out[i] = console.argv[i];
       
    88     }
       
    89 
       
    90     dmx.count = i;
       
    91 
       
    92     return 0;
       
    93 }
       
    94 
       
    95 /*
       
    96  * Set output at offset.
       
    97  */
       
    98 int cmd_set ()
       
    99 {
       
   100     if (console.argc < 1) {
       
   101         return '!';
       
   102     }
       
   103 
       
   104     byte i = console.argv[0];
       
   105 
       
   106     for (byte a = 1; a < console.argc; a++) {
       
   107         dmx.out[i++] = console.argv[a];
       
   108     }
       
   109 
       
   110     if (i > dmx.count)
       
   111         dmx.count = i;
       
   112 
       
   113     return 0;
       
   114 }
       
   115 
       
   116 /*
       
   117  * Set output to max. zeroes
       
   118  */
       
   119 int cmd_zero ()
       
   120 {
       
   121     byte count;
       
   122 
       
   123     if (console.argc == 0) {
       
   124         count = DMX_COUNT;
       
   125     } else if (console.argc == 1) {
       
   126         count = console.argv[0];
       
   127     } else {
       
   128         return '!';
       
   129     }
       
   130 
       
   131     // set
       
   132     byte i;
       
   133     for (i = 0; i < count; i++) {
       
   134         dmx.out[i] = 0;
       
   135     }
       
   136     dmx.count = i;
       
   137 
       
   138     return 0;
       
   139 }
       
   140 
       
   141 /*
    74  * Process console command.
   142  * Process console command.
    75  */
   143  */
    76 int command ()
   144 int command ()
    77 {
   145 {
    78     switch (console.cmd) {
   146     switch (console.cmd) {
    79         default:
   147         case 'c':       return cmd_clear();
    80             return '?';
   148         case 'o':       return cmd_out();
       
   149         case 's':       return cmd_set();
       
   150         case 'z':       return cmd_zero();
       
   151 
       
   152         default:        return '?';
    81     }
   153     }
    82 }
   154 }
    83 
   155 
    84 /*
   156 /*
    85  * Process console input.
   157  * Process console input.
   106             ret = '\n';
   178             ret = '\n';
   107         }
   179         }
   108         
   180         
   109         // return to START with response
   181         // return to START with response
   110         console.state = START;
   182         console.state = START;
   111         return ret;
   183         return '\n'; // XXX: required to delimit command reponses..
   112     
   184     
   113     } else if (c == ' ' || c == '\t') {
   185     } else if (c == ' ' || c == '\t' || c == ',') {
   114         // argument
   186         // argument
   115         if (console.state == CMD) {
   187         if (console.state == CMD) {
   116             console.state = ARG;
   188             console.state = ARG;
   117             console.argc = 0;
   189             console.argc = 0;
   118 
   190 
   119             return c;
   191             return ',';
   120 
   192 
   121         } else if (console.state == ARG) {
   193         } else if (console.state == ARG) {
   122             if (console.argc++ < CONSOLE_ARGS) {
   194             if (console.argc++ < CONSOLE_ARGS) {
   123                 console.argv[console.argc] = 0;
   195                 console.argv[console.argc] = 0;
   124 
   196 
   125                 return c;
   197                 return ',';
   126             }
   198             }
   127         }
   199         }
   128 
   200 
   129     // printable    
   201     // printable    
   130     } else if (32 < c && c < 128) {
   202     } else if (32 < c && c < 128) {
   156 /*
   228 /*
   157  * Tick output state
   229  * Tick output state
   158  */
   230  */
   159 void update ()
   231 void update ()
   160 {
   232 {
   161     dmx_break();
   233     dmx_packet(dmx.count, dmx.out);
   162     dmx_frame(0);
       
   163 
       
   164     for (byte i = 0; i < dmx.count; i++) {
       
   165         dmx_frame(dmx.out[i]);
       
   166     }
       
   167 }
   234 }
   168 
   235 
   169 void main ()
   236 void main ()
   170 {
   237 {
   171     led_init();
   238     led_init();
   172     timer_init();
   239     timer_init();
   173     serial_init();
   240     serial_init();
   174     //dmx_init();
   241     dmx_init();
   175 
   242 
   176     // mainloop
   243     // mainloop
   177     char c = '>';
   244     char c = '>';
   178     unsigned timeout = 8000; // 2Hz
   245     unsigned timeout = 8000; // 2Hz
   179     
   246     
       
   247     // start
   180     sei();
   248     sei();
       
   249     serial_write(c);
   181 
   250 
   182     while (true) {
   251     while (true) {
   183         // sleep
   252         // sleep
   184         //led_on();
   253         //led_on();
   185         if (timer_sleep(timeout)) {
   254         if (timer_sleep(timeout)) {
   186             c = '.';
   255             //c = ' ';
       
   256             led_toggle();
       
   257             c = 0;
   187 
   258 
   188         } else if ((c = serial_read())) {
   259         } else if ((c = serial_read())) {
   189             // got serial data
   260             // got serial data
   190             c = input(c);
   261             c = input(c);
   191 
   262 
   193             // unknown interrupt
   264             // unknown interrupt
   194             c = '?';
   265             c = '?';
   195         }
   266         }
   196         //led_off();
   267         //led_off();
   197         
   268         
   198         // respond
   269         if (c)
   199         serial_write(c);
   270             // respond
       
   271             serial_write(c);
   200         
   272         
   201         // output
   273         // output
   202         update();
   274         update();
   203         
   275     }
   204         led_toggle();
   276 }
   205     }
       
   206 }