-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbitlashMio-functionsOld.h
509 lines (464 loc) · 13 KB
/
bitlashMio-functionsOld.h
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/***
bitlash-functions.c
Bitlash is a tiny language interpreter that provides a serial port shell environment
for bit banging and hardware hacking.
Bitlash lives at: http://bitlash.net
The author can be reached at: bill@bitlash.net
Copyright (C) 2008-2012 Bill Roy
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
***/
//#include "bitlashMio.h"
#include <TimeLib.h>
#undef TINY_BUILD
// syntactic sugar for func_handlers()
#if 0 // 15022 bytes
#define arg1 getarg(1)
#define arg2 getarg(2)
#define arg3 getarg(3)
#endif
extern long VARIAB[];
// 14772 vs 15022
extern numvar *arg;
#define arg1 arg[-1]
#define arg2 arg[-2]
#define arg3 arg[-3]
#define arg4 arg[-4]
#define arg5 arg[-5]
void reqargs(byte n) { if (arg[0] < n) missing(M_arg); }
// this costs 500 bytes more!
//#define reqargs(n) { if (arg[0] < n) missing(M_arg); }
///////////////////////
// FUNCTION HANDLERS
//
// The native tone() routine first included in Arduino 0018 is quite large,
// about 1k differential: 15196 - 14110
// It is, however, for the moment quite a bit more frequency-accurate
//
//#define USE_NATIVE_TONE
#ifdef USE_NATIVE_TONE
void make_beep(unumvar pin, unumvar frequency, unumvar duration) {
tone(pin, frequency, duration);
delay(duration);
}
#else
numvar func_beep(void) { // unumvar pin, unumvar frequency, unumvar duration)
reqargs(3);
unsigned long cycles = ((unsigned long) arg2 * (unsigned long) arg3) / 1000UL;
unsigned long halfperiod = (500000UL / (unsigned long) arg2) - 7UL; // 7 fudge
// todo: check for break in here? This could go 32 seconds...
pinMode(arg1, OUTPUT);
while (cycles--) {
digitalWrite(arg1, HIGH);
delayMicroseconds(halfperiod);
digitalWrite(arg1, LOW);
delayMicroseconds(halfperiod-1);
}
return 0;
}
#endif
numvar func_free(void) {
#ifdef ESP8266
return 0;
#elif defined(UNIX_BUILD)
return 1000L;
#elif defined(ARM_BUILD)
return 1000L;
#else
numvar ret;
// from http://forum.pololu.com/viewtopic.php?f=10&t=989&view=unread#p4218
extern int __bss_end;
return ((int)&ret) - ((int)&__bss_end);
#endif
}
#if 0
void zapheap(void) {
int heaptop;
extern int __bss_end;
//Serial.println((int) &__bss_end, DEC);
memset(((char *)&__bss_end)+1, 0xbb, (&heaptop - &__bss_end) - 6);
}
#endif
///////////////////////////////////////////////
// Dead Beef Random Number Generator
// from http://inglorion.net/software/deadbeef_rand/
// stated license terms: "Feel free to use the code in your own software."
///////////////////////////////////////////////
//
static uint32_t deadbeef_seed = 0xbeefcafe;
static uint32_t deadbeef_beef = 0xdeadbeef;
numvar func_random(void) {
unumvar ret;
reqargs(1);
deadbeef_seed = (deadbeef_seed << 7) ^ ((deadbeef_seed >> 25) + deadbeef_beef);
ret = ((numvar) deadbeef_seed & 0x7fffffff) % arg1;
deadbeef_beef = (deadbeef_beef << 7) ^ ((deadbeef_beef >> 25) + 0xdeadbeef);
return ret;
}
// add entropy to the pool
void stir(byte trash) {
deadbeef_seed ^= 1 << (trash & 0x1f);
}
#if 0
// seed the random number generator
void dbseed(uint32_t x) {
deadbeef_seed = x;
deadbeef_beef = 0xdeadbeef;
}
#endif
numvar func_now(void) { return now(); }
/////////////////////////////////////////
// memory mapped port io: inb() and outb()
//
// see the table of register addresses on page 203 of the Tiny85 spec
// it is necessary to add 0x20 to the address in the "register" column
// to get the proper mapped address for the register
// thus: TCCR0B assigned 0x33 is accesssible at 0x53
// >print inb(0x53)
// 2
//
numvar func_val(void) { reqargs(1); return VARIAB[arg1]; }
numvar func_inb(void) { reqargs(1); return *(volatile byte *) arg1; }
numvar func_outb(void) { reqargs(2); *(volatile byte *) arg1 = (byte) arg2; return 0;}
numvar func_abs(void) { reqargs(1); return arg1 < 0 ? -arg1 : arg1; }
numvar func_sign(void) {
reqargs(1);
if (arg1 < 0) return -1;
if (arg1 > 0) return 1;
return 0;
}
numvar func_min(void) { reqargs(2); return (arg1 < arg2) ? arg1 : arg2; }
numvar func_max() { reqargs(2); return (arg1 > arg2) ? arg1 : arg2; }
numvar func_constrain(void) {
reqargs(3);
if (arg1 < arg2) return arg2;
if (arg1 > arg3) return arg3;
return arg1;
}
numvar func_ar(void) { reqargs(1); return analogRead(arg1); }
numvar func_aw(void) { reqargs(2); analogWrite(arg1, arg2); return 0; }
numvar func_dr(void) { reqargs(1); return digitalRead(arg1); }
numvar func_dw(void) { reqargs(2); digitalWrite(arg1, arg2); return 0; }
numvar func_er(void) { reqargs(1); return eeread(arg1); }
numvar func_ew(void) { reqargs(2); eewrite(arg1, arg2); return 0; }
numvar func_pinmode(void) { reqargs(2); pinMode(arg1, arg2); return 0; }
numvar func_pulsein(void) { reqargs(3); return pulseIn(arg1, arg2, arg3); }
numvar func_snooze(void) { reqargs(1); /*snooze(arg1);*/ return 0; }
numvar func_delay(void) { reqargs(1); delay(arg1); return 0; }
#if !defined(TINY_BUILD)
//numvar func_setBaud(void) { reqargs(2); setBaud(arg1, arg2); return 0; }
#endif
//numvar func_map(void) { reqargs(5); return map(arg1, arg2, arg3, arg4, arg5); }
//numvar func_shiftout(void) { reqargs(4); shiftOut(arg1, arg2, arg3, arg4); return 0; }
numvar func_bitclear(void) { reqargs(2); return arg1 & ~((numvar)1 << arg2); }
numvar func_bitset(void) { reqargs(2); return arg1 | ((numvar)1 << arg2); }
numvar func_bitread(void) { reqargs(2); return (arg1 & ((numvar)1 << arg2)) != 0; }
numvar func_bitwrite(void) { reqargs(3); return arg3 ? func_bitset() : func_bitclear(); }
numvar func_getkey(void) {
if (getarg(0) > 0) sp((char *) getarg(1));
while (!Serial.available()) {;} // blocking!
return (numvar) Serial.read();
}
numvar func_getnum(void) {
numvar num = 0;
if (getarg(0) > 0) sp((char *) getarg(1));
for (;;) {
while (!Serial.available()) {;} // blocking!
int k = Serial.read();
if ((k == '\r') || (k == '\n')) {
speol();
return num;
}
else if ((k >= '0') && (k <= '9')) {
num = num * 10L + (long) (k - '0');
}
else if (k == '-') num = -num;
else if ((k == 8) || (k == 0x7f)) {
if (num != 0) {
num /= 10L;
spb(8); spb(' '); spb(8);
}
}
else {
spb(7); // beep
continue;
}
spb(k); // else echo what we ate
}
}
//////////
// Function name dictionary
//
// Function dispatch is accomplished by looking up the proposed function name in this structure,
// which must be in ascending alphabetical order.
//
// The index at which a matching function name is found is used to reference
// the function_table defined below to get the argument signature and C function
// address for the handler.
//
// MAINTENANCE NOTE: This dictionary must be sorted in alpha order
// and must be 1:1 with function_table below.
//
#if defined(TINY_BUILD)
const /*prog_*/char functiondict[] /*PROGMEM*/ = { "delay\0free\0millis\0now\0pinmode\0snooze\0"
/*
// "abs\0"
// "ar\0"
// "aw\0"
// "baud\0"
// "bc\0"
// "beep\0"
// "br\0"
// "bs\0"
// "bw\0"
// "constrain\0"
"delay\0",
// "dr\0"
// "dw\0"
// "er\0"
// "ew\0"
"free\0",
// "inb\0"
// "map\0"
// "max\0"
"millis\0",
// "min\0"
// "outb\0"
"pinmode\0",
// "printf\0"
// "pulsein\0"
// "random\0"
// "shiftout\0"
// "sign\0"
"snooze\0"*/
};
#else // standard function set
const /*prog_*/char functiondict[] /*PROGMEM*/ = {"abs\0ar\0aw\0bc\0beep\0br\0bs\0bw\0constrain\0delay\0dr\0dw\0er\0ew\0free\0getkey\0getnum\0inb\0max\0millis\0min\0now\0outb\0pinmode\0pulsein\0random\0val\0sign\0snooze\0"
/* "abs\0"
"ar\0"
"aw\0"
// "baud\0"
"bc\0"
"beep\0"
"br\0"
"bs\0"
"bw\0"
"constrain\0"
"delay\0"
"dr\0"
"dw\0"
"er\0"
"ew\0"
"free\0"
"getkey\0"
"getnum\0"
"inb\0"
// "isstr\0"
// "map\0"
"max\0"
"millis\0"
"min\0"
"outb\0"
"pinmode\0"
// "printf\0"
"pulsein\0"
"random\0"
// "shiftout\0"
"sign\0"
"snooze\0"*/
};
#endif
// function_table
//
// this must be 1:1 with the symbols above, which in turn must be in alpha order
//
#if defined(TINY_BUILD)
const bitlash_function function_table[] /*PROGMEM*/ = {
// func_abs,
// func_ar,
// func_aw,
// func_setBaud,
// func_bitclear,
// func_beep,
// func_bitread,
// func_bitset,
// func_bitwrite,
// func_constrain,
func_delay,
// func_dr,
// func_dw,
// func_er,
// func_ew,
func_free,
// func_inb,
// func_map,
// func_max,
(bitlash_function) millis,
// func_min,
func_now,
// func_outb,
func_pinmode,
// func_printf,
// func_pulsein,
// func_random,
// func_shiftout,
// func_sign,
func_snooze // last one no comma!
};
#else // standard function set
const bitlash_function function_table[]/* PROGMEM*/ = {
func_abs,
func_ar,
func_aw,
// func_setBaud,
func_bitclear,
func_beep,
func_bitread,
func_bitset,
func_bitwrite,
func_constrain,
func_delay,
func_dr,
func_dw,
func_er,
func_ew,
func_free,
func_getkey,
func_getnum,
func_inb,
// isstring,
// func_map,
func_max,
(bitlash_function) millis,
func_min,
func_now,
func_outb,
func_pinmode,
// func_printf,
func_pulsein,
func_random,
func_val,
// func_shiftout,
func_sign,
func_snooze // last one no comma!
};
#endif
// Enable USER_FUNCTIONS to include the add_bitlash_function() extension mechanism
// This costs about 256 bytes
//
#if !defined(TINY_BUILD)
#define USER_FUNCTIONS
#endif
#ifdef USER_FUNCTIONS
#define MAX_USER_FUNCTIONS 20 // increase this if needed, but keep free() > 200 ish
#define USER_FUNCTION_FLAG 0x80
typedef struct {
const char *name; // pointer to the name
bitlash_function func_ptr; // pointer to the implementing function
} user_functab_entry;
byte bf_install_count; // number of installed functions
user_functab_entry user_functions[MAX_USER_FUNCTIONS]; // the table
//////////
// addBitlashFunction: add a function to the user function table
//
//
// name: Pointer to a string containing the name for the function, like "myfunc".
//
// Note: Since the user table is searched last, attempting to redefine an
// existing function will fail silently, leading to interesting debugging experience.
//
// func_ptr: pointer to the implementing C function
//
// if it weren't built-in, you could add millis() like this:
// addBitlashFunction("millis", (bitlash_function) millis);
//
// Bitlash uses the typedefs "numvar" and "unumvar" for signed and unsigned values within the
// bitlash calculation engine. The engine can be configured for 16- or 32-bit calculations
// by changing these definitions.
//
//
// Example:
// numvar foo_the_function(void) { return random(arg(1)) * random(arg(2)); }
// addBitlashFunction("foo", &foo_the_function);
//
// then in Bitlash you can say:
// > print foo(22,33)
// 148
//
void addBitlashFunction(const char *name, bitlash_function func_ptr) {
if (bf_install_count >= MAX_USER_FUNCTIONS) overflow(M_functions);
user_functions[bf_install_count].name = name;
user_functions[bf_install_count].func_ptr = func_ptr;
bf_install_count++;
}
//////////
// find_user_function: find id in the user function table.
// return true if found, with the user function token in symval (with USER_FUNCTION_FLAG set)
//
char find_user_function(char *id) {
symval = 0;
while (symval < bf_install_count) {
if (!strcmp(id, user_functions[symval].name)) {
symval |= USER_FUNCTION_FLAG;
return 1;
}
symval++;
}
return 0;
}
//////////
//
// show_user_functions: display a list of registered user functions
//
void show_user_functions(void) {
byte i;
for (i=0; i < bf_install_count; i++) {
sp(user_functions[i].name);
spb(' ');
}
speol();
}
#endif // USER_FUNCTIONS
//////////
// dofunctioncall(): evaluate a function reference
//
// parse the argument list, marshall the arguments and call the function,
// and push its return value, if any, on the value stack
//
void dofunctioncall(byte entry) {
bitlash_function fp;
#ifdef USER_FUNCTIONS
// Detect and handle a user function: its id has the high bit set
// we set nargs and fp and fall through to masquerade as a built-in
if (entry & USER_FUNCTION_FLAG) {
fp = (bitlash_function) user_functions[entry & 0x7f].func_ptr;
}
else
#endif
// built-in function
#ifdef ESP8266
fp = function_table[entry];
#else
fp = (bitlash_function) pgm_read_word(&function_table[entry]);
#endif
parsearglist(); // parse the arguments
numvar ret = (*fp)(); // call the function
releaseargblock(); // peel off the arguments
vpush(ret); // and push the return value
}