Debian Codegraph @

online/static/blocks/KuttyPy.js

raw symbols SCIP
1 var textElements={};
2 var txt;
3
4 var stepper_pos=[0,0];
5 var set_stepper_pos = [[],[]], cursteppos=[0,0];
6 var step_positions=[[3,6,12,9,0], [3<<4,6<<4,12<<4,9<<4, 0]];
7
8 // ----------DOM MANIPULATION
9 const SVG_NS = "http://www.w3.org/2000/svg";
10 const SVG_XLINK = "http://www.w3.org/1999/xlink"
11 // an object to define the properties and text content of the text element
12
13 // a function to create a text element
14 function drawText(parent, props, textcontent) {
15 // create a new text element
16 let text = document.createElementNS(SVG_NS, "text");
17 //set the attributes for the text
18 for (var name in props) {
19 if (props.hasOwnProperty(name)) {
20 text.setAttributeNS(null, name, props[name]);
21 }
22 }
23 text.textContent = textcontent;
24 parent.appendChild(text);
25 return text;
26 }
27
28 //Create arrow
29 function createArrow(parent, startX, startY, endX, endY, color = "black") {
30 // create a new line element
31 let line = document.createElementNS(SVG_NS, "line");
32 // set attributes for the line (start and end points, stroke color)
33 line.setAttributeNS(null, "x1", startX);
34 line.setAttributeNS(null, "y1", startY);
35 line.setAttributeNS(null, "x2", endX);
36 line.setAttributeNS(null, "y2", endY);
37 line.setAttributeNS(null, "stroke", color);
38 line.setAttributeNS(null, "stroke-width", 2);
39
40 // append the line to the parent SVG element
41 parent.appendChild(line);
42 return line;
43 }
44
45 // a function to create an image
46 function drawImage(parent, props, url) {
47 // create a new text element
48 let img = document.createElementNS(SVG_NS, "image");
49 //set the attributes for the text
50 for (var name in props) {
51 if (props.hasOwnProperty(name)) {
52 img.setAttributeNS(null, name, props[name]);
53 }
54 }
55 img.setAttributeNS(SVG_XLINK, 'xlink:href', url);
56 //text.textContent = textcontent;
57 parent.appendChild(img);
58 return img;
59 }
60
61
62 /*----- BINARY VALUE -------*/
63
64 Blockly.Blocks['binary_value'] = {
65 init: function() {
66 this.appendDummyInput()
67 .setAlign(Blockly.ALIGN_LEFT)
68 .appendField("BINARY NUMBER");
69 this.appendDummyInput()
70 .appendField("7")
71 .appendField(new Blockly.FieldCheckbox("FALSE",this.bitset.bind(this)), "B7")
72 .appendField(new Blockly.FieldCheckbox("FALSE",this.bitset.bind(this)), "B6")
73 .appendField(new Blockly.FieldCheckbox("FALSE",this.bitset.bind(this)), "B5")
74 .appendField(new Blockly.FieldCheckbox("FALSE",this.bitset.bind(this)), "B4")
75 .appendField(new Blockly.FieldCheckbox("FALSE",this.bitset.bind(this)), "B3")
76 .appendField(new Blockly.FieldCheckbox("FALSE",this.bitset.bind(this)), "B2")
77 .appendField(new Blockly.FieldCheckbox("FALSE",this.bitset.bind(this)), "B1")
78 .appendField(new Blockly.FieldCheckbox("FALSE",this.bitset.bind(this)), "B0")
79 .appendField("0");
80
81 this.setOutput(true, "Number");
82 this.setInputsInline(false);
83 this.setColour(230);
84
85 props= {
86 x: 145,
87 y: 15,
88 "fill": "lightgreen",
89 "dominant-baseline": "middle",
90 "text-anchor": "left",
91 };
92
93 this.txt = drawText(this.getSvgRoot(),props,":");
94
95
96 this.setTooltip("get binary value");
97 this.setHelpUrl("");
98 },
99 bitset: function(val) {
100 setTimeout(() => {
101
102 var dropdown_channel = this.getFieldValue('REGISTER');
103 var value=0;
104 if( this.getFieldValue('B0') == "TRUE" )value|=1;
105 if( this.getFieldValue('B1') == "TRUE" )value|=2;
106 if( this.getFieldValue('B2') == "TRUE" )value|=4;
107 if( this.getFieldValue('B3') == "TRUE" )value|=8;
108 if( this.getFieldValue('B4') == "TRUE" )value|=16;
109 if( this.getFieldValue('B5') == "TRUE" )value|=32;
110 if( this.getFieldValue('B6') == "TRUE" )value|=64;
111 if( this.getFieldValue('B7') == "TRUE" )value|=128;
112
113 this.txt.textContent = '='+value+", 0x"+parseInt(value, 10).toString(16).toUpperCase();//+'[0b'+Number(value).toString(2)+']';
114
115 }, 100);
116
117 },
118
119
120 };
121
122
123 Blockly.JavaScript['binary_value'] = function(block) {
124 var dropdown_channel = block.getFieldValue('REGISTER');
125 var value=0;
126 if( block.getFieldValue('B0') == "TRUE" )value|=1;
127 if( block.getFieldValue('B1') == "TRUE" )value|=2;
128 if( block.getFieldValue('B2') == "TRUE" )value|=4;
129 if( block.getFieldValue('B3') == "TRUE" )value|=8;
130 if( block.getFieldValue('B4') == "TRUE" )value|=16;
131 if( block.getFieldValue('B5') == "TRUE" )value|=32;
132 if( block.getFieldValue('B6') == "TRUE" )value|=64;
133 if( block.getFieldValue('B7') == "TRUE" )value|=128;
134
135 var code = value;
136 return [code,Blockly.JavaScript.ORDER_NONE];
137
138 };
139
140
141 Blockly.Python['binary_value'] = function(block) {
142 var dropdown_channel = block.getFieldValue('REGISTER');
143 var value=0;
144 if( block.getFieldValue('B0') == "TRUE" )value|=1;
145 if( block.getFieldValue('B1') == "TRUE" )value|=2;
146 if( block.getFieldValue('B2') == "TRUE" )value|=4;
147 if( block.getFieldValue('B3') == "TRUE" )value|=8;
148 if( block.getFieldValue('B4') == "TRUE" )value|=16;
149 if( block.getFieldValue('B5') == "TRUE" )value|=32;
150 if( block.getFieldValue('B6') == "TRUE" )value|=64;
151 if( block.getFieldValue('B7') == "TRUE" )value|=128;
152 //this.txt.textContent="Value:"+value+", 0x"+parseInt(value, 10).toString(16).toUpperCase();
153
154 var code = value;
155 return [code,Blockly.JavaScript.ORDER_NONE];
156
157 };
158
159 /*---------- REGISTER VALUE----------------*/
160
161 /*---------------------- SET REG---------------*/
162
163 Blockly.Blocks['reg'] = {
164 init: function() {
165 this.appendDummyInput()
166 .appendField(new Blockly.FieldDropdown([["DDRA","DDRA"],["DDRB","DDRB"], ["DDRC","DDRC"], ["DDRD","DDRD"],
167 ["PORTA","PORTA"],["PORTB","PORTB"],["PORTC","PORTC"],["PORTD","PORTD"],
168 ["PINA","PINA"],["PINB","PINB"],["PINC","PINC"],["PIND","PIND"],
169 ["ADCSRA","ADCSRA"],["ADMUX","ADMUX"],["ADCH","ADCH"],["ADCL","ADCL"],["TCNT0","TCNT0"]
170
171 ]), "REGISTER");
172 this.setInputsInline(false);
173 this.setOutput(true);
174 this.setColour(230);
175 this.setTooltip("Get a register");
176 this.setHelpUrl("");
177 }
178 };
179
180
181 Blockly.JavaScript['reg'] = function(block) {
182 var dropdown_channel = block.getFieldValue('REGISTER');
183 return [dropdown_channel,Blockly.JavaScript.ORDER_NONE];
184 };
185
186
187 /*---------------------- STEPDIR---------------*/
188
189 Blockly.Blocks['direction'] = {
190 init: function() {
191 this.appendDummyInput()
192 .appendField(new Blockly.FieldDropdown([["Clockwise","CW"],["Counter Clockwise","CCW"]
193
194 ]), "DIR");
195 this.setInputsInline(false);
196 this.setOutput(true);
197 this.setColour(230);
198 this.setTooltip("Get a register");
199 this.setHelpUrl("");
200 }
201 };
202
203
204 Blockly.JavaScript['direction'] = function(block) {
205 var dropdown_channel = block.getFieldValue('DIR');
206 if( this.getFieldValue('DIR') == "CW" )
207 return [1,Blockly.JavaScript.ORDER_NONE]; // clockwise is 1
208 else
209 return [0,Blockly.JavaScript.ORDER_NONE]; // clockwise is 1
210
211 };
212
213
214 Blockly.Python['direction'] = function(block) {
215 var dropdown_channel = block.getFieldValue('REGISTER');
216 return [dropdown_channel,Blockly.Python.ORDER_NONE];
217 };
218
219
220
221 /* --------------- Get Voltage ------------ */
222
223
224
225 Blockly.Blocks['get_voltage'] = {
226 init: function() {
227 this.appendDummyInput()
228 .appendField("Read ADC")
229 .appendField(new Blockly.FieldDropdown([["A0","0"], ["A1","1"], ["A2","2"], ["A3","3"], ["A4","4"], ["A5","5"], ["A6","6"], ["A7","7"]]), "CHANNEL");
230 this.setOutput(true, "Number");
231 this.setColour(330);
232
233 this.setTooltip("Read Voltage from selected channel");
234 this.setHelpUrl("");
235 }
236 };
237
238
239 Blockly.JavaScript['get_voltage'] = function(block) {
240 var dropdown_channel = block.getFieldValue('CHANNEL');
241 var code = "get_voltage("+dropdown_channel+")";
242 return [code,Blockly.JavaScript.ORDER_NONE];
243 };
244
245 Blockly.Python['get_voltage'] = function(block) {
246 var dropdown_channel = block.getFieldValue('CHANNEL');
247 // TODO: Assemble Python into code variable.
248 var code = 'readAdc('+dropdown_channel+')';
249 return [code,Blockly.JavaScript.ORDER_NONE];
250 };
251
252
253 /* --------------- Get reg ------------ */
254
255
256
257 Blockly.Blocks['get_reg'] = {
258 init: function() {
259 this.appendDummyInput()
260 .appendField("READ REGISTER")
261 .appendField(new Blockly.FieldDropdown([["PINA","PINA"],["PINB","PINB"], ["PINC","PINC"], ["PIND","PIND"],["PORTA","PORTA"],["PORTB","PORTB"],["PORTC","PORTC"],["PORTD","PORTD"]]), "REGISTER");
262 this.setOutput(true, "Number");
263 this.setColour(330);
264 this.setTooltip("Read Voltage from selected channel");
265 this.setHelpUrl("");
266 }
267 };
268
269
270 Blockly.JavaScript['get_reg'] = function(block) {
271 var dropdown_channel = block.getFieldValue('REGISTER');
272 var code = "get_reg('"+dropdown_channel+"')";
273 return [code,Blockly.JavaScript.ORDER_NONE];
274 };
275
276 Blockly.Python['get_reg'] = function(block) {
277 var dropdown_channel = block.getFieldValue('REGISTER');
278 // TODO: Assemble Python into code variable.
279 var code = 'getReg(\''+dropdown_channel+'\')';
280 return [code,Blockly.JavaScript.ORDER_NONE];
281 };
282
283
284 /*---------------------- SET REG---------------*/
285
286 Blockly.Blocks['set_reg'] = {
287 init: function() {
288 this.appendValueInput("VALUE")
289 .setCheck(null)
290 .setAlign(Blockly.ALIGN_RIGHT)
291 .appendField("SET REGISTER")
292 .appendField(new Blockly.FieldDropdown([["DDRA","DDRA"],["DDRB","DDRB"], ["DDRC","DDRC"], ["DDRD","DDRD"],["PORTA","PORTA"],["PORTB","PORTB"],["PORTC","PORTC"],["PORTD","PORTD"]]), "REGISTER");
293 this.setInputsInline(false);
294 this.setPreviousStatement(true, null);
295 this.setNextStatement(true, null);
296 this.setColour(230);
297 this.setTooltip("Set register to value");
298 this.setHelpUrl("");
299 }
300 };
301
302
303 Blockly.JavaScript['set_reg'] = function(block) {
304 var dropdown_channel = block.getFieldValue('REGISTER');
305 var value = Blockly.JavaScript.valueToCode(block, 'VALUE', Blockly.JavaScript.ORDER_NONE);
306 var code = 'set_reg(\''+dropdown_channel+'\','+value+');\n';
307 return code;
308 };
309
310
311 Blockly.Python['set_reg'] = function(block) {
312 var dropdown_channel = block.getFieldValue('REGISTER');
313 var value_voltage = Blockly.Python.valueToCode(block, 'VALUE', Blockly.Python.ORDER_NONE);
314 // TODO: Assemble Python into code variable.
315 var code = 'setReg(\''+dropdown_channel+'\','+value_voltage+')\n';
316 return code;
317 };
318
319
320 /*---------- STEPPER MOTORS ------------*/
321
322 Blockly.Blocks['init_stepper'] = {
323 init: function() {
324 this.appendDummyInput()
325 .appendField("Init Stepper Motor[ DDRB=255]");
326 this.appendDummyInput()
327 .appendField("S1 :PB0-3, S2:PB4-7 outputs.");
328 this.setInputsInline(false);
329 this.setPreviousStatement(true, null);
330 this.setNextStatement(true, null);
331 this.setColour(230);
332 this.setTooltip("init stepper");
333 this.setHelpUrl("");
334 }
335 };
336
337
338 Blockly.JavaScript['init_stepper'] = function(block) {
339 stepper_pos=[0,0];
340 cursteppos = [4,4];
341 //return '';
342 return 'set_reg(\'DDRB\','+255+');\n';
343 };
344
345
346 Blockly.Python['init_stepper'] = function(block) {
347 return 'set_reg(\'DDRB\','+255+')\n';
348 };
349
350
351
352 Blockly.Blocks['move_stepper'] = {
353 init: function() {
354 this.appendDummyInput()
355 .appendField("Move Stepper: ");
356 this.appendDummyInput()
357 .setAlign(Blockly.ALIGN_RIGHT)
358 .appendField(new Blockly.FieldDropdown([["PB0-3","B1"],["PB4-7","B2"]]), "PINS");
359 this.appendValueInput("STEPS")
360 .setCheck(null)
361 .setAlign(Blockly.ALIGN_RIGHT)
362 .appendField("STEPS: ");
363 this.appendValueInput("DELAY")
364 .setCheck(null)
365 .setAlign(Blockly.ALIGN_RIGHT)
366 .appendField("DELAY: ");
367 this.setInputsInline(false);
368 this.setPreviousStatement(true, null);
369 this.setNextStatement(true, null);
370 this.setColour(230);
371 this.setTooltip("move stepper motor");
372 this.setHelpUrl("");
373
374 props= {
375 x: 135,
376 y: 15,
377 "fill": "lightgreen",
378 "dominant-baseline": "middle",
379 "text-anchor": "middle",
380 };
381
382
383 this.txt = drawText(this.getSvgRoot(),props,":");
384
385 imgprops= {
386 x: 0,
387 y: 30,
388 "dominant-baseline": "middle",
389 "text-anchor": "middle",
390 width: '80px',
391 height: '80px'
392 };
393 this.imgbody = drawImage(this.getSvgRoot(),imgprops, "media/stepper_motor_body.png");
394 this.imgshaft = drawImage(this.getSvgRoot(),imgprops, "media/stepper_motor_shaft.png");
395
396
397
398 },
399 set_stepper_display: function(val, angle) {
400 this.txt.textContent = '['+val+']';
401 this.imgshaft.setAttributeNS(null, "transform", `rotate(${angle} ${41} ${71})`);
402 }
403
404 };
405
406
407 Blockly.JavaScript['move_stepper'] = function(block) {
408 var code = '';
409 var motor=1;
410
411
412 if( this.getFieldValue('PINS') == "B1" ){
413 motor = 0;
414 }
415
416 var steps = Blockly.JavaScript.valueToCode(block, 'STEPS', Blockly.JavaScript.ORDER_NONE);
417 var msdelay = Blockly.JavaScript.valueToCode(block, 'DELAY', Blockly.JavaScript.ORDER_NONE);
418 set_stepper_pos[motor].push(block);
419 if( steps>0 ) // clockwise
420 code = 'for(var i=0;i<'+steps+';i++){\nmove_stepper_cw('+motor+');\nsleep('+msdelay*.001+');\n}\n';
421 else
422 code = 'for(var i=0;i<'+steps*-1+';i++){\nmove_stepper_ccw('+motor+');\nsleep('+msdelay*.001+');\n}\n';
423 return code;
424 };
425
426
427 Blockly.Python['move_stepper'] = function(block) {
428 var code = '';
429 var steps = Blockly.JavaScript.valueToCode(block, 'STEPS', Blockly.JavaScript.ORDER_NONE);
430 if(steps>0 )
431 code = 'for a in range('+steps+'):move_stepper_cw('+motor+')\n';
432 else
433 code = 'for a in range('+steps*-1+'):move_stepper_ccw('+motor+')\n';
434 return code;
435 };
436
437
438 /*------------- MOVE STEPPERS --------------*/
439 Blockly.Blocks['move_steppers'] = {
440 init: function() {
441 this.appendDummyInput()
442 .appendField("Move Steppers: ")
443 .appendField(new Blockly.FieldImage("media/help.svg", 25, 25, "*", ()=>{showBlockHelp('move_steppers');},'SS'));
444
445 this.appendDummyInput()
446 .appendField("MOTOR PB0-3:");
447 this.appendValueInput("STEPS1")
448 .setCheck(null)
449 .setAlign(Blockly.ALIGN_RIGHT)
450 .appendField("STEPS: ");
451 this.appendDummyInput()
452 .appendField(" ");
453
454 this.appendDummyInput()
455 .appendField("MOTOR PB4-7:")
456 this.appendValueInput("STEPS2")
457 .setCheck(null)
458 .setAlign(Blockly.ALIGN_RIGHT)
459 .appendField("STEPS: ");
460 this.appendValueInput("DELAY")
461 .setCheck(null)
462 .setAlign(Blockly.ALIGN_RIGHT)
463 .appendField("DELAY/step: ");
464
465 this.setInputsInline(false);
466 this.setPreviousStatement(true, null);
467 this.setNextStatement(true, null);
468 this.setColour(230);
469 this.setTooltip("move stepper motor");
470 this.setHelpUrl("");
471
472 imgprops= {
473 x: 30,
474 y: 60,
475 "dominant-baseline": "middle",
476 "text-anchor": "middle",
477 width: '60px',
478 height: '60px'
479 };
480 this.imgbody1 = drawImage(this.getSvgRoot(),imgprops, "media/stepper_motor_body.png");
481 this.imgshaft1 = drawImage(this.getSvgRoot(),imgprops, "media/stepper_motor_shaft.png");
482 imgprops['y']=138;
483 this.imgbody2 = drawImage(this.getSvgRoot(),imgprops, "media/stepper_motor_body.png");
484 this.imgshaft2 = drawImage(this.getSvgRoot(),imgprops, "media/stepper_motor_shaft.png");
485
486
487 props= {
488 x: 140,
489 y: 42,
490 "fill": "magenta",
491 "dominant-baseline": "middle",
492 "text-anchor": "middle",
493 };
494
495 this.txt1 = drawText(this.getSvgRoot(),props,":");
496 props['y']=130;
497 this.txt2 = drawText(this.getSvgRoot(),props,":");
498
499
500 },
501 set_stepper_display: function(val1, val2) {
502 this.txt1.textContent = '['+parseInt(val1)+']';
503 this.txt2.textContent = '['+parseInt(val2)+']';
504
505 this.imgshaft1.setAttributeNS(null, "transform", `rotate(${val1} ${61} ${91})`);
506 this.imgshaft2.setAttributeNS(null, "transform", `rotate(${val2} ${61} ${169})`);
507
508
509 },
510
511
512 };
513
514
515 Blockly.JavaScript['move_steppers'] = function(block) {
516 var code = '';
517 var motor=1;
518 var dir1=0, dir2=0;
519
520 var steps1 = Blockly.JavaScript.valueToCode(block, 'STEPS1', Blockly.JavaScript.ORDER_NONE);
521 var steps2 = Blockly.JavaScript.valueToCode(block, 'STEPS2', Blockly.JavaScript.ORDER_NONE);
522 var msdelay = Blockly.JavaScript.valueToCode(block, 'DELAY', Blockly.JavaScript.ORDER_NONE);
523
524 set_stepper_pos[0].push(block);
525 code = 'move_stepper('+steps1+','+steps2+','+msdelay+');\nsleep('+msdelay*.001+');\n';
526 return code;
527 };
528
529
530 Blockly.Python['move_steppers'] = function(block) {
531 var code = '';
532 var motor=1;
533 var dir1=0, dir2=0;
534
535 var steps1 = Blockly.JavaScript.valueToCode(block, 'STEPS1', Blockly.JavaScript.ORDER_NONE);
536 var steps2 = Blockly.JavaScript.valueToCode(block, 'STEPS2', Blockly.JavaScript.ORDER_NONE);
537 var msdelay = Blockly.JavaScript.valueToCode(block, 'DELAY', Blockly.JavaScript.ORDER_NONE);
538
539 code = 'move_stepper('+steps1+','+steps2+','+msdelay+')\nsleep('+msdelay*.001+')\n';
540 return code;
541 };
542
543
544
545
546 /* ---------- set_pwm-------------*/
547
548 Blockly.Blocks['set_pwm'] = {
549 init: function() {
550 this.appendValueInput("VALUE")
551 .setCheck(null)
552 .appendField("Set Brightness of")
553 .appendField(new Blockly.FieldDropdown([["RED LED","RED"], ["GREEN LED","GREEN"], ["BLUE LED","BLUE"]]), "NAME");
554 this.setPreviousStatement(true, null);
555 this.setNextStatement(true, null);
556 this.setColour(0);
557 this.setTooltip("");
558 this.setHelpUrl("");
559 }
560 };
561
562
563 Blockly.JavaScript['set_pwm'] = function(block) {
564 var dropdown_name = block.getFieldValue('NAME');
565 var value = Blockly.JavaScript.valueToCode(block, 'VALUE', Blockly.JavaScript.ORDER_ATOMIC);
566 var code = 'set_pwm(\''+dropdown_name+'\','+value+');\n';
567 return code;
568 };
569
570 Blockly.Python['set_pwm'] = function(block) {
571 var dropdown_name = block.getFieldValue('NAME');
572 var value = Blockly.Python.valueToCode(block, 'VALUE', Blockly.Python.ORDER_ATOMIC);
573 var code = 'set_pwm(\''+dropdown_name+'\','+value+')\n';
574 return code;
575 };
576
577 /*---------------------- SET REG FROM STRING---------------*/
578
579 Blockly.Blocks['set_reg_from_string'] = {
580 init: function() {
581 this.appendValueInput("REGISTER")
582 .appendField("Set Register");
583 this.appendValueInput("VALUE")
584 .appendField(", to:")
585 .setCheck(null);
586 this.setInputsInline(false);
587 this.setPreviousStatement(true, null);
588 this.setNextStatement(true, null);
589 this.setColour(230);
590 this.setTooltip("");
591 this.setHelpUrl("");
592 }
593 };
594
595 Blockly.JavaScript['set_reg_from_string'] = function(block) {
596 //var dropdown_channel = block.getFieldValue('REGISTER');
597 var reg = Blockly.JavaScript.valueToCode(block, 'REGISTER', Blockly.JavaScript.ORDER_NONE);
598 var value = Blockly.JavaScript.valueToCode(block, 'VALUE', Blockly.JavaScript.ORDER_NONE);
599 if(reg.startsWith('\'') || reg.startsWith('\"') ) // String has quotes.
600 var code = 'set_reg('+reg+','+value+');\n';
601 else // add quotes
602 var code = 'set_reg(\''+reg+'\','+value+');\n';
603 return code;
604 };
605
606
607 Blockly.Python['set_reg_from_string'] = function(block) {
608 var reg = Blockly.Python.valueToCode(block, 'REGISTER', Blockly.Python.ORDER_NONE);
609 var value = Blockly.Python.valueToCode(block, 'VALUE', Blockly.Python.ORDER_NONE);
610 if(reg.startsWith('\'') || reg.startsWith('\"') ) // String has quotes.
611 var code = 'set_reg('+reg+','+value+')\n';
612 else // add quotes
613 var code = 'set_reg(\''+reg+'\','+value+')\n';
614 return code;
615 };
616
617
618 /*---------------------- GET REG FROM STRING---------------*/
619
620 Blockly.Blocks['get_reg_from_string'] = {
621 init: function() {
622 this.appendDummyInput()
623 .appendField("Read Register")
624 .appendField(new Blockly.FieldTextInput(""), "REGISTER")
625 this.setInputsInline(false);
626 this.setColour(230);
627 this.setOutput(true, "Number");
628 this.setTooltip("");
629 this.setHelpUrl("");
630 }
631 };
632
633 Blockly.JavaScript['get_reg_from_string'] = function(block) {
634 var dropdown_channel = block.getFieldValue('REGISTER');
635 var code = 'get_reg(\''+dropdown_channel+'\')';
636 return [code,Blockly.JavaScript.ORDER_NONE];
637 };
638
639
640 Blockly.Python['get_reg_from_string'] = function(block) {
641 var dropdown_channel = block.getFieldValue('REGISTER');
642 // TODO: Assemble Python into code variable.
643 var code = 'get_reg(\''+dropdown_channel+'\')';
644 return [code,Blockly.Python.ORDER_NONE];
645 };
646
647 /* ---------- Bytes to Integer -------------*/
648
649 Blockly.Blocks['bytes_to_int'] = {
650 init: function() {
651 this.appendDummyInput()
652 .setAlign(Blockly.ALIGN_RIGHT)
653 .appendField("Bytes to Int");
654 this.appendValueInput("HIGH")
655 .setAlign(Blockly.ALIGN_RIGHT)
656 .setCheck(null)
657 .appendField("MSB (");
658 this.appendDummyInput()
659 .setAlign(Blockly.ALIGN_RIGHT)
660 .appendField("<<8)");
661 this.appendValueInput("LOW")
662 .setAlign(Blockly.ALIGN_RIGHT)
663 .setCheck(null)
664 .appendField("LSB | (");
665 this.appendDummyInput()
666 .setAlign(Blockly.ALIGN_RIGHT)
667 .appendField(")");
668 this.setInputsInline(false);
669 this.setOutput(true, null);
670 this.setColour(135);
671 this.setTooltip("");
672 this.setHelpUrl("");
673 }
674 };
675
676 Blockly.JavaScript['bytes_to_int'] = function(block) {
677 var value_low = Blockly.JavaScript.valueToCode(block, 'LOW', Blockly.JavaScript.ORDER_NONE);
678 var value_high = Blockly.JavaScript.valueToCode(block, 'HIGH', Blockly.JavaScript.ORDER_NONE);
679 var code = '('+value_low+')|('+value_high+'<<8)';
680 return [code, Blockly.JavaScript.ORDER_NONE];
681 };
682
683 Blockly.Python['bytes_to_int'] = function(block) {
684 var value_low = Blockly.Python.valueToCode(block, 'LOW', Blockly.Python.ORDER_NONE);
685 var value_high = Blockly.Python.valueToCode(block, 'HIGH', Blockly.Python.ORDER_NONE);
686 var code = '('+value_low+')|('+value_high+'<<8)';
687 return [code, Blockly.Python.ORDER_NONE];
688 };
689
690 /* ---------- Shift bits -------------*/
691
692 Blockly.Blocks['shift_bits'] = {
693 init: function() {
694 this.appendValueInput("VALUE")
695 .setCheck(null)
696 .setAlign(Blockly.ALIGN_LEFT)
697 .appendField("Shift bit:");
698 this.appendDummyInput()
699 .appendField(new Blockly.FieldDropdown([["<<","<<"], [">>",">>"]]), "DIRECTION")
700 this.appendValueInput("BITS")
701 .setCheck(null)
702 .setAlign(Blockly.ALIGN_LEFT)
703 this.setInputsInline(true);
704 this.setOutput(true, null);
705 this.setColour(135);
706 this.setTooltip("");
707 this.setHelpUrl("");
708 }
709 };
710
711 Blockly.JavaScript['shift_bits'] = function(block) {
712 var direction = block.getFieldValue('DIRECTION');
713 var bits = Blockly.JavaScript.valueToCode(block, 'BITS', Blockly.JavaScript.ORDER_NONE);
714 var value = Blockly.JavaScript.valueToCode(block, 'VALUE', Blockly.JavaScript.ORDER_NONE);
715 code = value+direction+bits;
716 return [code, Blockly.JavaScript.ORDER_NONE];
717 };
718
719 Blockly.Python['shift_bits'] = function(block) {
720 var direction = block.getFieldValue('DIRECTION');
721 var bits = Blockly.JavaScript.valueToCode(block, 'BITS', Blockly.JavaScript.ORDER_NONE);
722 var value = Blockly.JavaScript.valueToCode(block, 'VALUE', Blockly.JavaScript.ORDER_NONE);
723 code = value+direction+bits;
724 return [code, Blockly.Python.ORDER_NONE];
725 };
726
727
728 /* ---------- clear bit -------------*/
729
730 Blockly.Blocks['clear_bit'] = {
731 init: function() {
732 this.appendValueInput("VALUE")
733 .setCheck(null)
734 .setAlign(Blockly.ALIGN_LEFT)
735 .appendField("Set/Clear Bit:");
736 this.appendDummyInput()
737 .appendField(new Blockly.FieldDropdown([["&~(1<<","&~(1<<"], ["|(1<<","|(1<<"]]), "OPERATION")
738 .appendField(new Blockly.FieldDropdown([["0","0"], ["1","1"], ["2","2"], ["2","2"], ["3","3"], ["4","4"], ["5","5"], ["6","6"], ["7","7"]]), "BITS");
739 this.appendDummyInput()
740 .appendField(")");
741 this.setInputsInline(true);
742 this.setOutput(true, null);
743 this.setColour(135);
744 this.setTooltip("");
745 this.setHelpUrl("");
746 }
747 };
748
749 Blockly.JavaScript['clear_bit'] = function(block) {
750 var operation = block.getFieldValue('OPERATION');
751 var bits = block.getFieldValue('BITS');
752 var value = Blockly.JavaScript.valueToCode(block, 'VALUE', Blockly.JavaScript.ORDER_NONE);
753 code = value+operation+bits+")";
754 return [code, Blockly.JavaScript.ORDER_NONE];
755 };
756
757 Blockly.Python['clear_bit'] = function(block) {
758 var operation = block.getFieldValue('OPERATION');
759 var bits = block.getFieldValue('BITS');
760 var value = Blockly.Python.valueToCode(block, 'VALUE', Blockly.Python.ORDER_NONE);
761 code = value+operation+bits+")";
762 return [code, Blockly.Python.ORDER_NONE];
763 };
764
765 /* ---------- AND/OR bit -------------*/
766
767 Blockly.Blocks['and_or'] = {
768 init: function() {
769 this.appendValueInput("VALUE")
770 .setCheck(null)
771 .setAlign(Blockly.ALIGN_LEFT)
772 this.appendDummyInput()
773 .appendField(new Blockly.FieldDropdown([["&","&"], ["|","|"]]), "OPERATION")
774 this.appendValueInput("VALUE2")
775 .setCheck(null)
776 .setAlign(Blockly.ALIGN_LEFT)
777 this.setInputsInline(true);
778 this.setOutput(true, null);
779 this.setColour(135);
780 this.setTooltip("");
781 this.setHelpUrl("");
782 }
783 };
784
785 Blockly.JavaScript['and_or'] = function(block) {
786 var operation = block.getFieldValue('OPERATION');
787 var value = Blockly.JavaScript.valueToCode(block, 'VALUE', Blockly.JavaScript.ORDER_NONE);
788 var value2 = Blockly.JavaScript.valueToCode(block, 'VALUE2', Blockly.JavaScript.ORDER_NONE);
789 code = value+operation+value2;
790 return [code, Blockly.JavaScript.ORDER_NONE];
791 };
792
793 Blockly.Python['and_or'] = function(block) {
794 var operation = block.getFieldValue('OPERATION');
795 var value = Blockly.Python.valueToCode(block, 'VALUE', Blockly.Python.ORDER_NONE);
796 var value2 = Blockly.JavaScript.valueToCode(block, 'VALUE2', Blockly.JavaScript.ORDER_NONE);
797 code = value+operation+value2;
798 return [code, Blockly.Python.ORDER_NONE];
799 };
800
801
802 /*---------------------- SET REG FROM STRING---------------*/
803
804 Blockly.Blocks['set_dac'] = {
805 init: function() {
806 this.appendDummyInput()
807 .appendField("Set DAC on PORTC ");
808 this.appendValueInput("VALUE")
809 .setCheck(null)
810 .appendField(", to:");
811 this.setInputsInline(true);
812 this.setPreviousStatement(true, null);
813 this.setNextStatement(true, null);
814 this.setColour(230);
815 this.setTooltip("");
816 this.setHelpUrl("");
817 }
818 };
819
820 Blockly.JavaScript['set_dac'] = function(block) {
821 var value = Blockly.JavaScript.valueToCode(block, 'VALUE', Blockly.JavaScript.ORDER_NONE);
822 var code = "set_reg('DDRC',255);\nset_reg('PORTC',"+value+");\n";
823 return code;
824 };
825
826
827 Blockly.Python['set_dac'] = function(block) {
828 var value = Blockly.Python.valueToCode(block, 'VALUE', Blockly.Python.ORDER_NONE);
829 var code = "set_reg('DDRC',255)\nset_reg('PORTC',"+value+")\n";
830 return code;
831 };
832
833
834 /*---------------------- SET REG BITS FROM STRING---------------*/
835
836 Blockly.Blocks['set_reg_bits_string'] = {
837 init: function() {
838 this.appendDummyInput()
839 .setAlign(Blockly.ALIGN_LEFT)
840 .appendField("SET BITS")
841 .appendField(new Blockly.FieldTextInput(""), "REGISTER");
842 this.appendDummyInput()
843 .appendField("7")
844 .appendField(new Blockly.FieldCheckbox("FALSE",this.bitset.bind(this)), "B7")
845 .appendField(new Blockly.FieldCheckbox("FALSE",this.bitset.bind(this)), "B6")
846 .appendField(new Blockly.FieldCheckbox("FALSE",this.bitset.bind(this)), "B5")
847 .appendField(new Blockly.FieldCheckbox("FALSE",this.bitset.bind(this)), "B4")
848 .appendField(new Blockly.FieldCheckbox("FALSE",this.bitset.bind(this)), "B3")
849 .appendField(new Blockly.FieldCheckbox("FALSE",this.bitset.bind(this)), "B2")
850 .appendField(new Blockly.FieldCheckbox("FALSE",this.bitset.bind(this)), "B1")
851 .appendField(new Blockly.FieldCheckbox("FALSE",this.bitset.bind(this)), "B0")
852 .appendField("0");
853
854 this.setInputsInline(false);
855 this.setPreviousStatement(true, null);
856 this.setNextStatement(true, null);
857 this.setColour(230);
858
859 props= {
860 x: 145,
861 y: 15,
862 "fill": "lightgreen",
863 "dominant-baseline": "middle",
864 "text-anchor": "left",
865 };
866
867 this.txt = drawText(this.getSvgRoot(),props,":");
868
869
870 this.setTooltip("Set register to value");
871 this.setHelpUrl("");
872 },
873 bitset: function(val) {
874 setTimeout(() => {
875
876 var dropdown_channel = this.getFieldValue('REGISTER');
877 var value=0;
878 if( this.getFieldValue('B0') == "TRUE" )value|=1;
879 if( this.getFieldValue('B1') == "TRUE" )value|=2;
880 if( this.getFieldValue('B2') == "TRUE" )value|=4;
881 if( this.getFieldValue('B3') == "TRUE" )value|=8;
882 if( this.getFieldValue('B4') == "TRUE" )value|=16;
883 if( this.getFieldValue('B5') == "TRUE" )value|=32;
884 if( this.getFieldValue('B6') == "TRUE" )value|=64;
885 if( this.getFieldValue('B7') == "TRUE" )value|=128;
886
887 this.txt.textContent = '='+value;//+'[0b'+Number(value).toString(2)+']';
888
889 }, 100);
890
891 },
892
893
894 };
895
896
897 Blockly.JavaScript['set_reg_bits_string'] = function(block) {
898 var dropdown_channel = block.getFieldValue('REGISTER');
899 var value=0;
900 if( block.getFieldValue('B0') == "TRUE" )value|=1;
901 if( block.getFieldValue('B1') == "TRUE" )value|=2;
902 if( block.getFieldValue('B2') == "TRUE" )value|=4;
903 if( block.getFieldValue('B3') == "TRUE" )value|=8;
904 if( block.getFieldValue('B4') == "TRUE" )value|=16;
905 if( block.getFieldValue('B5') == "TRUE" )value|=32;
906 if( block.getFieldValue('B6') == "TRUE" )value|=64;
907 if( block.getFieldValue('B7') == "TRUE" )value|=128;
908
909 var code = 'set_reg(\''+dropdown_channel+'\','+value+');\n';
910 return code;
911 };
912
913
914 Blockly.Python['set_reg_bits_string'] = function(block) {
915 var dropdown_channel = block.getFieldValue('REGISTER');
916 var value=0;
917 if( block.getFieldValue('B0') == "TRUE" )value|=1;
918 if( block.getFieldValue('B1') == "TRUE" )value|=2;
919 if( block.getFieldValue('B2') == "TRUE" )value|=4;
920 if( block.getFieldValue('B3') == "TRUE" )value|=8;
921 if( block.getFieldValue('B4') == "TRUE" )value|=16;
922 if( block.getFieldValue('B5') == "TRUE" )value|=32;
923 if( block.getFieldValue('B6') == "TRUE" )value|=64;
924 if( block.getFieldValue('B7') == "TRUE" )value|=128;
925 this.txt.textContent="Value:"+value+", 0x"+parseInt(value, 10).toString(16).toUpperCase();
926 var code = 'setReg(\''+dropdown_channel+'\','+value+')\n';
927 return code;
928 };
929
930 /*---------------------- SET REG BITS---------------*/
931
932 Blockly.Blocks['set_reg_bits'] = {
933 init: function() {
934 this.appendDummyInput()
935 .setAlign(Blockly.ALIGN_LEFT)
936 .appendField("SET BITS")
937 .appendField(new Blockly.FieldDropdown([["DDRA","DDRA"],["DDRB","DDRB"], ["DDRC","DDRC"], ["DDRD","DDRD"],["PORTA","PORTA"],["PORTB","PORTB"],["PORTC","PORTC"],["PORTD","PORTD"]]), "REGISTER");
938 this.appendDummyInput()
939 .appendField("7")
940 .appendField(new Blockly.FieldCheckbox("FALSE"), "B7")
941 .appendField(new Blockly.FieldCheckbox("FALSE"), "B6")
942 .appendField(new Blockly.FieldCheckbox("FALSE"), "B5")
943 .appendField(new Blockly.FieldCheckbox("FALSE"), "B4")
944 .appendField(new Blockly.FieldCheckbox("FALSE"), "B3")
945 .appendField(new Blockly.FieldCheckbox("FALSE"), "B2")
946 .appendField(new Blockly.FieldCheckbox("FALSE"), "B1")
947 .appendField(new Blockly.FieldCheckbox("FALSE"), "B0")
948 .appendField("0");
949
950 this.setInputsInline(false);
951 this.setPreviousStatement(true, null);
952 this.setNextStatement(true, null);
953 this.setColour(230);
954
955 this.setTooltip("Set register to value");
956 this.setHelpUrl("");
957 }
958 };
959
960
961 Blockly.JavaScript['set_reg_bits'] = function(block) {
962 var dropdown_channel = block.getFieldValue('REGISTER');
963 var value=0;
964 if( block.getFieldValue('B0') == "TRUE" )value|=1;
965 if( block.getFieldValue('B1') == "TRUE" )value|=2;
966 if( block.getFieldValue('B2') == "TRUE" )value|=4;
967 if( block.getFieldValue('B3') == "TRUE" )value|=8;
968 if( block.getFieldValue('B4') == "TRUE" )value|=16;
969 if( block.getFieldValue('B5') == "TRUE" )value|=32;
970 if( block.getFieldValue('B6') == "TRUE" )value|=64;
971 if( block.getFieldValue('B7') == "TRUE" )value|=128;
972
973 var code = 'set_reg(\''+dropdown_channel+'\','+value+');\n';
974 return code;
975 };
976
977
978 Blockly.Python['set_reg_bits'] = function(block) {
979 var dropdown_channel = block.getFieldValue('REGISTER');
980 var value=0;
981 if( block.getFieldValue('B0') == "TRUE" )value|=1;
982 if( block.getFieldValue('B1') == "TRUE" )value|=2;
983 if( block.getFieldValue('B2') == "TRUE" )value|=4;
984 if( block.getFieldValue('B3') == "TRUE" )value|=8;
985 if( block.getFieldValue('B4') == "TRUE" )value|=16;
986 if( block.getFieldValue('B5') == "TRUE" )value|=32;
987 if( block.getFieldValue('B6') == "TRUE" )value|=64;
988 if( block.getFieldValue('B7') == "TRUE" )value|=128;
989 var code = 'setReg(\''+dropdown_channel+'\','+value+')\n';
990 return code;
991 };
992
993
994
995
996
997
998 //Flask Endpoints
999
1000
1001
1002 //-------------------- API ------------------------
1003
1004
1005
1006 function initKuttyPy(interpreter, scope) {
1007
1008 // kuttypy API CALLS
1009 interpreter.setProperty(scope, 'get_voltage', interpreter.createAsyncFunction(
1010 function(channel, callback) {
1011 fetch(`/get_voltage/${channel}`)
1012 .then(response => response.json())
1013 .then(data => {
1014 callback(data.voltage);
1015 })
1016 .catch(error => {
1017 console.error('Error fetching voltage:', error);
1018 callback(0);
1019 });
1020 }
1021 ));
1022
1023
1024 var getRegWrapper = interpreter.createAsyncFunction(function(channel, callback) {
1025 fetch(`/get_reg/${channel}`)
1026 .then(response => response.json())
1027 .then(data => {
1028 showReg(channel,data.data);
1029 callback(data.data); // Pass the register value to Blockly
1030 })
1031 .catch(error => {
1032 showReg(channel,'err');
1033 console.error('Error fetching register value:', error);
1034 callback(0); // Return 0 if there's an error
1035 });
1036 });
1037
1038 interpreter.setProperty(scope, 'get_reg', getRegWrapper);
1039 interpreter.setProperty(scope, 'getReg', getRegWrapper);
1040
1041 var setRegWrapper = interpreter.createAsyncFunction(function(channel, value, callback) {
1042 fetch('/set_reg', {
1043 method: 'POST',
1044 headers: {
1045 'Content-Type': 'application/json'
1046 },
1047 body: JSON.stringify({
1048 reg: channel,
1049 data: value
1050 })
1051 })
1052 .then(response => response.json())
1053 .then(data => {
1054 showReg(channel,value);
1055 callback(); // Just call the callback once the register is set
1056 })
1057 .catch(error => {
1058 showReg(channel,'err');
1059 console.error('Error setting register value:', error);
1060 callback();
1061 });
1062 });
1063
1064 interpreter.setProperty(scope, 'set_reg', setRegWrapper);
1065 interpreter.setProperty(scope, 'setReg', setRegWrapper);
1066
1067
1068 // Add an API for stepper
1069 var wrapper = interpreter.createAsyncFunction(
1070 function(motor, callback) {
1071 stepper_pos[motor] += 1;
1072 cursteppos[motor] += 1;
1073 if (cursteppos[motor] > 3) cursteppos[motor] = 0;
1074
1075 fetch('/set_reg', {
1076 method: 'POST',
1077 headers: {
1078 'Content-Type': 'application/json'
1079 },
1080 body: JSON.stringify({
1081 reg: 'PORTB',
1082 data: step_positions[0][cursteppos[0]] + step_positions[1][cursteppos[1]]
1083 })
1084 })
1085 .then(response => response.json())
1086 .then(data => {
1087 for (var i = 0; i < set_stepper_pos[motor].length; i++) {
1088 set_stepper_pos[motor][i].set_stepper_display(stepper_pos[motor], stepper_pos[motor]);
1089 }
1090 callback(); // Call the callback after updating the display
1091 })
1092 .catch(error => {
1093 callback();
1094 });
1095
1096
1097
1098 }
1099 );
1100 interpreter.setProperty(scope, 'move_stepper_cw', wrapper);
1101
1102 var wrapper = interpreter.createAsyncFunction(
1103 function(motor, callback) {
1104 stepper_pos[motor] -= 1;
1105 cursteppos[motor] -= 1;
1106 if (cursteppos[motor] < 0) cursteppos[motor] = 3;
1107
1108 fetch('/set_reg', {
1109 method: 'POST',
1110 headers: {
1111 'Content-Type': 'application/json'
1112 },
1113 body: JSON.stringify({
1114 reg: 'PORTB',
1115 data: step_positions[0][cursteppos[0]] + step_positions[1][cursteppos[1]]
1116 })
1117 })
1118 .then(response => response.json())
1119 .then(data => {
1120 for (var i = 0; i < set_stepper_pos[motor].length; i++) {
1121 set_stepper_pos[motor][i].set_stepper_display(stepper_pos[motor], stepper_pos[motor]);
1122 }
1123 callback(); // Call the callback after updating the display
1124 })
1125 .catch(error => {
1126 callback();
1127 });
1128
1129
1130
1131
1132
1133
1134 }
1135 );
1136 interpreter.setProperty(scope, 'move_stepper_ccw', wrapper);
1137
1138 var wrapper = interpreter.createAsyncFunction( // Move based on direction.
1139 function(steps1, steps2, msdelay, callback) {
1140 var longer_distance = 1. * Math.max(Math.abs(steps1), Math.abs(steps2));
1141 var d1 = steps1 / longer_distance, d2 = steps2 / longer_distance;
1142
1143 var moveSteps = (mystep) => {
1144 //Motor1
1145 stepper_pos[0] += d1;
1146 cursteppos[0] += d1;
1147 if (cursteppos[0] < 0) cursteppos[0] = 3;
1148 if (cursteppos[0] > 3) cursteppos[0] = 0;
1149
1150 //Motor2
1151 stepper_pos[1] += d2;
1152 cursteppos[1] += d2;
1153 if (cursteppos[1] < 0) cursteppos[1] = 3;
1154 if (cursteppos[1] > 3) cursteppos[1] = 0;
1155
1156
1157 fetch('/set_reg', {
1158 method: 'POST',
1159 headers: {
1160 'Content-Type': 'application/json'
1161 },
1162 body: JSON.stringify({
1163 reg: 'PORTB',
1164 data: step_positions[0][parseInt(cursteppos[0])] + step_positions[1][parseInt(cursteppos[1])]
1165 })
1166 })
1167 .then(response => response.json())
1168 .then(data => {
1169 for (var i = 0; i < set_stepper_pos[0].length; i++) {
1170 set_stepper_pos[0][i].set_stepper_display(stepper_pos[0], stepper_pos[1]);
1171 }
1172 callback(); // Call the callback after updating the display
1173 })
1174 .catch(error => {
1175 callback();
1176 });
1177
1178 if (mystep < longer_distance - 1) {
1179 setTimeout(() => moveSteps(mystep + 1), msdelay);
1180 } else {
1181 callback(); // Call the callback after all steps are done
1182 }
1183
1184
1185
1186 };
1187
1188 moveSteps(0); // Start moving steps
1189 }
1190 );
1191 interpreter.setProperty(scope, 'move_stepper', wrapper);
1192
1193
1194 }
1195
1196
1197
1198
1199
1200