This repository has been archived by the owner on Jan 2, 2022. It is now read-only.
forked from nakkaya/clodiuno
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
112 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,92 @@ | ||
#include "WProgram.h" | ||
#include <string.h> | ||
#include <WProgram.h> | ||
|
||
int parsePin(char* msg){ | ||
char pin[] = {msg[2],msg[3]}; | ||
return atoi(pin); | ||
} | ||
|
||
int cmdCmpr(char* msg, char* expected){ | ||
if (msg[0] == expected[0] && msg[1] == expected[1]) | ||
return true; | ||
else | ||
return false; | ||
} | ||
|
||
char* setPinMode(char* msg){ | ||
char pin[] = {msg[2],msg[3]}; | ||
int pin = parsePin(msg); | ||
char mode = msg[4]; | ||
|
||
if (mode == 'o'){ | ||
pinMode(atoi(pin), OUTPUT); | ||
digitalWrite(pin, LOW); // disable PWM | ||
pinMode(pin, OUTPUT); | ||
return "OK\n"; | ||
}else if (mode == 'i'){ | ||
pinMode(atoi(pin), INPUT); | ||
pinMode(pin, INPUT); | ||
return "OK\n"; | ||
}else if (mode == 'a'){ | ||
digitalWrite(pin + 14, LOW); // disable internal pull-ups | ||
pinMode(pin + 14, INPUT); | ||
return "OK\n"; | ||
}else if (mode == 'p'){ | ||
pinMode(pin, OUTPUT); | ||
return "OK\n"; | ||
}else | ||
return "Error.\n"; | ||
return "Error\n"; | ||
} | ||
|
||
char* setDigitalWrite(char* msg){ | ||
char pin[] = {msg[2],msg[3]}; | ||
int pin = parsePin(msg); | ||
char mode = msg[4]; | ||
|
||
if (mode == 'h'){ | ||
digitalWrite(atoi(pin), HIGH); | ||
digitalWrite(pin, HIGH); | ||
return "OK\n"; | ||
}else if (mode == 'l'){ | ||
digitalWrite(atoi(pin), LOW); | ||
digitalWrite(pin, LOW); | ||
return "OK\n"; | ||
}else | ||
return "Error.\n"; | ||
return "Error\n"; | ||
} | ||
|
||
char* setAnalogWrite(char* msg){ | ||
int pin = parsePin(msg); | ||
char val[] = {msg[4], msg[5], msg[6]}; | ||
|
||
analogWrite(pin, atoi(val)); | ||
return "OK\n"; | ||
} | ||
|
||
char* getDigitalRead(char* msg){ | ||
int pin = parsePin(msg); | ||
|
||
char buff [20]; | ||
itoa(digitalRead(pin),buff,10); | ||
|
||
strncat(buff,"\n",1); | ||
return buff; | ||
} | ||
|
||
int getAnalogRead(char* msg){ | ||
int pin = parsePin(msg); | ||
|
||
char buff [20]; | ||
itoa(analogRead(pin),buff,10); | ||
|
||
strncat(buff,"\n",1); | ||
return buff; | ||
} | ||
|
||
char* process(char* msg){ | ||
if (msg[0] == 'p' && msg[1] == 'm') | ||
if (cmdCmpr(msg,"pm")) | ||
return setPinMode(msg); | ||
else if (msg[0] == 'd' && msg[1] == 'w') | ||
else if (cmdCmpr(msg,"dw")) | ||
return setDigitalWrite(msg); | ||
else if (cmdCmpr(msg,"aw")) | ||
return setAnalogWrite(msg); | ||
else if (cmdCmpr(msg,"dr")) | ||
return getDigitalRead(msg); | ||
else if (cmdCmpr(msg,"ar")) | ||
return getAnalogRead(msg); | ||
else | ||
return "Unknown Command.\n"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters