|
19 | 19 |
|
20 | 20 | from microbit import * |
21 | 21 |
|
22 | | - |
23 | 22 | # This is a script used to control a micro:bit from s2m |
24 | 23 |
|
25 | 24 | # This loop continuously polls the sensors and then prints a reply string. |
@@ -111,12 +110,83 @@ def loop(): |
111 | 110 | image_key = cmd_list[1] |
112 | 111 | except IndexError: |
113 | 112 | continue |
114 | | - if image_key in image_dict: |
115 | | - display.show(image_dict.get(image_key),[0,0,255]) |
| 113 | + try: |
| 114 | + r_value = int(cmd_list[2]) |
| 115 | + except ValueError: |
| 116 | + continue |
| 117 | + except IndexError: |
| 118 | + continue |
| 119 | + |
| 120 | + if r_value < 0: |
| 121 | + r_value = 0 |
| 122 | + if r_value > 255: |
| 123 | + r_value = 255 |
| 124 | + try: |
| 125 | + g_value = int(cmd_list[3]) |
| 126 | + except ValueError: |
| 127 | + continue |
| 128 | + except IndexError: |
| 129 | + continue |
| 130 | + |
| 131 | + if g_value < 0: |
| 132 | + g_value = 0 |
| 133 | + if g_value > 255: |
| 134 | + g_value = 255 |
| 135 | + try: |
| 136 | + b_value = int(cmd_list[4]) |
| 137 | + except ValueError: |
| 138 | + continue |
| 139 | + except IndexError: |
| 140 | + continue |
| 141 | + |
| 142 | + if b_value < 0: |
| 143 | + b_value = 0 |
| 144 | + if b_value > 255: |
| 145 | + b_value = 255 |
| 146 | + |
| 147 | + #if image_key in image_dict: |
| 148 | + display.show(image_dict.get(image_key),[r_value,g_value,b_value]) |
116 | 149 |
|
117 | 150 | # scroll text command |
118 | 151 | elif cmd_id == 's': |
119 | | - display.scroll(str(cmd_list[1]),[0,0,255]) |
| 152 | + try: |
| 153 | + r_value = int(cmd_list[2]) |
| 154 | + except ValueError: |
| 155 | + continue |
| 156 | + except IndexError: |
| 157 | + continue |
| 158 | + |
| 159 | + if r_value < 0: |
| 160 | + r_value = 0 |
| 161 | + if r_value > 255: |
| 162 | + r_value = 255 |
| 163 | + |
| 164 | + try: |
| 165 | + g_value = int(cmd_list[3]) |
| 166 | + except ValueError: |
| 167 | + continue |
| 168 | + except IndexError: |
| 169 | + continue |
| 170 | + |
| 171 | + if g_value < 0: |
| 172 | + g_value = 0 |
| 173 | + if g_value > 255: |
| 174 | + g_value = 255 |
| 175 | + |
| 176 | + try: |
| 177 | + b_value = int(cmd_list[4]) |
| 178 | + except ValueError: |
| 179 | + continue |
| 180 | + except IndexError: |
| 181 | + continue |
| 182 | + |
| 183 | + if b_value < 0: |
| 184 | + b_value = 0 |
| 185 | + if b_value > 255: |
| 186 | + b_value = 255 |
| 187 | + |
| 188 | + display.scroll(str(cmd_list[1]),[r_value,g_value,b_value]) |
| 189 | + |
120 | 190 |
|
121 | 191 | # write pixel command |
122 | 192 | elif cmd_id == 'p': |
@@ -147,17 +217,46 @@ def loop(): |
147 | 217 | if y > 4: |
148 | 218 | y = 4 |
149 | 219 | try: |
150 | | - value = int(cmd_list[3]) |
| 220 | + r_value = int(cmd_list[3]) |
151 | 221 | except ValueError: |
152 | 222 | continue |
153 | 223 | except IndexError: |
154 | 224 | continue |
155 | 225 |
|
156 | | - if value < 0: |
157 | | - value = 0 |
158 | | - if value > 9: |
159 | | - value = 9 |
160 | | - display.set_pixel(x, y, value) |
| 226 | + if r_value < 0: |
| 227 | + r_value = 0 |
| 228 | + if r_value > 255: |
| 229 | + r_value = 255 |
| 230 | + |
| 231 | + try: |
| 232 | + g_value = int(cmd_list[4]) |
| 233 | + except ValueError: |
| 234 | + continue |
| 235 | + except IndexError: |
| 236 | + continue |
| 237 | + |
| 238 | + if g_value < 0: |
| 239 | + g_value = 0 |
| 240 | + if g_value > 255: |
| 241 | + g_value = 255 |
| 242 | + |
| 243 | + try: |
| 244 | + b_value = int(cmd_list[5]) |
| 245 | + except ValueError: |
| 246 | + continue |
| 247 | + except IndexError: |
| 248 | + continue |
| 249 | + |
| 250 | + if b_value < 0: |
| 251 | + b_value = 0 |
| 252 | + if b_value > 255: |
| 253 | + b_value = 255 |
| 254 | + |
| 255 | + #display.set_pixel(x, y, value) |
| 256 | + from pixel import Pixel |
| 257 | + View = Pixel() |
| 258 | + View.LoadXY(x, y, [r_value,g_value,b_value]) |
| 259 | + View.Show() |
161 | 260 |
|
162 | 261 | # clear display command |
163 | 262 | elif cmd_id == 'c': |
@@ -217,9 +316,9 @@ def loop(): |
217 | 316 | sensor_string = "" |
218 | 317 |
|
219 | 318 | # accelerometer |
220 | | - sensor_string += str(int(accelerometer.get_x())) + ',' |
221 | | - sensor_string += str(int(accelerometer.get_y())) + ',' |
222 | | - sensor_string += str(int(accelerometer.get_z())) + ',' |
| 319 | + sensor_string += str(int(accelerometer.get_x()*20)) + ',' |
| 320 | + sensor_string += str(int(accelerometer.get_y()*20)) + ',' |
| 321 | + sensor_string += str(int(accelerometer.get_z()*20)) + ',' |
223 | 322 |
|
224 | 323 | # buttons |
225 | 324 | sensor_string += str(button_a.is_pressed()) + ',' |
@@ -254,9 +353,17 @@ def loop(): |
254 | 353 | sensor_string += '0' + ',' |
255 | 354 |
|
256 | 355 | if not digital_outputs[2]: |
257 | | - sensor_string += str(pin2.read_analog()) |
| 356 | + sensor_string += str(pin2.read_analog())+',' |
258 | 357 | else: |
259 | 358 | sensor_string += '0' + ',' |
| 359 | + ###temperature |
| 360 | + sensor_string += str(temperature()) +',' |
| 361 | + #L R light |
| 362 | + import light |
| 363 | + R = light.Intensity(39) |
| 364 | + L = light.Intensity(36) |
| 365 | + sensor_string += str(int(L.read()))+',' |
| 366 | + sensor_string += str(int(R.read())) |
260 | 367 |
|
261 | 368 | print(sensor_string) |
262 | 369 | sleep(10) |
|
0 commit comments