Skip to content

Commit 201c266

Browse files
committed
reorg du code pour avoir des external imbriques
via un truc "a la" require.js
1 parent 04cc720 commit 201c266

File tree

12 files changed

+384
-354
lines changed

12 files changed

+384
-354
lines changed

Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ export PROJECT_DIR := $(dir $(realpath ${MAKEFILE_LIST}))
66
CORE_DIR := ${PROJECT_DIR}../ArduinoCore/
77

88
# other arduino librairies project pathes this project depends on
9-
export DEPENDENCIES := ${CORE_DIR} ${CORE_DIR}../ArduinoLibs/ ${CORE_DIR}../ArduinoTools/
9+
export DEPENDENCIES := ${CORE_DIR} ${CORE_DIR}../ArduinoLibs/
10+
# ${CORE_DIR}../ArduinoTools/
1011

1112
# generate assembler source code also
1213
export WITH_ASSEMBLY := yes
@@ -36,4 +37,4 @@ endif
3637
ifneq (${TARGET},)
3738
rm -rf ${PROJECT_DIR}target/${TARGET}
3839
endif
39-
40+

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ TODO :
99
=> A TESTER
1010
+ documenter comment la charger (appel d'un script manuel en attendant que
1111
le système d'extension soit ouvert)
12-
- 2' idem en mode socket+nodejs comme passe plat
12+
* 2' idem en mode socket+nodejs comme passe plat
1313
* 3 - ajouter un flag et consulter ce flag par un "?"
14-
- 3' + faire son pendant scratch
14+
* 3' + faire son pendant scratch
1515
* 4 - ajouter un bouton, qui envoie un caractère "!" quand on l'appuie une fois
16-
- 4' + faire l'évènement associé coté scratch
16+
* 4' + faire l'évènement associé coté scratch
1717

1818
voir ensuite quelles interactions seraient plus sympa à mettre en place ?
1919
- moteurs pas à pas ? tortue ? jeu de lumière ? guirlande ?
2020

2121
idéalement : le chargement d'une extension coté scratch déclenche l'appel
22-
à nodejs qui lance l'upload du code associé
22+
à nodejs qui lance l'upload du code associé ?

ArduinoScratch.ino renamed to basic.ino

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,48 @@
44
#endif
55

66
#define LED 13
7+
// PCMSK0:4 + Int PCI0
78
#define BUTTON 12
89

910
bool ledState = LOW;
10-
bool buttonState = HIGH;
11-
bool buttonLastChange = 0;
1211

1312
void setup(void) {
1413
pinMode(LED, OUTPUT);
1514
digitalWrite(LED, LOW);
1615

16+
// enable interrupt for PCINT4 = D12
17+
PCMSK0 |= 1 << 4;
18+
// enable interrupt for PCI0 (pcint0-7)
19+
PCICR |= 1;
20+
1721
// TODO : bind raising interrupt on this digital input
1822
pinMode(BUTTON, INPUT_PULLUP);
1923

2024
Serial.begin(DEFAULT_BAUDRATE);
2125
}
2226

23-
void loop() {
24-
// test button
27+
volatile bool mustSendButton = false;
28+
volatile bool buttonState = HIGH;
29+
#define BOUND_DELAY 100
30+
volatile int buttonLastChange = 0;
31+
32+
ISR(PCINT0_vect) {
2533
bool b = digitalRead(BUTTON);
26-
if (b != buttonState && millis() - buttonLastChange > 100) {
27-
//Serial.println(millis() - buttonLastChange);
34+
if (b != buttonState && millis() - buttonLastChange > BOUND_DELAY) {
2835
buttonLastChange = millis();
2936
buttonState = b;
3037
if (buttonState == LOW) {
31-
Serial.println('!');
38+
mustSendButton = true;
3239
}
3340
}
41+
}
42+
43+
void loop() {
44+
// test button
45+
if (mustSendButton) {
46+
Serial.println('!');
47+
mustSendButton = false;
48+
}
3449

3550
// test input
3651
int input = Serial.read();

extensions/basic.js

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
// basic example, see http://scratch.mit.edu/projects/26016195/#editor
2+
// and load this file in browser with :
3+
// ScratchExtensions.loadExternalJS("http://localhost:8000/extensions/basic.websock.js")
4+
// after having launched "node server.js"
5+
6+
var mode = /.*\?(\w*)$/.exec(MY_URL);
7+
if (!mode || mode[1] == "") {
8+
mode = "websock";
9+
} else {
10+
mode = mode[1];
11+
}
12+
var deps = {};
13+
deps[mode + "Gateway"] = "/extensions/lib/" + mode + "Gateway.js";
14+
15+
declareModule("basic", deps, function(moduleName) {
16+
17+
//make accessible from outside, for debug
18+
basicExtension = new (function() {
19+
var ext = this;
20+
21+
var gateway = new Gateway("basic");
22+
var status = {
23+
led: "off",
24+
buttonPressed: false
25+
};
26+
// make accessible from outside, for debug
27+
ext._myData = {
28+
status : status,
29+
gateway : gateway
30+
};
31+
32+
gateway.initialize(function () {
33+
console.info("gateway init");
34+
gateway.registerReceive(handleData);
35+
});
36+
37+
ext.getLed = function() {
38+
if (gateway.ready()) {
39+
return status.led;
40+
} else {
41+
return "???";
42+
}
43+
};
44+
45+
ext.setLed = function(what) {
46+
if (!gateway.ready()) {
47+
return;
48+
}
49+
status.led = what;
50+
gateway.send(what === "on" ? 1 : 0);
51+
};
52+
53+
ext.toggleLed = function() {
54+
ext.setLed(status.led === "on" ? "off" : "on");
55+
};
56+
57+
ext.onButton = function() {
58+
if (!gateway.ready()) {
59+
return false;;
60+
}
61+
62+
if (status.buttonPressed === true) {
63+
status.buttonPressed = false;
64+
return true;
65+
} else {
66+
return false;
67+
}
68+
};
69+
70+
// serial dialog methods
71+
function handleData(rawData) {
72+
for(i = 0; i < rawData.length; i++) {
73+
switch (rawData[i]) {
74+
case '\n':
75+
case '\r':
76+
break;
77+
case '.':
78+
// ping => ok
79+
clearWatchDog();
80+
break;
81+
case '!':
82+
status.buttonPressed = true;
83+
break;
84+
case '0':
85+
status.led = "off";
86+
break;
87+
case '1':
88+
status.led = "on";
89+
break;
90+
default:
91+
console.log("bad char '" + rawData[i] + "' ?");
92+
}
93+
}
94+
}
95+
96+
// Status reporting code
97+
// Use this to report missing hardware, plugin or unsupported browser
98+
// Value / status light Color / Meaning
99+
// 0 red error
100+
// 1 yellow not ready
101+
// 2 green ready
102+
ext._getStatus = function() {
103+
if(gateway.ready()) {
104+
return {status: 2, msg: 'Ready'};
105+
} else {
106+
return {status: 1, msg: 'Not ready'};
107+
}
108+
}
109+
110+
// Block and block menu descriptions
111+
var descriptor = {
112+
// each block begins by a Op Code :
113+
// ' ' (space) Synchronous command
114+
// 'w' Asynchronous command
115+
// 'r' Synchronous reporter
116+
// 'R' Asynchronous reporter
117+
// 'h' Hat block (synchronous, returns boolean, true = run stack)
118+
// then, a description with arguments marked by %... : %n for number, %s for string, and %m.menuName for menu
119+
blocks: [
120+
['', 'set led %m.led', 'setLed', 'on'],
121+
['', 'toggle led state', 'toggleLed'],
122+
['r', 'led state', 'getLed'],
123+
['h', 'when button is pressed', 'onButton'],
124+
],
125+
menus: {
126+
led: ['on', 'off']
127+
}
128+
};
129+
130+
// Register the extension
131+
ScratchExtensions.unregister('My first arduino extension');
132+
ScratchExtensions.register('My first arduino extension', descriptor, ext);
133+
})();
134+
moduleExport(basicExtension, "basicExtension");
135+
});

extensions/basic.mock.js

Lines changed: 0 additions & 160 deletions
This file was deleted.

0 commit comments

Comments
 (0)