Arduino-FVM
Byte Token Threaded Forth Virtual Machine (FVM) for Arduino
Blink.ino
Go to the documentation of this file.
1 
47 #define MEASURE
48 #define BLINK_SKETCH
49 #define BLINK_TRACE
50 #define BLINK_RUN
51 #define CODE_GENERATED
52 
53 // Enable/disable virtual machine code
54 #if defined(BLINK_SKETCH)
55 #include "FVM.h"
56 
57 // Use code generated or manual coded blink sketch
58 #if defined(CODE_GENERATED)
59 
60 /* Source code for token compiler. Note: constants for OUTPUT and LED.
61 ----------------------------------------------------------------------
62 1 constant OUTPUT
63 13 constant LED
64 : blink ( ms pin -- ) begin dup digitaltoggle over delay again ;
65 : sketch ( -- ) OUTPUT LED pinmode 500 LED blink halt ;
66 generate-code
67 ----------------------------------------------------------------------
68 */
69 
70 const char WORD0_PSTR[] PROGMEM = "OUTPUT";
71 const FVM::const_t WORD0_CONST[] PROGMEM = {
72  FVM::OP_CONST, 1
73 };
74 const char WORD1_PSTR[] PROGMEM = "LED";
75 const FVM::const_t WORD1_CONST[] PROGMEM = {
76  FVM::OP_CONST, 13
77 };
78 const char WORD2_PSTR[] PROGMEM = "blink";
79 const FVM::code_t WORD2_CODE[] PROGMEM = {
80  45, 127, 47, 123, 10, -5, 0
81 };
82 const char WORD3_PSTR[] PROGMEM = "sketch";
83 const FVM::code_t WORD3_CODE[] PROGMEM = {
84  -1, -2, 124, 2, -12, 1, -2, -3, 20, 0
85 };
86 const FVM::code_P FVM::fntab[] PROGMEM = {
87  (code_P) &WORD0_CONST,
88  (code_P) &WORD1_CONST,
89  WORD2_CODE,
91 };
92 const str_P FVM::fnstr[] PROGMEM = {
93  (str_P) WORD0_PSTR,
94  (str_P) WORD1_PSTR,
95  (str_P) WORD2_PSTR,
96  (str_P) WORD3_PSTR,
97  0
98 };
99 
101 
102 #else
103 
104 /* Manually translated to C++ source code using FVM macro set:
105 ----------------------------------------------------------------------
106 1 constant OUTPUT
107 13 constant LED
108 : blink ( ms pin -- ) begin dup digitaltoggle over delay again ;
109 : sketch ( -- ) OUTPUT LED pinmode 500 LED blink halt ;
110 ----------------------------------------------------------------------
111 */
112 
113 FVM_CONSTANT(0, OUTPUT_MODE, "OUTPUT", 1);
114 
115 FVM_CONSTANT(1, LED_PIN, "LED", 13);
116 
117 FVM_COLON(2, BLINK, "blink")
118  FVM_OP(DUP),
119  FVM_OP(DIGITALTOGGLE),
120  FVM_OP(OVER),
121  FVM_OP(DELAY),
122  FVM_OP(BRANCH), -5,
123  FVM_OP(EXIT)
124 };
125 
126 FVM_COLON(3, SKETCH, "sketch")
127  FVM_CALL(OUTPUT_MODE),
128  FVM_CALL(LED_PIN),
129  FVM_OP(PINMODE),
130  FVM_LIT(500),
131  FVM_CALL(LED_PIN),
132  FVM_CALL(BLINK),
133  FVM_OP(HALT)
134 };
135 
136 const FVM::code_P FVM::fntab[] PROGMEM = {
137  (code_P) &OUTPUT_MODE_CONST,
138  (code_P) &LED_PIN_CONST,
139  BLINK_CODE,
141 };
142 
143 const str_P FVM::fnstr[] PROGMEM = {
144  (str_P) OUTPUT_MODE_PSTR,
145  (str_P) LED_PIN_PSTR,
146  (str_P) BLINK_PSTR,
147  (str_P) SKETCH_PSTR,
148  0
149 };
150 
152 #endif
153 
155 #endif
156 
157 void setup()
158 {
159  Serial.begin(57600);
160  while (!Serial);
161 
162  // Enable/disable trace
163 #if defined(BLINK_TRACE)
164  task.trace(true);
165 #endif
166 }
167 
168 void loop()
169 {
170  // Enable/disable measurement
171 #if defined(MEASURE)
172  uint32_t start = micros();
173 #endif
174 
175  // Enable/disable resume (base-line foot-print)
176 #if defined(BLINK_RUN)
177  fvm.resume(task);
178 #endif
179 
180  // Enable/disable measurement
181 #if defined(MEASURE)
182  uint32_t stop = micros();
183  uint32_t us = stop-start;
184  Serial.print(stop);
185  Serial.print(':');
186  Serial.println(us);
187  Serial.flush();
188  delay(100);
189 #endif
190 }
#define FVM_CALL(fn)
Definition: FVM.h:675
const FVM::code_t SKETCH_CODE[]
Definition: Demo.ino:31
static const code_P fntab[]
Definition: FVM.h:628
int resume(task_t &task)
Definition: FVM.cpp:143
Definition: FVM.h:339
Handle constant.
Definition: FVM.h:46
const char * str_P
Definition: FVM.h:32
const code_t * code_P
Definition: FVM.h:249
bool trace()
Definition: FVM.h:312
static const str_P fnstr[]
Definition: FVM.h:629
const char SKETCH_PSTR[]
Definition: Demo.ino:30
#define FVM_CONSTANT(id, var, name, val)
Definition: FVM.h:714
int8_t code_t
Definition: FVM.h:248
#define FVM_OP(code)
Definition: FVM.h:652
#define FVM_LIT(n)
Definition: FVM.h:658
#define FVM_COLON(id, var, name)
Definition: FVM.h:728
Definition: FVM.h:34