Few day ago, i'm try out arduino UNO robotic board. found it not that easy to use (C <--> Python). I wrote a arduino UNO prototype to serial API, which can interface to any programming Language. as long as the programming Language can interface to virtual serial port and using serial API, this recipe can be use... Requirement:
Upload this recipes to arduino uno with my prototype code (name "csc.pde" as below)
Base concept:
- wait from serial reply "?" and ready for read
- pass function name as string to arduino uno
- pass all argument as string to arduino uno
- read result as string from arduino uno
Can easy extend it to support:
- python API(open source and i like most) in part II
- other arduino board
- Bluetooth with serial interface
- any PC can control the arduino board easily via your prefer Language.
- interrupt
Future add-on:
- Json string passing to arduino
- Json Reply from arduino
- thread base design
- Interrupt direct call
Please study it and extend it and share among open source members especially in python... :)
Next Part II
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 | //Author: Cheeng Shu Chin
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
typedef String (*cmd_func_ptr)();
typedef String (*funcp)(String);
typedef struct
{
String cmd;
cmd_func_ptr func;
} command;
String cmd,cmdlck;
volatile int int0=0;
volatile int int1=0;
int dbg=1;
cmd_func_ptr func;
command commands[] =
{
{"echo", &echo},
{"unknown", &unknown},
{"pinmode", &pinmode},
{"digitalwrite", &digitalwrite},
{"digitalread", &digitalread},
{"analogread", &analogread},
{"analoreference", &analogreference},
{"analogwrite", &analogwrite},
{"tones", &tones},
{"notones", ¬ones},
{"shiftout", &shiftout},
{"shiftin", &shiftin},
{"pulsein", &pulsein},
{"attachinterrupt", &attachinterrupt},
{"detachinterrupt", &detachinterrupt},
{"cinterrupts", &cinterrupts},
{"nocinterrupts", &nocinterrupts},
{"debug", &debug},
{"pycall", &pycall},
{"test", &test},
{"nop", &nop},
};
String rdata(String cstr)
{
readagain:
if (dbg>0)
{
reply(cstr);
}else{
reply("?");
}
String cst = "";
while(Serial.available()>0)
{
cst += byte(Serial.read());
}
if (cst.equalsIgnoreCase("!"))
{
sprintln("cmd = "+cmdlck);
goto readagain;
}
return cst;
}
void reply(String cst)
{
loopagain:
if (int0 != 0)
{
sprintln("!INT0 ?");
int0 = 0;
goto loopagain;
}
if (int1 != 0)
{
sprintln("!INT1 ?");
int1 = 0;
goto loopagain;
}
sprintln(cst);
while(Serial.available()<1)
{
}
}
String nop()
{
return "";
}
String test()
{
int intp=str2int(rdata("interrupts ?"));
if (intp == 0)
{
int0 = !int0;
}
if (intp == 1)
{
int1 = !int1;
}
return "";
}
String debug()
{
dbg=str2int(rdata("debug ?"));
return "";
}
String unknown()
{
return "unkown command: " + cmd;
}
void sprint(String cst)
{
Serial.print(cst);
}
void sprintln(String cst)
{
Serial.println(cst);
}
int str2int(String dt)
{
char str[dt.length()+1];
dt.toCharArray(str,dt.length()+1);
return atoi(str);
}
String pinmode()
{
int pin=str2int(rdata("pin ?"));
int mode=str2int(rdata("mode ?"));
pinMode(pin,mode);
return "";
}
String digitalwrite()
{
int pin=str2int(rdata("pin ?"));
int value=str2int(rdata("value ?"));
digitalWrite(pin,value);
return "";
}
String digitalread()
{
int pin=str2int(rdata("pin ?"));
return digitalRead(pin);
}
String analogreference()
{
int pin=str2int(rdata("type ?"));
analogReference(pin);
return "";
}
String analogwrite()
{
int pin=str2int(rdata("pin ?"));
int value=str2int(rdata("value ?"));
analogWrite(pin, value);
return "";
}
String analogread()
{
int pin=str2int(rdata("pin ?"));
return analogRead(pin);
}
String tones()
{
int pin=str2int(rdata("pin ?"));
int frequency=str2int(rdata("frequency ?"));
int duration=str2int(rdata("duration ?"));
tone(pin, frequency, duration);
return "";
}
String notones()
{
int pin=str2int(rdata("pin ?"));
noTone(pin);
return "";
}
String shiftout()
{
int dataPin=str2int(rdata("dataPin ?"));
int clockPin=str2int(rdata("clockPin ?"));
int bitOrder=str2int(rdata("bitOrder ?"));
int value=str2int(rdata("value ?"));
shiftOut(dataPin, clockPin, bitOrder, value);
return "";
}
String shiftin()
{
int dataPin=str2int(rdata("dataPin ?"));
int clockPin=str2int(rdata("clockPin ?"));
int bitOrder=str2int(rdata("bitOrder ?"));
return shiftIn(dataPin, clockPin, bitOrder);
}
String pulsein()
{
int pin=str2int(rdata("pin ?"));
int value=str2int(rdata("value ?"));
int timeout=str2int(rdata("timeout ?"));
return pulseIn(pin, value, timeout);
}
String attachinterrupt()
{
int interrupt=str2int(rdata("interrupt ?"));
if (interrupt == 0)
{
attachInterrupt(interrupt, pycallback0, CHANGE);
}
if (interrupt == 1)
{
attachInterrupt(interrupt, pycallback1, CHANGE);
}
return "";
}
void pycallback0()
{
int0 = !int0;
}
void pycallback1()
{
int1 = !int1;
}
String detachinterrupt()
{
int interrupt=str2int(rdata("interrupt ?"));
detachInterrupt(interrupt);
return "";
}
String cinterrupts()
{
interrupts();
return "";
}
String nocinterrupts()
{
noInterrupts();
return "";
}
String echo()
{
return rdata(cmd+" ?");
}
String pycall()
{
String fc=rdata("Func ?");
return funcparser(fc);
}
String funcparser(String cmdc)
{
byte i = 0;
func = &unknown;
for(i = 0; i != ARRAY_SIZE(commands); ++i)
{
if (commands[i].cmd.equalsIgnoreCase(cmdc))
{
func = commands[i].func;
break;
}
}
return func();
}
void setup()
{
Serial.begin(115200);
}
void loop()
{
cmdlck = "";
cmd = rdata("?");
cmdlck = cmd;
sprintln(funcparser(cmd));
}
|