project:gsm:shield:sketch
Differences
This shows you the differences between two versions of the page.
| project:gsm:shield:sketch [2012/04/25 20:33] – created jenda | project:gsm:shield:sketch [2012/04/25 20:33] (current) – jenda | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | < | ||
| + | byte cmdlen = 0; | ||
| + | |||
| + | void setup() { | ||
| + | DDRC = B11111111; | ||
| + | DDRB = B11111111; | ||
| + | DDRD = B11111110; | ||
| + | Serial.begin(9600); | ||
| + | PORTC = B00000000; | ||
| + | PORTB = B00000000; | ||
| + | PORTD = B00000000; | ||
| + | } | ||
| + | |||
| + | void loop() { | ||
| + | ServeConsole(); | ||
| + | } | ||
| + | |||
| + | void ServeConsole() { | ||
| + | static char cmd[16]; | ||
| + | while(Serial.available()) { // Read chars from buffer. | ||
| + | byte c = Serial.read(); | ||
| + | if (cmdlen> | ||
| + | Serial.println(" | ||
| + | c = ' | ||
| + | } | ||
| + | Serial.print((char)c); | ||
| + | if (c == 10 || c == 13 || c==64) { | ||
| + | for (cmdlen=cmdlen; | ||
| + | cmd[cmdlen] = 0; // FIXME: better way is correct command terminator handling | ||
| + | } | ||
| + | Serial.println(" | ||
| + | ExecCommand(cmd); | ||
| + | cmd[cmdlen = 0] = 0; | ||
| + | return; | ||
| + | } | ||
| + | if (c == ' | ||
| + | cmd[cmdlen = 0] = 0; | ||
| + | Serial.println(" | ||
| + | return; | ||
| + | } | ||
| + | cmd[cmdlen++] = c; | ||
| + | cmd[cmdlen] = 0; | ||
| + | } | ||
| + | return; | ||
| + | } | ||
| + | |||
| + | void ExecCommand(char *cmd) { | ||
| + | byte portnum; | ||
| + | boolean status; | ||
| + | switch (cmd[0]) { | ||
| + | case ' | ||
| + | /* set bit | ||
| + | 1 → portid (a-) | ||
| + | 2 → state (1/0) | ||
| + | FIXME: syntax checking | ||
| + | */ | ||
| + | portnum = cmd[1]-97; | ||
| + | status = false; | ||
| + | if (cmd[2] == ' | ||
| + | status = true; | ||
| + | } | ||
| + | if (portnum <= 7) { | ||
| + | PORTD = SetBit(portnum, | ||
| + | } else if (portnum <= 15) { | ||
| + | PORTB = SetBit(portnum-8, | ||
| + | } else if (portnum <= 23) { | ||
| + | PORTC = SetBit(portnum-16, | ||
| + | } | ||
| + | break; | ||
| + | default: | ||
| + | Serial.println(" | ||
| + | break; | ||
| + | } | ||
| + | return; | ||
| + | } | ||
| + | |||
| + | byte SetBit (byte pos, byte packet, boolean state) { | ||
| + | if(state) { | ||
| + | bitWrite(packet, | ||
| + | } | ||
| + | else { | ||
| + | bitWrite(packet, | ||
| + | } | ||
| + | return packet; | ||
| + | } | ||
| + | </ | ||