mmb.py
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 """__NATIVE__
00031 #include <stdio.h>
00032 #include <avr/io.h>
00033 #include <util/delay.h>
00034 #include "libmmb103.h"
00035 """
00036
00037
00038 def adc_get(c):
00039 """__NATIVE__
00040 /* The arg is the ADC channel to read */
00041 pPmObj_t pc = C_NULL;
00042 pPmObj_t pr = C_NULL;
00043 uint8_t chan;
00044 PmReturn_t retval;
00045
00046 /* If wrong number of args, throw type exception */
00047 if (NATIVE_GET_NUM_ARGS() != 1)
00048 {
00049 PM_RAISE(retval, PM_RET_EX_TYPE);
00050 return retval;
00051 }
00052
00053 /* Get arg, throw type exception if it's not an int */
00054 pc = NATIVE_GET_LOCAL(0);
00055 if (OBJ_GET_TYPE(pc) != OBJ_TYPE_INT)
00056 {
00057 PM_RAISE(retval, PM_RET_EX_TYPE);
00058 return retval;
00059 }
00060
00061 /* Get the channel number */
00062 chan = (uint8_t)(((pPmInt_t)pc)->val & 0x03);
00063
00064 /* Return new int from the conversion result on the stack */
00065 retval = int_new(mmb_adc_get(chan), &pr);
00066 NATIVE_SET_TOS(pr);
00067
00068 return retval;
00069 """
00070 pass
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168 def dip_get(c):
00169 """__NATIVE__
00170 pPmObj_t pc = C_NULL;
00171 pPmObj_t pr = C_NULL;
00172 int8_t chan;
00173 PmReturn_t retval;
00174
00175 /* If no args, get all dip switches as one byte */
00176 if (NATIVE_GET_NUM_ARGS() == 0)
00177 {
00178 retval = int_new(mmb_dip_get_byte(), &pr);
00179 }
00180
00181 /* If one arg, get the designated dip switch */
00182 else if (NATIVE_GET_NUM_ARGS() == 1)
00183 {
00184 /* Raise TypeError if arg is not an int */
00185 pc = NATIVE_GET_LOCAL(0);
00186 if (OBJ_GET_TYPE(pc) != OBJ_TYPE_INT)
00187 {
00188 PM_RAISE(retval, PM_RET_EX_TYPE);
00189 return retval;
00190 }
00191
00192 /* Get the channel number and read that dip switch */
00193 chan = (int8_t)(((pPmInt_t)pc)->val & (int8_t)0x03);
00194 retval = int_new(mmb_dip_get(chan), &pr);
00195 }
00196
00197 /* Raise TypeError if wrong number of args */
00198 else
00199 {
00200 PM_RAISE(retval, PM_RET_EX_TYPE);
00201 }
00202
00203 NATIVE_SET_TOS(pr);
00204 return retval;
00205 """
00206 pass
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253 def lcd_print(ps):
00254 """__NATIVE__
00255 pPmObj_t ps = C_NULL;
00256 uint8_t *s = C_NULL;
00257 PmReturn_t retval;
00258
00259 /* If wrong number of args, throw type exception */
00260 if (NATIVE_GET_NUM_ARGS() != 1)
00261 {
00262 PM_RAISE(retval, PM_RET_EX_TYPE);
00263 return retval;
00264 }
00265
00266 /* Get the arg, throw type exception if needed */
00267 ps = NATIVE_GET_LOCAL(0);
00268 if (OBJ_GET_TYPE(ps) != OBJ_TYPE_STR)
00269 {
00270 PM_RAISE(retval, PM_RET_EX_TYPE);
00271 return retval;
00272 }
00273
00274 /* Get a pointer to the string */
00275 s = ((pPmString_t)ps)->val;
00276
00277 /* Print the string on the mmb's lcd */
00278 mmb_lcd_print_str(s);
00279
00280 /* Return none obj on stack */
00281 NATIVE_SET_TOS(PM_NONE);
00282
00283 return PM_RET_OK;
00284 """
00285 pass
00286
00287
00288 def lcd_set_line(n):
00289 """__NATIVE__
00290 pPmObj_t pn = C_NULL;
00291 uint8_t n;
00292 PmReturn_t retval;
00293
00294 /* If wrong number of args, throw type exception */
00295 if (NATIVE_GET_NUM_ARGS() != 1)
00296 {
00297 PM_RAISE(retval, PM_RET_EX_TYPE);
00298 return retval;
00299 }
00300
00301 /* Get the arg, throw type exception if needed */
00302 pn = NATIVE_GET_LOCAL(0);
00303 if (OBJ_GET_TYPE(pn) != OBJ_TYPE_INT)
00304 {
00305 PM_RAISE(retval, PM_RET_EX_TYPE);
00306 return retval;
00307 }
00308
00309 /* Get the line number and set the cursor to that line */
00310 n = (uint8_t)((pPmInt_t)pn)->val;
00311 mmb_lcd_set_cursor(n, 0);
00312
00313 /* Return none obj on stack */
00314 NATIVE_SET_TOS(PM_NONE);
00315
00316 return PM_RET_OK;
00317 """
00318 pass
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342 def set_pwm_a(n):
00343 """__NATIVE__
00344 pPmObj_t pn = C_NULL;
00345 int16_t duty;
00346 PmReturn_t retval;
00347
00348 /* If wrong number of args, throw type exception */
00349 if (NATIVE_GET_NUM_ARGS() != 1)
00350 {
00351 PM_RAISE(retval, PM_RET_EX_TYPE);
00352 return retval;
00353 }
00354
00355 /* Get the arg, throw exception if needed */
00356 pn = NATIVE_GET_LOCAL(0);
00357 if (OBJ_GET_TYPE(pn) != OBJ_TYPE_INT)
00358 {
00359 PM_RAISE(retval, PM_RET_EX_TYPE);
00360 return retval;
00361 }
00362
00363 /* Get the duty cycle value */
00364 duty = (int16_t)((pPmInt_t)pn)->val;
00365
00366 mmb_set_mota_pwm(duty);
00367
00368 /* Return none obj on stack */
00369 NATIVE_SET_TOS(PM_NONE);
00370
00371 return PM_RET_OK;
00372 """
00373 pass
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410 def sleepms(ms):
00411 """__NATIVE__
00412 pPmObj_t pms = C_NULL;
00413 int16_t ms;
00414 PmReturn_t retval;
00415
00416 /* If wrong number of args, throw type exception */
00417 if (NATIVE_GET_NUM_ARGS() != 1)
00418 {
00419 PM_RAISE(retval, PM_RET_EX_TYPE);
00420 return retval;
00421 }
00422
00423 /* Get the arg, throw type exception if needed */
00424 pms = NATIVE_GET_LOCAL(0);
00425 if (OBJ_GET_TYPE(pms) != OBJ_TYPE_INT)
00426 {
00427 PM_RAISE(retval, PM_RET_EX_TYPE);
00428 return retval;
00429 }
00430
00431 /* Get the delay value, apply API limits and call delay fxn */
00432 ms = (int16_t)((pPmInt_t)pms)->val;
00433 if (ms < 0) ms = 0;
00434 else if (ms > 65) ms = 65;
00435 _delay_ms(ms);
00436
00437 /* Return none obj on stack */
00438 NATIVE_SET_TOS(PM_NONE);
00439
00440 return PM_RET_OK;
00441 """
00442 pass
00443
00444