|
| 1 | +""" |
| 2 | + Copyright (c) 2017-2018 Alan Yorinks All rights reserved. |
| 3 | +
|
| 4 | + This program is free software; you can redistribute it and/or |
| 5 | + modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE |
| 6 | + Version 3 as published by the Free Software Foundation; either |
| 7 | + or (at your option) any later version. |
| 8 | + This library is distributed in the hope that it will be useful, |
| 9 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | + General Public License for more details. |
| 12 | +
|
| 13 | + You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE |
| 14 | + along with this library; if not, write to the Free Software |
| 15 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 16 | +
|
| 17 | + Last modified 5 April 2018 |
| 18 | +""" |
| 19 | + |
| 20 | +from microbit import * |
| 21 | + |
| 22 | + |
| 23 | +# This is a script used to control a micro:bit from s2m |
| 24 | + |
| 25 | +# This loop continuously polls the sensors and then prints a reply string. |
| 26 | +# It also continuously calls readline to check for commands. |
| 27 | +# A command is specified as comma delimited string with the command |
| 28 | +# as the first element followed by its parameters. |
| 29 | + |
| 30 | +# Commands: |
| 31 | +# d - display the specified image |
| 32 | +# s - scroll the specified text |
| 33 | +# p - control a pixel for the specified row, column and intensity |
| 34 | +# c - clear the display (no parameters) |
| 35 | +# a - analog write for the specified pin and value (range 0-1023) |
| 36 | +# t - digital write for the specified pin and value (0 or 1) |
| 37 | +# v - get version string |
| 38 | + |
| 39 | +import sys |
| 40 | +uart = sys.stdin |
| 41 | + |
| 42 | +def logging(func): |
| 43 | + import sys |
| 44 | + def _deco(): |
| 45 | + try: |
| 46 | + func() |
| 47 | + except Exception as e: |
| 48 | + with open("run.log", "a") as f: |
| 49 | + sys.print_exception(e, f) |
| 50 | + return _deco |
| 51 | + |
| 52 | +# a list of current digital pin modes |
| 53 | +@logging |
| 54 | +def loop(): |
| 55 | + digital_outputs = [False, False, False] |
| 56 | + while True: |
| 57 | + data = uart.readline() |
| 58 | + sleep(8) |
| 59 | + if data: |
| 60 | + cmd = str(data, 'utf-8').rstrip() |
| 61 | + if not len(cmd): |
| 62 | + continue |
| 63 | + # noinspection PyUnresolvedReferences |
| 64 | + cmd_list = cmd.split(",") |
| 65 | + # get command id |
| 66 | + |
| 67 | + try: |
| 68 | + cmd_id = cmd_list[0] |
| 69 | + except IndexError: |
| 70 | + cmd_id = 'z' |
| 71 | + continue |
| 72 | + |
| 73 | + # display image command |
| 74 | + if cmd_id == 'd': |
| 75 | + image_dict = {"HAPPY": Image.HAPPY, |
| 76 | + "SAD": Image.SAD, |
| 77 | + "ANGRY": Image.ANGRY, |
| 78 | + "SMILE": Image.SMILE, |
| 79 | + "CONFUSED": Image.CONFUSED, |
| 80 | + "ASLEEP": Image.ASLEEP, |
| 81 | + "SURPRISED": Image.SURPRISED, |
| 82 | + "SILLY": Image.SILLY, |
| 83 | + "FABULOUS": Image.FABULOUS, |
| 84 | + "MEH": Image.MEH, |
| 85 | + "YES": Image.YES, |
| 86 | + "NO": Image.NO, |
| 87 | + "RABBIT": Image.RABBIT, |
| 88 | + "COW": Image.COW, |
| 89 | + "ROLLERSKATE": Image.ROLLERSKATE, |
| 90 | + "HOUSE": Image.HOUSE, |
| 91 | + "SNAKE": Image.SNAKE, |
| 92 | + "HEART": Image.HEART, |
| 93 | + "DIAMOND": Image.DIAMOND, |
| 94 | + "DIAMOND_SMALL": Image.DIAMOND_SMALL, |
| 95 | + "SQUARE": Image.SQUARE, |
| 96 | + "SQUARE_SMALL": Image.SQUARE_SMALL, |
| 97 | + "TRIANGLE": Image.TRIANGLE, |
| 98 | + "TARGET": Image.TARGET, |
| 99 | + "STICKFIGURE": Image.STICKFIGURE, |
| 100 | + "ARROW_N": Image.ARROW_N, |
| 101 | + "ARROW_NE": Image.ARROW_NE, |
| 102 | + "ARROW_E": Image.ARROW_E, |
| 103 | + "ARROW_SE": Image.ARROW_SE, |
| 104 | + "ARROW_S": Image.ARROW_S, |
| 105 | + "ARROW_SW": Image.ARROW_SW, |
| 106 | + "ARROW_W": Image.ARROW_W, |
| 107 | + "ARROW_NW": Image.ARROW_NW} |
| 108 | + |
| 109 | + # get image key |
| 110 | + try: |
| 111 | + image_key = cmd_list[1] |
| 112 | + except IndexError: |
| 113 | + continue |
| 114 | + if image_key in image_dict: |
| 115 | + display.show(image_dict.get(image_key), wait=False) |
| 116 | + |
| 117 | + # scroll text command |
| 118 | + elif cmd_id == 's': |
| 119 | + display.scroll(str(cmd_list[1]), wait=False) |
| 120 | + |
| 121 | + # write pixel command |
| 122 | + elif cmd_id == 'p': |
| 123 | + # get row, column and intensity value |
| 124 | + # make sure values are within valid range |
| 125 | + # print(cmd) |
| 126 | + try: |
| 127 | + x = int(cmd_list[1]) |
| 128 | + except ValueError: |
| 129 | + continue |
| 130 | + except IndexError: |
| 131 | + continue |
| 132 | + |
| 133 | + if x < 0: |
| 134 | + x = 0 |
| 135 | + if x > 4: |
| 136 | + x = 4 |
| 137 | + |
| 138 | + try: |
| 139 | + y = int(cmd_list[2]) |
| 140 | + except ValueError: |
| 141 | + continue |
| 142 | + except IndexError: |
| 143 | + continue |
| 144 | + |
| 145 | + if y < 0: |
| 146 | + y = 0 |
| 147 | + if y > 4: |
| 148 | + y = 4 |
| 149 | + try: |
| 150 | + value = int(cmd_list[3]) |
| 151 | + except ValueError: |
| 152 | + continue |
| 153 | + except IndexError: |
| 154 | + continue |
| 155 | + |
| 156 | + if value < 0: |
| 157 | + value = 0 |
| 158 | + if value > 9: |
| 159 | + value = 9 |
| 160 | + display.set_pixel(x, y, value) |
| 161 | + |
| 162 | + # clear display command |
| 163 | + elif cmd_id == 'c': |
| 164 | + display.clear() |
| 165 | + |
| 166 | + # analog write command |
| 167 | + # if values are out of range, command is ignored |
| 168 | + elif cmd_id == 'a': |
| 169 | + # check pin and value ranges |
| 170 | + try: |
| 171 | + pin = int(cmd_list[1]) |
| 172 | + value = int(cmd_list[2]) |
| 173 | + digital_outputs[pin] = True |
| 174 | + except IndexError: |
| 175 | + continue |
| 176 | + except ValueError: |
| 177 | + continue |
| 178 | + |
| 179 | + if 0 <= pin <= 2: |
| 180 | + if not 0 <= value <= 1023: |
| 181 | + value = 256 |
| 182 | + if pin == 0: |
| 183 | + pin0.write_analog(value) |
| 184 | + elif pin == 1: |
| 185 | + pin1.write_analog(value) |
| 186 | + elif pin == 2: |
| 187 | + pin2.write_analog(value) |
| 188 | + |
| 189 | + # digital write command |
| 190 | + elif cmd_id == 't': |
| 191 | + # check pin and value ranges |
| 192 | + # if values are out of range, command is ignored |
| 193 | + try: |
| 194 | + pin = int(cmd_list[1]) |
| 195 | + value = int(cmd_list[2]) |
| 196 | + digital_outputs[pin] = True |
| 197 | + except IndexError: |
| 198 | + continue |
| 199 | + except ValueError: |
| 200 | + continue |
| 201 | + |
| 202 | + if 0 <= pin <= 2: |
| 203 | + if 0 <= value <= 1: |
| 204 | + if pin == 0: |
| 205 | + pin0.write_digital(value) |
| 206 | + elif pin == 1: |
| 207 | + pin1.write_digital(value) |
| 208 | + elif pin == 2: |
| 209 | + pin2.write_digital(value) |
| 210 | + else: |
| 211 | + pass |
| 212 | + |
| 213 | + elif cmd == 'g': |
| 214 | + # This string will contain the sensor values and will |
| 215 | + # be "printed" to the serial port. |
| 216 | + # Fields are comma delimited |
| 217 | + sensor_string = "" |
| 218 | + |
| 219 | + # 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())) + ',' |
| 223 | + |
| 224 | + # buttons |
| 225 | + sensor_string += str(button_a.is_pressed()) + ',' |
| 226 | + |
| 227 | + sensor_string += str(button_b.is_pressed()) + ',' |
| 228 | + |
| 229 | + # get digital input pin values |
| 230 | + if not digital_outputs[0]: |
| 231 | + sensor_string += str(pin0.read_digital()) + ',' |
| 232 | + else: |
| 233 | + sensor_string += '0' + ',' |
| 234 | + # |
| 235 | + if not digital_outputs[1]: |
| 236 | + sensor_string += str(pin1.read_digital()) + ',' |
| 237 | + else: |
| 238 | + sensor_string += '0' + ',' |
| 239 | + # |
| 240 | + if not digital_outputs[2]: |
| 241 | + sensor_string += str(pin2.read_digital()) + ',' |
| 242 | + else: |
| 243 | + sensor_string += '0' + ',' |
| 244 | + |
| 245 | + # get analog input pin values |
| 246 | + if not digital_outputs[0]: |
| 247 | + sensor_string += '0' + ',' |
| 248 | + else: |
| 249 | + sensor_string += '0' + ',' |
| 250 | + |
| 251 | + if not digital_outputs[1]: |
| 252 | + sensor_string += str(pin1.read_analog()) + ',' |
| 253 | + else: |
| 254 | + sensor_string += '0' + ',' |
| 255 | + |
| 256 | + if not digital_outputs[2]: |
| 257 | + sensor_string += str(pin2.read_analog()) |
| 258 | + else: |
| 259 | + sensor_string += '0' + ',' |
| 260 | + |
| 261 | + print(sensor_string) |
| 262 | + sleep(10) |
| 263 | + |
| 264 | + |
| 265 | + elif cmd == 'v': |
| 266 | + print('s2mb.py Version 1.10 14 April 2018') |
| 267 | + else: |
| 268 | + continue |
| 269 | + sleep(8) |
| 270 | + |
| 271 | +while True: |
| 272 | + loop() |
0 commit comments