├── LICENSE.txt ├── README.md ├── docs ├── demo.jpg └── tm1637_datasheet_v2.4.pdf ├── examples ├── counter │ ├── main.py │ └── readme.md ├── direction │ ├── main.py │ └── readme.md ├── gestures │ ├── main.py │ └── readme.md ├── leds │ ├── main.py │ └── readme.md ├── random │ ├── main.py │ └── readme.md ├── running_time │ ├── main.py │ └── readme.md ├── scoreboard │ ├── main.py │ └── readme.md ├── scroll │ ├── main.py │ └── readme.md ├── show │ ├── main.py │ └── readme.md ├── stopwatch │ ├── main.py │ └── readme.md └── temperature │ ├── main.py │ └── readme.md ├── firmware ├── AUTHORS.txt ├── LICENSE.txt ├── README.md ├── microbit-micropython-v1.7.9-gbe020eb.hex └── microbit-micropython-v1.9.2-34-gd64154c73.hex └── tm1637.py /LICENSE.txt: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Mike Causer 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # BBC micro:bit MicroPython TM1637 2 | 3 | A micro:bit MicroPython library for quad 7-segment LED display modules using the TM1637 LED driver. 4 | 5 | For example, the [Grove - 4 Digit Display module](http://wiki.seeed.cc/Grove-4-Digit_Display/) 6 | 7 | ![demo](docs/demo.jpg) 8 | 9 | ## Installation 10 | 11 | * Install MicroPython for micro:bit by either compiling from [source](https://github.com/bbcmicrobit/micropython) or using one of the included [precompiled firmwares](/firmware) 12 | * Install [ufs](https://github.com/ntoll/microfs) to upload .py scripts 13 | * Copy `tm1637.py` and one of the example `main.py` scripts to the micro:bit 14 | * Reset to run `main.py` on boot. 15 | 16 | # Examples 17 | 18 | There are multiple methods which can produce the same result. Here are a few examples to get you started. 19 | 20 | ```python 21 | from microbit import * 22 | from tm1637 import TM1637 23 | tm = TM1637(clk=pin1, dio=pin2) 24 | 25 | # all LEDS on "88:88" 26 | tm.write([127, 255, 127, 127]) 27 | tm.show('8888', True) 28 | tm.numbers(88, 88) 29 | 30 | # all LEDS off 31 | tm.write([0, 0, 0, 0]) 32 | tm.show(' ') 33 | 34 | # write to the 2nd and 3rd segments only 35 | tm.write([119, 124], 1) 36 | tm.write([124], 2) 37 | tm.write([119], 1) 38 | 39 | # display "0123" 40 | tm.show('1234') 41 | tm.number(1234) 42 | tm.numbers(12, 34) 43 | 44 | # show "COOL" 45 | tm.write([0b00111001, 0b00111111, 0b00111111, 0b00111000]) 46 | tm.write([0x39, 0x3F, 0x3F, 0x38]) 47 | tm.write([57, 63, 63, 56]) 48 | tm.show('cool') 49 | tm.show('COOL') 50 | 51 | # display "dEAd", "bEEF" 52 | tm.hex(0xdead) 53 | tm.hex(0xbeef) 54 | tm.show('dead') 55 | tm.show('Beef') 56 | 57 | # show "12:59" 58 | tm.numbers(12,59) 59 | tm.number(1259, True) 60 | tm.show('1259', True) 61 | 62 | # show "-123" 63 | tm.number(-123) 64 | tm.show('-123') 65 | 66 | # show temperature '24*C' 67 | tm.temperature(24) 68 | tm.show('24*C') 69 | 70 | # get current brightness 71 | tm.brightness() 72 | 73 | # reduce brightness 74 | tm.brightness(3) 75 | ``` 76 | 77 | For more detailed examples, see /examples. 78 | 79 | # Seven Segment Font 80 | 81 | They are called 7-segment displays as there are 7 LEDs for each digit (segment). 82 | One byte (7 lower bits) for each segment. The 8th bit (MSB) is for the colon and only on the 2nd segment. 83 | 84 | 85 | ``` 86 | A 87 | --- 88 | F | | B * 89 | -G- H (on 2nd segment) 90 | E | | C * 91 | --- 92 | D 93 | 94 | HGFEDCBA 95 | 0b01101101 = 0x6D = 109 = show "5" 96 | ``` 97 | 98 | Display | Bin | Hex | Dec 99 | ------- | ---------- | ---- | --- 100 | 0 | 0b00111111 | 0x3F | 63 101 | 1 | 0b00000110 | 0x06 | 6 102 | 2 | 0b01011011 | 0x5B | 91 103 | 3 | 0b01001111 | 0x4F | 79 104 | 4 | 0b01100110 | 0x66 | 102 105 | 5 | 0b01101101 | 0x6D | 109 106 | 6 | 0b01111101 | 0x7D | 125 107 | 7 | 0b00000111 | 0x07 | 7 108 | 8 | 0b01111111 | 0x7F | 127 109 | 9 | 0b01101111 | 0x6F | 111 110 | A | 0b01110111 | 0x77 | 119 111 | b | 0b01111100 | 0x7C | 124 112 | C | 0b00111001 | 0x39 | 57 113 | d | 0b01011110 | 0x5E | 94 114 | E | 0b01111001 | 0x79 | 121 115 | F | 0b01110001 | 0x71 | 113 116 | G | 0b00111101 | 0x3D | 61 117 | H | 0b01110110 | 0x76 | 118 118 | I | 0b00000110 | 0x06 | 6 119 | J | 0b00011110 | 0x1E | 30 120 | K | 0b01110110 | 0x76 | 118 121 | L | 0b00111000 | 0x38 | 56 122 | M | 0b01010101 | 0x55 | 85 123 | n | 0b01010100 | 0x54 | 84 124 | O | 0b00111111 | 0x3F | 63 125 | P | 0b01110011 | 0x73 | 115 126 | q | 0b01100111 | 0x67 | 103 127 | r | 0b01010000 | 0x50 | 80 128 | S | 0b01101101 | 0x6D | 109 129 | t | 0b01111000 | 0x78 | 120 130 | U | 0b00111110 | 0x3E | 62 131 | v | 0b00011100 | 0x1C | 28 132 | W | 0b00101010 | 0x2A | 42 133 | X | 0b01110110 | 0x76 | 118 134 | y | 0b01101110 | 0x6E | 110 135 | Z | 0b01011011 | 0x5B | 91 136 | blank | 0b00000000 | 0x00 | 0 137 | \- | 0b01000000 | 0x40 | 64 138 | \* | 0b01100011 | 0x63 | 99 139 | 140 | # Methods 141 | 142 | Get or set brightness. 143 | ``` 144 | brightness(val=None) 145 | ``` 146 | 147 | Write one or more segments at a given offset. 148 | ``` 149 | write(segments, pos=0) 150 | ``` 151 | 152 | Convert a string to a list of segments. 153 | ``` 154 | encode_string(string) 155 | ``` 156 | 157 | Convert a single character to a segment. 158 | ``` 159 | encode_char(char) 160 | ``` 161 | 162 | Display a number in hexadecimal format 0000 through FFFF. 163 | ``` 164 | hex(val) 165 | ``` 166 | 167 | Display a number -999 through 9999, right aligned. 168 | ``` 169 | number(num) 170 | ``` 171 | 172 | Display 2 independent numbers on either side of the (optional) colon, with leading zeros. 173 | ``` 174 | numbers(num1, num2, colon=True) 175 | ``` 176 | 177 | Display a temperature -9 through 99 followed by degrees C. 178 | ``` 179 | temperature(num) 180 | ``` 181 | 182 | Show a string on the display. 183 | Shorthand for write(encode_string()). 184 | Limited to first 4 characters. 185 | ``` 186 | show(string, colon=False) 187 | ``` 188 | 189 | Display a string on the display, scrolling from the right to left, speed adjustable. 190 | String starts off-screen and scrolls until off-screen at 4 FPS by default. 191 | ``` 192 | scroll(string, delay=250) 193 | ``` 194 | 195 | ## Parts 196 | 197 | * [BBC micro:bit](https://tronixlabs.com.au/bbc-micro-bit/bbc-micro-bit-board-only-retail-pack-australia/) $24.95 AUD 198 | * [Edge Connector Breakout Board](https://tronixlabs.com.au/bbc-micro-bit/edge-connector-breakout-board-for-bbc-micro-bit-australia/) $11.95 AUD 199 | * [Grove 4 Digit Display](https://www.seeedstudio.com/grove-4digital-display-p-1198.html) $5.90 USD 200 | * [Grove Male Jumper Cable](https://www.seeedstudio.com/Grove-4-pin-Male-Jumper-to-Grove-4-pin-Conversion-Cable-%285-PCs-per-Pack%29-p-1565.html) $2.90 USD 201 | 202 | ## Connections 203 | 204 | micro:bit | Grove 4 Digit Display 205 | --------- | --------------- 206 | Pin 1 | CLK (yellow) 207 | Pin 2 | DIO (white) 208 | 3V3 | VCC (red) 209 | GND | GND (black) 210 | 211 | An edge connector breakout board comes in handy here. 212 | 213 | You're welcome to change the data and clock pins to something else - just update `TM1637(clk=pin1, dio=pin2)` in your `main.py` 214 | 215 | ## Links 216 | 217 | * [BBC micro:bit](http://microbit.org/) 218 | * [MicroPython for the BBC micro:bit](https://github.com/bbcmicrobit/micropython) 219 | * [Kitronik Edge Connector Breakout Board](https://www.https://www.kitronik.co.uk/5601b-edge-connector-breakout-board-for-bbc-microbit-pre-built.html.co.uk/5601b-edge-connector-breakout-board-for-bbc-microbit-pre-built.html) 220 | * [Grove 4 Digit Display Wiki Page](http://wiki.seeed.cc/Grove-4-Digit_Display/) 221 | * [micropython.org](http://micropython.org) 222 | * [micro:bit on the MicroPython forum](https://forum.micropython.org/viewforum.php?f=17) 223 | * [microfs](https://github.com/ntoll/microfs) 224 | * [TM1637 datasheet](http://www.titanmec.com/index.php/en/project/download/id/302.html) 225 | * [Titan Micro TM1637 product page](http://www.titanmec.com/index.php/en/project/view/id/302.html) 226 | * [MicroPython TM1637](https://github.com/mcauser/micropython-tm1637) 227 | 228 | # Troubleshooting 229 | 230 | If you upload a new `.hex` file, all changes are overwritten and you will need to `ufs put` both the `tm1637.py` and `main.py` scripts again. 231 | 232 | If you edit one of the example precompiled `.hex` files in one of the [online editors](https://python.microbit.org/v/1), it may swap the MicroPython runtime to an older version and introduce bugs. 233 | Best to flash one of the stock MicroPython firmwares and upload your own `tm1637.py` and `main.py` scripts. 234 | 235 | If you flash a `.hex` file from an online editor, it may block `main.py` from running as the editor combines the MicroPython runtime with your script. 236 | Your script is executed like the main script, however, you can't `ufs get` and `ufs put` changes. 237 | 238 | If you board is throwing MemoryErrors and you are unable to continue, start fresh by copying one of the MicroPython firmware `.hex` files to the MICROBIT mounted filesystem. 239 | 240 | ## License 241 | 242 | Licensed under the [MIT License](http://opensource.org/licenses/MIT). 243 | -------------------------------------------------------------------------------- /docs/demo.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mcauser/microbit-tm1637/5792d44a4145df8be1ab902f19378ff9a493de0f/docs/demo.jpg -------------------------------------------------------------------------------- /docs/tm1637_datasheet_v2.4.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mcauser/microbit-tm1637/5792d44a4145df8be1ab902f19378ff9a493de0f/docs/tm1637_datasheet_v2.4.pdf -------------------------------------------------------------------------------- /examples/counter/main.py: -------------------------------------------------------------------------------- 1 | from microbit import * 2 | from tm1637 import TM1637 3 | 4 | tm = TM1637(clk=pin1, dio=pin2) 5 | 6 | num = 0 7 | tm.number(num) 8 | 9 | while True: 10 | if button_a.was_pressed(): 11 | num = num + 1 12 | tm.number(num) 13 | elif button_b.was_pressed(): 14 | num = num - 1 15 | tm.number(num) 16 | -------------------------------------------------------------------------------- /examples/counter/readme.md: -------------------------------------------------------------------------------- 1 | # Counter 2 | 3 | * Start with zero. 4 | * Press button A to increment. 5 | * Press button B to decrement. 6 | * Number is changed and sent to the display on button up. 7 | 8 | ## Install 9 | 10 | * Connect to micro:bit via USB 11 | * Install v1.9.2 firmware 12 | 13 | ``` 14 | $ ufs put tm1637.py 15 | $ ufs put examples/counter/main.py 16 | ``` 17 | 18 | ## Links 19 | 20 | * [BBC micro:bit docs](https://microbit-micropython.readthedocs.io/en/latest/tutorials/buttons.html) 21 | -------------------------------------------------------------------------------- /examples/direction/main.py: -------------------------------------------------------------------------------- 1 | from microbit import * 2 | from tm1637 import TM1637 3 | 4 | tm = TM1637(clk=pin1, dio=pin2) 5 | 6 | compass.calibrate() 7 | 8 | while True: 9 | tm.number(compass.heading()) 10 | needle = ((15 - compass.heading()) // 30) % 12 11 | display.show(Image.ALL_CLOCKS[needle]) 12 | sleep(10) 13 | -------------------------------------------------------------------------------- /examples/direction/readme.md: -------------------------------------------------------------------------------- 1 | # Direction 2 | 3 | * Display compass heading on 4 digit display 4 | * Display compass using clock hands on micro:bit led matrix 5 | 6 | ## Install 7 | 8 | * Connect to micro:bit via USB 9 | * Install v1.9.2 firmware 10 | 11 | ``` 12 | $ ufs put tm1637.py 13 | $ ufs put examples/direction/main.py 14 | ``` 15 | 16 | ## Links 17 | 18 | * [BBC micro:bit docs](https://microbit-micropython.readthedocs.io/en/latest/tutorials/direction.html) 19 | -------------------------------------------------------------------------------- /examples/gestures/main.py: -------------------------------------------------------------------------------- 1 | from microbit import * 2 | from tm1637 import TM1637 3 | 4 | tm = TM1637(clk=pin1, dio=pin2) 5 | 6 | while True: 7 | gesture = accelerometer.current_gesture() 8 | if gesture == "up": 9 | tm.scroll('up'); 10 | elif gesture == "down": 11 | tm.scroll('down'); 12 | elif gesture == "left": 13 | tm.scroll('left'); 14 | elif gesture == "right": 15 | tm.scroll('right'); 16 | elif gesture == "face up": 17 | tm.scroll('face up'); 18 | elif gesture == "face down": 19 | tm.scroll('face down'); 20 | elif gesture == "freefall": 21 | tm.scroll('freefall'); 22 | elif gesture == "3g": 23 | tm.scroll('3g'); 24 | elif gesture == "6g": 25 | tm.scroll('6g'); 26 | elif gesture == "8g": 27 | tm.scroll('8g'); 28 | elif gesture == "shake": 29 | tm.scroll('shake'); 30 | -------------------------------------------------------------------------------- /examples/gestures/readme.md: -------------------------------------------------------------------------------- 1 | # Gestures 2 | 3 | * Listen for gestures: up, down, left, right, face up, face down, freefall, 3g, 6g, 8g, shake 4 | * Show captured gesture 5 | 6 | ## Install 7 | 8 | * Connect to micro:bit via USB 9 | * Install v1.9.2 firmware 10 | 11 | ``` 12 | $ ufs put tm1637.py 13 | $ ufs put examples/gestures/main.py 14 | ``` 15 | 16 | ## Links 17 | 18 | * [BBC micro:bit docs](https://microbit-micropython.readthedocs.io/en/latest/tutorials/gestures.html) 19 | -------------------------------------------------------------------------------- /examples/leds/main.py: -------------------------------------------------------------------------------- 1 | from microbit import * 2 | from tm1637 import TM1637 3 | 4 | tm = TM1637(clk=pin1, dio=pin2) 5 | 6 | num = 0 7 | class Stop(Exception): 8 | pass 9 | 10 | try: 11 | # walk through all possible LED combinations 12 | # press A or B buttons to stop 13 | while True: 14 | for i in range(128): 15 | tm.number(i) 16 | tm.write([i]) 17 | if button_a.is_pressed() or button_b.is_pressed(): 18 | num = i 19 | raise Stop 20 | sleep(100) 21 | except Stop: 22 | while True: 23 | # Press A to increment 24 | if button_a.is_pressed(): 25 | num = num + 1 26 | if num > 255: 27 | num = 0 28 | tm.number(num) 29 | tm.write([num]) 30 | # Press B to decrement 31 | elif button_b.is_pressed(): 32 | num = num - 1 33 | if num < 0: 34 | num = 255 35 | tm.number(num) 36 | tm.write([num]) 37 | sleep(100) 38 | -------------------------------------------------------------------------------- /examples/leds/readme.md: -------------------------------------------------------------------------------- 1 | # LEDs 2 | 3 | * Walk through all possible LED combinations 4 | * Press any button to stop 5 | * Press A to increment 6 | * Press B to decrement 7 | 8 | ## Install 9 | 10 | * Connect to micro:bit via USB 11 | * Install v1.9.2 firmware 12 | 13 | ``` 14 | $ ufs put tm1637.py 15 | $ ufs put examples/leds/main.py 16 | ``` 17 | 18 | ## Links 19 | 20 | * [BBC micro:bit docs](https://microbit-micropython.readthedocs.io/en/latest/tutorials/buttons.html) 21 | -------------------------------------------------------------------------------- /examples/random/main.py: -------------------------------------------------------------------------------- 1 | from microbit import * 2 | import random 3 | from tm1637 import TM1637 4 | 5 | tm = TM1637(clk=pin1, dio=pin2) 6 | 7 | random.seed(1337) 8 | 9 | tm.show('rand') 10 | 11 | while True: 12 | if button_a.was_pressed(): 13 | tm.number(random.randint(1, 9999)) 14 | -------------------------------------------------------------------------------- /examples/random/readme.md: -------------------------------------------------------------------------------- 1 | # Random 2 | 3 | * Display "rand" 4 | * Press A to display a random number 0-9999 5 | 6 | ## Install 7 | 8 | * Connect to micro:bit via USB 9 | * Install v1.9.2 firmware 10 | 11 | ``` 12 | $ ufs put tm1637.py 13 | $ ufs put examples/random/main.py 14 | ``` 15 | 16 | ## Links 17 | 18 | * [BBC micro:bit docs](https://microbit-micropython.readthedocs.io/en/latest/tutorials/random.html) 19 | -------------------------------------------------------------------------------- /examples/running_time/main.py: -------------------------------------------------------------------------------- 1 | from microbit import * 2 | import random 3 | from tm1637 import TM1637 4 | 5 | tm = TM1637(clk=pin1, dio=pin2) 6 | 7 | while True: 8 | tm.number(running_time() // 1000) 9 | sleep(1000) 10 | -------------------------------------------------------------------------------- /examples/running_time/readme.md: -------------------------------------------------------------------------------- 1 | # Running Time 2 | 3 | * Displays the running_time() once per second 4 | 5 | ## Install 6 | 7 | * Connect to micro:bit via USB 8 | * Install v1.9.2 firmware 9 | 10 | ``` 11 | $ ufs put tm1637.py 12 | $ ufs put examples/running_time/main.py 13 | ``` 14 | 15 | ## Links 16 | 17 | * [BBC micro:bit docs](https://microbit-micropython.readthedocs.io/en/latest/microbit.html) 18 | -------------------------------------------------------------------------------- /examples/scoreboard/main.py: -------------------------------------------------------------------------------- 1 | from microbit import * 2 | from tm1637 import TM1637 3 | 4 | tm = TM1637(clk=pin1, dio=pin2) 5 | 6 | # write P1:P2 7 | tm.show('P1P2', True) 8 | sleep(1000) 9 | 10 | player1 = 0 11 | player2 = 0 12 | tm.numbers(player1, player2) 13 | 14 | while True: 15 | if button_a.is_pressed(): 16 | player1 = player1 + 1 17 | tm.numbers(player1, player2) 18 | if button_b.is_pressed(): 19 | player2 = player2 + 1 20 | tm.numbers(player1, player2) 21 | sleep(100) 22 | -------------------------------------------------------------------------------- /examples/scoreboard/readme.md: -------------------------------------------------------------------------------- 1 | # Scoreboard 2 | 3 | * Displays P1:P2 for a second 4 | * Press button A to increment left 2 digits 5 | * Press button B to increment right 2 digits 6 | 7 | ## Install 8 | 9 | * Connect to micro:bit via USB 10 | * Install v1.9.2 firmware 11 | 12 | ``` 13 | $ ufs put tm1637.py 14 | $ ufs put examples/scoreboard/main.py 15 | ``` 16 | 17 | ## Links 18 | 19 | * [BBC micro:bit docs](https://microbit-micropython.readthedocs.io/en/latest/tutorials/buttons.html) 20 | -------------------------------------------------------------------------------- /examples/scroll/main.py: -------------------------------------------------------------------------------- 1 | from microbit import * 2 | from tm1637 import TM1637, _SEG 3 | 4 | tm = TM1637(clk=pin1, dio=pin2) 5 | 6 | while True: 7 | tm.scroll('Hello World', 500) 8 | sleep(2000) 9 | tm.scroll('The quick brown fox jumps over the lazy dog') 10 | sleep(2000) 11 | tm.scroll('123456789') 12 | sleep(2000) 13 | tm.scroll(list(_SEG)) 14 | sleep(2000) 15 | -------------------------------------------------------------------------------- /examples/scroll/readme.md: -------------------------------------------------------------------------------- 1 | # Scroll 2 | 3 | * Display strings on the 4 digit display, scrolling right to left 4 | 5 | ## Install 6 | 7 | * Connect to micro:bit via USB 8 | * Install v1.9.2 firmware 9 | 10 | ``` 11 | $ ufs put tm1637.py 12 | $ ufs put examples/scroll/main.py 13 | ``` 14 | -------------------------------------------------------------------------------- /examples/show/main.py: -------------------------------------------------------------------------------- 1 | from microbit import * 2 | from tm1637 import TM1637 3 | 4 | tm = TM1637(clk=pin1, dio=pin2) 5 | 6 | while True: 7 | tm.show(' ') 8 | sleep(1000) 9 | tm.show('1234') 10 | sleep(1000) 11 | tm.show('help') 12 | sleep(1000) 13 | tm.show('cafe') 14 | sleep(1000) 15 | tm.show('beef') 16 | sleep(1000) 17 | tm.show('rainbow') # only shows rain 18 | sleep(1000) 19 | tm.show('cool') 20 | sleep(1000) 21 | tm.show('f') # fool 22 | sleep(1000) 23 | tm.show('p') # pool 24 | sleep(1000) 25 | tm.show('t') # tool 26 | sleep(1000) 27 | -------------------------------------------------------------------------------- /examples/show/readme.md: -------------------------------------------------------------------------------- 1 | # Show 2 | 3 | * Displays a few strings, one second apart 4 | 5 | ## Install 6 | 7 | * Connect to micro:bit via USB 8 | * Install v1.9.2 firmware 9 | 10 | ``` 11 | $ ufs put tm1637.py 12 | $ ufs put examples/show/main.py 13 | ``` 14 | -------------------------------------------------------------------------------- /examples/stopwatch/main.py: -------------------------------------------------------------------------------- 1 | from microbit import * 2 | from tm1637 import TM1637 3 | 4 | tm = TM1637(clk=pin1, dio=pin2) 5 | 6 | tm.show('stop') 7 | 8 | num = 0 9 | started = False 10 | 11 | while True: 12 | if button_a.was_pressed(): 13 | # start/stop 14 | started = not started 15 | if button_b.was_pressed(): 16 | # reset 17 | num = 0 18 | started = False 19 | tm.number(num) 20 | if started: 21 | num = num + 1 22 | tm.number(num) 23 | sleep(1000) 24 | -------------------------------------------------------------------------------- /examples/stopwatch/readme.md: -------------------------------------------------------------------------------- 1 | # Stopwatch 2 | 3 | * A = Start/Stop 4 | * B = Reset 5 | 6 | ## Install 7 | 8 | * Connect to micro:bit via USB 9 | * Install v1.9.2 firmware 10 | 11 | ``` 12 | $ ufs put tm1637.py 13 | $ ufs put examples/stopwatch/main.py 14 | ``` 15 | 16 | ## Links 17 | 18 | * [BBC micro:bit docs](https://microbit-micropython.readthedocs.io/en/latest/tutorials/buttons.html) 19 | -------------------------------------------------------------------------------- /examples/temperature/main.py: -------------------------------------------------------------------------------- 1 | from microbit import * 2 | from tm1637 import TM1637 3 | 4 | tm = TM1637(clk=pin1, dio=pin2) 5 | 6 | while True: 7 | tm.temperature(temperature()) 8 | sleep(1000) 9 | -------------------------------------------------------------------------------- /examples/temperature/readme.md: -------------------------------------------------------------------------------- 1 | # Temperature 2 | 3 | * Display the temperature in degrees celcius. 4 | 5 | ## Install 6 | 7 | * Connect to micro:bit via USB 8 | * Install v1.9.2 firmware 9 | 10 | ``` 11 | $ ufs put tm1637.py 12 | $ ufs put examples/temperature/main.py 13 | ``` 14 | 15 | ## Links 16 | 17 | * [BBC micro:bit docs](https://microbit-micropython.readthedocs.io/en/latest/microbit.html) 18 | -------------------------------------------------------------------------------- /firmware/AUTHORS.txt: -------------------------------------------------------------------------------- 1 | Damien P. George (@dpgeorge) 2 | Nicholas H. Tollervey (@ntoll) 3 | Matthew Else (@matthewelse) 4 | Alan M. Jackson (@alanmjackson) 5 | Mark Shannon (@markshannon) 6 | Larry Hastings (@larryhastings) 7 | Mariia Koroliuk (@marichkakorolyuk) 8 | Andrew Mulholland (@gbaman) 9 | Joe Glancy (@JoeGlancy) -------------------------------------------------------------------------------- /firmware/LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013-2016 The MicroPython-on-micro:bit Developers, as listed 4 | in the accompanying AUTHORS file 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. -------------------------------------------------------------------------------- /firmware/README.md: -------------------------------------------------------------------------------- 1 | # Precompiled MicroPython firmware for the BBC micro:bit 2 | 3 | https://github.com/bbcmicrobit/micropython 4 | 5 | #### microbit-micropython-v1.7.9-gbe020eb.hex 6 | 7 | * https://github.com/bbcmicrobit/micropython/commits/master 8 | * https://github.com/bbcmicrobit/micropython/tree/09162c6b38d24f464db0fc1fbdf04658baaf9928 9 | 10 | #### microbit-micropython-v1.9.2-34-gd64154c73.hex 11 | 12 | * https://github.com/bbcmicrobit/micropython/commits/version1 13 | * https://github.com/bbcmicrobit/micropython/tree/e17de0954178d88aa2a1d70b6e6ebc43f5456607 14 | 15 | # Installation 16 | 17 | * This will overwrite your scripts, so be sure to backup any existing code you wish to keep. 18 | * Connect the micro:bit to your computer via USB and you should see a MICROBIT drive appear. 19 | * Copy the v1.7.9 or v1.9.2 precompiled firmware `.hex` file to the MICROBIT drive and wait for board to reboot. 20 | * Use [ufs](https://github.com/ntoll/microfs) to copy the `tm1637.py` script to the flash. 21 | * Open the /examples folders and use `ufs` to copy one of the `main.py` scripts to the flash. 22 | * Reboot the micro:bit to run `main.py`. 23 | * Edit `main.py` and `ufs put` your changes and reboot. 24 | 25 | ``` 26 | $ ufs put tm1637.py 27 | $ ufs put examples/counter/main.py 28 | ``` 29 | -------------------------------------------------------------------------------- /tm1637.py: -------------------------------------------------------------------------------- 1 | """ 2 | MicroPython for micro:bit TM1637 quad 7-segment LED display driver 3 | https://github.com/mcauser/microbit-tm1637 4 | 5 | MIT License 6 | Copyright (c) 2017 Mike Causer 7 | 8 | Permission is hereby granted, free of charge, to any person obtaining a copy 9 | of this software and associated documentation files (the "Software"), to deal 10 | in the Software without restriction, including without limitation the rights 11 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | copies of the Software, and to permit persons to whom the Software is 13 | furnished to do so, subject to the following conditions: 14 | 15 | The above copyright notice and this permission notice shall be included in all 16 | copies or substantial portions of the Software. 17 | 18 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | SOFTWARE. 25 | """ 26 | 27 | from microbit import sleep 28 | 29 | _SEG = bytearray(b'\x3F\x06\x5B\x4F\x66\x6D\x7D\x07\x7F\x6F\x77\x7C\x39\x5E\x79\x71\x3D\x76\x06\x1E\x76\x38\x55\x54\x3F\x73\x67\x50\x6D\x78\x3E\x1C\x2A\x76\x6E\x5B\x00\x40\x63') 30 | 31 | class TM1637(object): 32 | def __init__(self, clk, dio, brightness=7): 33 | self._c = clk 34 | self._d = dio 35 | self._b = max(0, min(brightness, 7)) 36 | self._data_cmd() 37 | self._dsp_ctrl() 38 | 39 | def _start(self): 40 | self._d.write_digital(0) 41 | self._c.write_digital(0) 42 | 43 | def _stop(self): 44 | self._d.write_digital(0) 45 | self._c.write_digital(1) 46 | self._d.write_digital(1) 47 | 48 | def _data_cmd(self): 49 | self._start() 50 | self._write_byte(0x40) 51 | self._stop() 52 | 53 | def _dsp_ctrl(self): 54 | self._start() 55 | self._write_byte(0x88 | self._b) 56 | self._stop() 57 | 58 | def _write_byte(self, b): 59 | for i in range(8): 60 | self._d.write_digital((b >> i) & 1) 61 | self._c.write_digital(1) 62 | self._c.write_digital(0) 63 | self._c.write_digital(0) 64 | self._c.write_digital(1) 65 | self._c.write_digital(0) 66 | 67 | def brightness(self, val=None): 68 | if val is None: 69 | return self._b 70 | self._b = max(0, min(val, 7)) 71 | self._data_cmd() 72 | self._dsp_ctrl() 73 | 74 | def write(self, segments, pos=0): 75 | if not 0 <= pos <= 3: 76 | raise ValueError("Position out of range") 77 | self._data_cmd() 78 | self._start() 79 | self._write_byte(0xC0 | pos) 80 | for seg in segments: 81 | self._write_byte(seg) 82 | self._stop() 83 | self._dsp_ctrl() 84 | 85 | def encode_string(self, string): 86 | segments = bytearray(len(string)) 87 | for i in range(len(string)): 88 | segments[i] = self.encode_char(string[i]) 89 | return segments 90 | 91 | def encode_char(self, char): 92 | o = ord(char) 93 | if o == 32: 94 | return _SEG[36] # space 95 | if o == 42: 96 | return _SEG[38] # star/degrees 97 | if o == 45: 98 | return _SEG[37] # dash 99 | if o >= 65 and o <= 90: 100 | return _SEG[o-55] # uppercase A-Z 101 | if o >= 97 and o <= 122: 102 | return _SEG[o-87] # lowercase a-z 103 | if o >= 48 and o <= 57: 104 | return _SEG[o-48] # 0-9 105 | raise ValueError("Character out of range: {:d} '{:s}'".format(o, chr(o))) 106 | 107 | def hex(self, val): 108 | string = '{:04x}'.format(val & 0xffff) 109 | self.write(self.encode_string(string)) 110 | 111 | def number(self, num): 112 | num = max(-999, min(num, 9999)) 113 | string = '{0: >4d}'.format(num) 114 | self.write(self.encode_string(string)) 115 | 116 | def numbers(self, num1, num2, colon=True): 117 | num1 = max(-9, min(num1, 99)) 118 | num2 = max(-9, min(num2, 99)) 119 | segments = self.encode_string('{0:0>2d}{1:0>2d}'.format(num1, num2)) 120 | if colon: 121 | segments[1] |= 0x80 # colon on 122 | self.write(segments) 123 | 124 | def temperature(self, num): 125 | if num < -9: 126 | self.write([0x38, 0x3F]) # LO 127 | elif num > 99: 128 | self.write([0x76, 0x06]) # HI 129 | else: 130 | string = '{0: >2d}'.format(num) 131 | self.write(self.encode_string(string)) 132 | self.write([_SEG[38], _SEG[12]], 2) # degrees C 133 | 134 | def show(self, string, colon=False): 135 | segments = self.encode_string(string) 136 | if len(segments) > 1 and colon: 137 | segments[1] |= 128 138 | self.write(segments[:4]) 139 | 140 | def scroll(self, string, delay=250): 141 | segments = string if isinstance(string, list) else self.encode_string(string) 142 | data = [0] * 8 143 | data[4:0] = list(segments) 144 | for i in range(len(segments) + 5): 145 | self.write(data[0+i:4+i]) 146 | sleep(delay) 147 | --------------------------------------------------------------------------------