IPS(Interpreter for Process Structures) for mbed

Dependencies:   ConfigFile FATFileSystem mbed

IPS port from linux/unix version.

mbed_blinky.ips

0 VAR led1
" LED1 " DigitalOut led1 !
: main
    ANFANG
    1 JA?
      1 led1 @ write
      200 wait_ms
      0 led1 @ write
      200 wait_ms
    DANN/NOCHMAL
;
main
Committer:
va009039
Date:
Sun May 24 21:29:48 2015 +0900
Revision:
4:b62b40563944
Parent:
2:908338b1151a
fix I2C

Who changed what in which revision?

UserRevisionLine numberNew contents of line
va009039 2:908338b1151a 1 // mbedAPI.cpp 2015/5/22
va009039 2:908338b1151a 2 #include <algorithm>
va009039 2:908338b1151a 3 #include "mbed.h"
va009039 2:908338b1151a 4 #include "mbedAPI.h"
va009039 2:908338b1151a 5 #include "PinNameTable.h"
va009039 2:908338b1151a 6
va009039 2:908338b1151a 7 // $MBEDAPI
va009039 2:908338b1151a 8 #define mbed_wait 1
va009039 2:908338b1151a 9 #define mbed_wait_ms 2
va009039 2:908338b1151a 10 #define mbed_DigitalOut 3
va009039 2:908338b1151a 11 #define mbed_DigitalIn 4
va009039 2:908338b1151a 12 #define mbed_DigitalInOut 5
va009039 2:908338b1151a 13 #define mbed_Timer 6
va009039 2:908338b1151a 14 #define mbed_RawSerial 7
va009039 2:908338b1151a 15 #define mbed_read 8
va009039 2:908338b1151a 16 #define mbed_write 9
va009039 2:908338b1151a 17 #define mbed_putc 10
va009039 2:908338b1151a 18 #define mbed_getc 11
va009039 2:908338b1151a 19 #define mbed_readable 12
va009039 2:908338b1151a 20 #define mbed_baud 13
va009039 2:908338b1151a 21 #define mbed_SPI 14
va009039 2:908338b1151a 22 #define mbed_I2C 15
va009039 2:908338b1151a 23 #define mbed_start 16
va009039 2:908338b1151a 24 #define mbed_stop 17
va009039 2:908338b1151a 25 #define mbed_reset 18
va009039 2:908338b1151a 26 #define mbed_read_ms 19
va009039 2:908338b1151a 27 #define mbed_read_us 20
va009039 2:908338b1151a 28 #define mbed_PwmOut 21
va009039 2:908338b1151a 29 #define mbed_period_ms 22
va009039 2:908338b1151a 30 #define mbed_period_us 23
va009039 2:908338b1151a 31 #define mbed_pulsewidth_ms 24
va009039 2:908338b1151a 32 #define mbed_pulsewidth_us 25
va009039 2:908338b1151a 33 #define mbed_AnalogIn 26
va009039 2:908338b1151a 34 #define mbed_read_u16 27
va009039 2:908338b1151a 35
va009039 2:908338b1151a 36 void mbedAPI::code() {
va009039 2:908338b1151a 37 int f = ips.pull_ps();
va009039 2:908338b1151a 38 uint16_t a1;
va009039 2:908338b1151a 39 switch(f) {
va009039 2:908338b1151a 40 case mbed_wait_ms:
va009039 2:908338b1151a 41 a1 = ips.pull_ps();
va009039 2:908338b1151a 42 wait_ms(a1);
va009039 2:908338b1151a 43 break;
va009039 2:908338b1151a 44 case mbed_Timer:
va009039 2:908338b1151a 45 init<Timer>(f);
va009039 2:908338b1151a 46 break;
va009039 2:908338b1151a 47 case mbed_DigitalOut:
va009039 2:908338b1151a 48 init<DigitalOut, PinName>(f);
va009039 2:908338b1151a 49 break;
va009039 2:908338b1151a 50 case mbed_DigitalIn:
va009039 2:908338b1151a 51 init<DigitalIn, PinName>(f);
va009039 2:908338b1151a 52 break;
va009039 2:908338b1151a 53 case mbed_DigitalInOut:
va009039 2:908338b1151a 54 init<DigitalInOut, PinName>(f);
va009039 2:908338b1151a 55 break;
va009039 2:908338b1151a 56 case mbed_AnalogIn:
va009039 2:908338b1151a 57 init<AnalogIn, PinName>(f);
va009039 2:908338b1151a 58 break;
va009039 2:908338b1151a 59 case mbed_PwmOut:
va009039 2:908338b1151a 60 init<PwmOut, PinName>(f);
va009039 2:908338b1151a 61 break;
va009039 2:908338b1151a 62 case mbed_RawSerial:
va009039 2:908338b1151a 63 init<RawSerial, PinName, PinName>(f);
va009039 2:908338b1151a 64 break;
va009039 2:908338b1151a 65 case mbed_SPI:
va009039 2:908338b1151a 66 init<SPI, PinName, PinName, PinName>(f);
va009039 2:908338b1151a 67 break;
va009039 2:908338b1151a 68 case mbed_I2C:
va009039 2:908338b1151a 69 init<I2C, PinName, PinName>(f);
va009039 2:908338b1151a 70 break;
va009039 2:908338b1151a 71 default:
va009039 2:908338b1151a 72 code_method(f);
va009039 2:908338b1151a 73 break;
va009039 2:908338b1151a 74 }
va009039 2:908338b1151a 75 }
va009039 2:908338b1151a 76
va009039 2:908338b1151a 77 void mbedAPI::code_method(int f) {
va009039 2:908338b1151a 78 mbedObj obj = pull_ps_obj();
va009039 2:908338b1151a 79 uint16_t a1;
va009039 2:908338b1151a 80 switch(obj.ct) {
va009039 2:908338b1151a 81 case mbed_DigitalOut:
va009039 2:908338b1151a 82 switch(f) {
va009039 2:908338b1151a 83 case mbed_write:
va009039 2:908338b1151a 84 method<DigitalOut, int, &DigitalOut::write>(obj.p);
va009039 2:908338b1151a 85 break;
va009039 2:908338b1151a 86 case mbed_read:
va009039 2:908338b1151a 87 method<int, DigitalOut, &DigitalOut::read>(obj.p);
va009039 2:908338b1151a 88 break;
va009039 2:908338b1151a 89 default:
va009039 2:908338b1151a 90 break;
va009039 2:908338b1151a 91 }
va009039 2:908338b1151a 92 break;
va009039 2:908338b1151a 93 case mbed_DigitalInOut:
va009039 2:908338b1151a 94 switch(f) {
va009039 2:908338b1151a 95 case mbed_write:
va009039 2:908338b1151a 96 method<DigitalInOut, int, &DigitalInOut::write>(obj.p);
va009039 2:908338b1151a 97 break;
va009039 2:908338b1151a 98 case mbed_read:
va009039 2:908338b1151a 99 method<int, DigitalInOut, &DigitalInOut::read>(obj.p);
va009039 2:908338b1151a 100 break;
va009039 2:908338b1151a 101 default:
va009039 2:908338b1151a 102 break;
va009039 2:908338b1151a 103 }
va009039 2:908338b1151a 104 break;
va009039 2:908338b1151a 105 case mbed_AnalogIn:
va009039 2:908338b1151a 106 switch(f) {
va009039 2:908338b1151a 107 case mbed_read_u16:
va009039 2:908338b1151a 108 method<unsigned short, AnalogIn, &AnalogIn::read_u16>(obj.p);
va009039 2:908338b1151a 109 break;
va009039 2:908338b1151a 110 default:
va009039 2:908338b1151a 111 error("%s %d", __LINE__, f);
va009039 2:908338b1151a 112 break;
va009039 2:908338b1151a 113 }
va009039 2:908338b1151a 114 break;
va009039 2:908338b1151a 115 case mbed_PwmOut:
va009039 2:908338b1151a 116 switch(f) {
va009039 2:908338b1151a 117 case mbed_period_ms:
va009039 2:908338b1151a 118 method<PwmOut, int, &PwmOut::period_ms>(obj.p);
va009039 2:908338b1151a 119 break;
va009039 2:908338b1151a 120 case mbed_period_us:
va009039 2:908338b1151a 121 method<PwmOut, int, &PwmOut::period_us>(obj.p);
va009039 2:908338b1151a 122 break;
va009039 2:908338b1151a 123 case mbed_pulsewidth_ms:
va009039 2:908338b1151a 124 method<PwmOut, int, &PwmOut::pulsewidth_ms>(obj.p);
va009039 2:908338b1151a 125 break;
va009039 2:908338b1151a 126 case mbed_pulsewidth_us:
va009039 2:908338b1151a 127 method<PwmOut, int, &PwmOut::pulsewidth_us>(obj.p);
va009039 2:908338b1151a 128 break;
va009039 2:908338b1151a 129 default:
va009039 2:908338b1151a 130 error("%s %d", __LINE__, f);
va009039 2:908338b1151a 131 break;
va009039 2:908338b1151a 132 }
va009039 2:908338b1151a 133 break;
va009039 2:908338b1151a 134 case mbed_Timer:
va009039 2:908338b1151a 135 switch(f) {
va009039 2:908338b1151a 136 case mbed_start:
va009039 2:908338b1151a 137 method<Timer, &Timer::start>(obj.p);
va009039 2:908338b1151a 138 break;
va009039 2:908338b1151a 139 case mbed_stop:
va009039 2:908338b1151a 140 method<Timer, &Timer::stop>(obj.p);
va009039 2:908338b1151a 141 break;
va009039 2:908338b1151a 142 case mbed_reset:
va009039 2:908338b1151a 143 method<Timer, &Timer::reset>(obj.p);
va009039 2:908338b1151a 144 break;
va009039 2:908338b1151a 145 case mbed_read_ms:
va009039 2:908338b1151a 146 method<int, Timer, &Timer::read_ms>(obj.p);
va009039 2:908338b1151a 147 break;
va009039 2:908338b1151a 148 case mbed_read_us:
va009039 2:908338b1151a 149 method<int, Timer, &Timer::read_us>(obj.p);
va009039 2:908338b1151a 150 break;
va009039 2:908338b1151a 151 default:
va009039 2:908338b1151a 152 error("%s %d", __LINE__, f);
va009039 2:908338b1151a 153 break;
va009039 2:908338b1151a 154 }
va009039 2:908338b1151a 155 break;
va009039 2:908338b1151a 156 case mbed_DigitalIn:
va009039 2:908338b1151a 157 switch(f) {
va009039 2:908338b1151a 158 case mbed_read:
va009039 2:908338b1151a 159 method<int, DigitalIn, &DigitalIn::read>(obj.p);
va009039 2:908338b1151a 160 break;
va009039 2:908338b1151a 161 default:
va009039 2:908338b1151a 162 error("%s %d", __LINE__, f);
va009039 2:908338b1151a 163 break;
va009039 2:908338b1151a 164 }
va009039 2:908338b1151a 165 break;
va009039 2:908338b1151a 166 case mbed_SPI:
va009039 2:908338b1151a 167 switch(f) {
va009039 2:908338b1151a 168 case mbed_write:
va009039 2:908338b1151a 169 method<int, SPI, int, &SPI::write>(obj.p);
va009039 2:908338b1151a 170 break;
va009039 2:908338b1151a 171 default:
va009039 2:908338b1151a 172 error("%s %d", __LINE__, f);
va009039 2:908338b1151a 173 break;
va009039 2:908338b1151a 174 }
va009039 2:908338b1151a 175 break;
va009039 2:908338b1151a 176 case mbed_I2C:
va009039 2:908338b1151a 177 code_method_I2C(f, obj);
va009039 2:908338b1151a 178 break;
va009039 2:908338b1151a 179 case mbed_RawSerial:
va009039 2:908338b1151a 180 switch(f) {
va009039 2:908338b1151a 181 case mbed_putc:
va009039 2:908338b1151a 182 a1 = ips.pull_ps();
va009039 2:908338b1151a 183 reinterpret_cast<RawSerial*>(obj.p)->putc(a1);
va009039 2:908338b1151a 184 break;
va009039 2:908338b1151a 185 case mbed_getc:
va009039 2:908338b1151a 186 method<int, RawSerial, &RawSerial::getc>(obj.p);
va009039 2:908338b1151a 187 break;
va009039 2:908338b1151a 188 case mbed_readable:
va009039 2:908338b1151a 189 ips.push_ps(reinterpret_cast<RawSerial*>(obj.p)->readable());
va009039 2:908338b1151a 190 break;
va009039 2:908338b1151a 191 case mbed_baud:
va009039 2:908338b1151a 192 a1 = ips.pull_ps();
va009039 2:908338b1151a 193 reinterpret_cast<RawSerial*>(obj.p)->baud(a1);
va009039 2:908338b1151a 194 break;
va009039 2:908338b1151a 195 default:
va009039 2:908338b1151a 196 error("%s %d", __LINE__, f);
va009039 2:908338b1151a 197 break;
va009039 2:908338b1151a 198 }
va009039 2:908338b1151a 199 break;
va009039 2:908338b1151a 200 default:
va009039 2:908338b1151a 201 break;
va009039 2:908338b1151a 202 }
va009039 2:908338b1151a 203 }
va009039 2:908338b1151a 204
va009039 2:908338b1151a 205 void mbedAPI::code_method_I2C(int f, mbedObj& obj) {
va009039 2:908338b1151a 206 if (f == mbed_read) {
va009039 4:b62b40563944 207 uint16_t addr = ips.pull_ps();
va009039 4:b62b40563944 208 uint16_t len = ips.pull_ps();
va009039 4:b62b40563944 209 uint16_t ptr = ips.pull_ps();
va009039 4:b62b40563944 210 char buf[len];
va009039 4:b62b40563944 211 reinterpret_cast<I2C*>(obj.p)->read(addr, buf, len);
va009039 4:b62b40563944 212 for(int i = 0; i < len; i++) {
va009039 4:b62b40563944 213 ips.pokeB(ptr + i, buf[i]);
va009039 2:908338b1151a 214 }
va009039 2:908338b1151a 215 } else if (f == mbed_write) {
va009039 4:b62b40563944 216 uint16_t addr = ips.pull_ps();
va009039 4:b62b40563944 217 uint16_t len = ips.pull_ps();
va009039 4:b62b40563944 218 uint16_t ptr = ips.pull_ps();
va009039 4:b62b40563944 219 char buf[len];
va009039 4:b62b40563944 220 for(int i = 0; i < len; i++) {
va009039 4:b62b40563944 221 buf[i] = ips.peekB(ptr + i);
va009039 2:908338b1151a 222 }
va009039 4:b62b40563944 223 reinterpret_cast<I2C*>(obj.p)->write(addr, buf, len);
va009039 2:908338b1151a 224 } else {
va009039 2:908338b1151a 225 error("%s %d", __LINE__, f);
va009039 2:908338b1151a 226 }
va009039 2:908338b1151a 227 }
va009039 2:908338b1151a 228
va009039 2:908338b1151a 229 template<>
va009039 2:908338b1151a 230 PinName mbedAPI::pull_ps<PinName>() {
va009039 2:908338b1151a 231 char buf[64];
va009039 2:908338b1151a 232 pull_ps_string(buf, sizeof(buf));
va009039 2:908338b1151a 233 return findPinName(buf);
va009039 2:908338b1151a 234 }
va009039 2:908338b1151a 235
va009039 2:908338b1151a 236 mbedObj mbedAPI::pull_ps_obj() {
va009039 2:908338b1151a 237 int i = ips.pull_ps();
va009039 2:908338b1151a 238 return objs[i];
va009039 2:908338b1151a 239 }
va009039 2:908338b1151a 240
va009039 2:908338b1151a 241 void mbedAPI::pull_ps_string(char* buf, size_t size) {
va009039 2:908338b1151a 242 size_t s_len = ips.pull_ps();
va009039 2:908338b1151a 243 int s_start = ips.pull_ps();
va009039 2:908338b1151a 244 s_len = std::min(s_len, size-1);
va009039 2:908338b1151a 245 for(size_t i = 0; i < s_len; i++) {
va009039 2:908338b1151a 246 buf[i] = ips.peekB(s_start + i);
va009039 2:908338b1151a 247 }
va009039 2:908338b1151a 248 buf[s_len] = '\0';
va009039 2:908338b1151a 249 }
va009039 2:908338b1151a 250
va009039 2:908338b1151a 251 PinName mbedAPI::findPinName(const char* name) const {
va009039 2:908338b1151a 252 int imin = 0;
va009039 2:908338b1151a 253 int imax = sizeof(pinname_table) / sizeof(pinNameStr) - 1;
va009039 2:908338b1151a 254 while(imax >= imin) {
va009039 2:908338b1151a 255 int i = (imin + imax) / 2;
va009039 2:908338b1151a 256 int c = strcmp(name, pinname_table[i].name);
va009039 2:908338b1151a 257 if (c == 0) {
va009039 2:908338b1151a 258 return pinname_table[i].pin;
va009039 2:908338b1151a 259 } else if (c > 0) {
va009039 2:908338b1151a 260 imin = i + 1;
va009039 2:908338b1151a 261 } else {
va009039 2:908338b1151a 262 imax = i - 1;
va009039 2:908338b1151a 263 }
va009039 2:908338b1151a 264 }
va009039 2:908338b1151a 265 return NC;
va009039 2:908338b1151a 266 }
va009039 2:908338b1151a 267
va009039 2:908338b1151a 268 void mbedAPI::push_ps_obj(void* p, int ct) {
va009039 2:908338b1151a 269 mbedObj obj;
va009039 2:908338b1151a 270 obj.p = p;
va009039 2:908338b1151a 271 obj.ct = ct;
va009039 2:908338b1151a 272 objs.push_back(obj);
va009039 2:908338b1151a 273 uint16_t r1 = objs.size() - 1;
va009039 2:908338b1151a 274 ips.push_ps(r1);
va009039 2:908338b1151a 275 }
va009039 2:908338b1151a 276
va009039 2:908338b1151a 277
va009039 2:908338b1151a 278