-
Notifications
You must be signed in to change notification settings - Fork 0
/
code_barres_v4.aesl
218 lines (179 loc) · 3.53 KB
/
code_barres_v4.aesl
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
<!DOCTYPE aesl-source>
<network>
<!--list of global events-->
<event size="3" name="chunk"/>
<event size="6" name="result"/>
<event size="2" name="rnd"/>
<event size="1" name="scan"/>
<constant value="6" name="bits"/>
<constant value="75" name="speed"/>
<!--node -->
<node name=""><![CDATA[#State:
# 0: idle
# 1: black calibrated
# 2: white calibrated
# 3: calculating length
# 4: reading
var state = 0
var i = 0
var j
var delta
var black = 0
var white = 0
var avg
var col # -1: undefined / 0: white / 1: black
var last_col = 0
var length
var length2
var code[bits]
var flash_col[3]
var cur_col
var to_round
var rounded
sub round
rounded = to_round/10
emit rnd [to_round, rounded]
if to_round%10 >=5 then
rounded++
end
sub update_state
if state == 0 then
call leds.circle(0,0,0,0,0,0,0,0)
elseif state == 1 then
call leds.circle(1,0,0,0,0,0,0,0)
elseif state == 2 then
call leds.circle(0,1,0,0,0,0,0,0)
elseif state == 3 then
call leds.circle(0,0,1,0,0,0,0,0)
elseif state == 4 then
call leds.circle(0,0,0,1,0,0,0,0)
elseif state == 5 then
call leds.circle(0,0,0,0,1,0,0,0)
elseif state == 6 then
call leds.circle(0,0,0,0,0,1,0,0)
elseif state == 7 then
call leds.circle(0,0,0,0,0,0,1,0)
elseif state == 8 then
call leds.circle(0,0,0,0,0,0,0,1)
end
if col == -1 then
call leds.top(25,25,10)
elseif col == 0 then
call leds.top(0,31,0)
elseif col == 1 then
call leds.top(31,0,0)
end
sub get_avg
avg = (prox.ground.delta[0]+prox.ground.delta[1])/2
callsub get_col
sub get_col
if black == 0 or white == 0 then
col = -1
else
col = 10*(avg-black)/(white-black)*10 #10 *... *10 to remain in the range -32768 to 32767
if col < 40 then
col = 1
elseif col > 70 then
col = 0
else
col = -1
end
end
if col != -1 then
last_col = col
end
emit scan col
sub flash
call leds.bottom.left(flash_col[0],flash_col[1],flash_col[2])
call leds.bottom.right(flash_col[0],flash_col[1],flash_col[2])
timer.period[1] = 200
sub unflash
call leds.bottom.left(0,0,0)
call leds.bottom.right(0,0,0)
timer.period[1] = 0
onevent buttons
callsub get_avg
when button.center == 1 do
if state == 0 then
state = 1
black = avg
elseif state == 1 then
state = 2
white = avg
else
motor.left.target = 0
motor.right.target = 0
end
end
when button.forward == 1 do
if state == 2 then
motor.left.target = speed
motor.right.target = speed
end
end
callsub update_state
onevent motor
if state == 3 then
length++
elseif state == 4 then
length2++
end
onevent prox
callsub get_avg
if state == 2 then
if col == 1 then
state = 3
length = 0
end
elseif state == 3 then
if col == 0 then
delta = 1500/speed * length
#length = length
i = -1
state = 4
cur_col = 0
length2 = 0
#timer.period[0] = delta+delta/2
end
elseif state == 4 then
if col != cur_col then
to_round = 10*length2/length
callsub round
emit chunk [cur_col, rounded, length2]
length2 = rounded
j = 0
if cur_col != -1 then
while j < length2 do
if i+j >= 0 and i+j < bits then
code[i+j] = cur_col
end
j++
end
i = i+length2
end
cur_col = last_col
length2 = 0
if i >= bits then
state = 5
emit result code
end
end
end
callsub update_state
onevent timer0
if state == 4 then
if i < bits then
callsub get_avg
code[i] = last_col
i++
flash_col = [31,31,31]
callsub flash
else
state = 5
emit result code
end
end
callsub update_state
onevent timer1
callsub unflash]]></node>
</network>