SHOW:
|
|
- or go back to the newest paste.
| 1 | from microbit import * | |
| 2 | import neopixel | |
| 3 | import speech | |
| 4 | import music | |
| 5 | np = neopixel.NeoPixel(pin1, 72) | |
| 6 | ||
| 7 | a = 0x68 | |
| 8 | ||
| 9 | def bcd2bin(value, mask): | |
| 10 | units = value & 0x0F | |
| 11 | tens = (value & mask) >> 4 | |
| 12 | return tens * 10 + units | |
| 13 | ||
| 14 | def bin2bcd(value): | |
| 15 | return (value or 0) + 6 * ((value or 0) // 10) | |
| 16 | ||
| 17 | def setup_rtc(): | |
| 18 | i2c.write(a, b'\x00') | |
| 19 | cs = i2c.read(a,1)[0] | |
| 20 | i2c.write(a, b'\x07\x43') | |
| 21 | i2c.write(a, b'\x03') | |
| 22 | cwd = i2c.read(a,1)[0] | |
| 23 | i2c.write(a,bytes([0x03,0x1F | cwd])) | |
| 24 | i2c.write(a,bytes([0x00,0x80 | cs])) | |
| 25 | ||
| 26 | def get_time(): | |
| 27 | i2c.write(a, b'\x00') | |
| 28 | buf = i2c.read(a,7) | |
| 29 | ms = [0x70,0x70,0x30,0x10,0x30,0x10,0xF0] | |
| 30 | tm = [bcd2bin(buf[i],ms[i]) for i in range(7)] | |
| 31 | tm[3] -= 8 | |
| 32 | return tm | |
| 33 | ||
| 34 | def set_time(t): | |
| 35 | i2c.write(a, b'\x00') | |
| 36 | i2c.write(a, b'\x00\x00') | |
| 37 | t[3] = 0x1f | t[3] | |
| 38 | for i in range(1,7): | |
| 39 | i2c.write(a,bytes([i,bin2bcd(t[i])])) | |
| 40 | i2c.write(a,bytes([0,0x80 | bin2bcd(t[0])])) | |
| 41 | ||
| 42 | setup_rtc() | |
| 43 | ||
| 44 | hours = get_time()[2] | |
| 45 | minutes = get_time()[1] | |
| 46 | while True: | |
| 47 | if button_a.is_pressed() and button_b.is_pressed(): | |
| 48 | display.scroll("H?")
| |
| 49 | while not button_b.is_pressed(): | |
| 50 | if button_a.is_pressed(): | |
| 51 | hours+=1 | |
| 52 | if hours > 23: | |
| 53 | hours=0 | |
| 54 | display.scroll(hours) | |
| 55 | display.scroll("M?")
| |
| 56 | while not button_b.is_pressed(): | |
| 57 | if button_a.is_pressed(): | |
| 58 | minutes+=5 | |
| 59 | if minutes > 55: | |
| 60 | minutes=0 | |
| 61 | display.scroll(minutes) | |
| 62 | display.scroll(hours) | |
| 63 | display.scroll(":")
| |
| 64 | display.scroll(minutes) | |
| 65 | set_time([0,minutes,hours,1,17,3,19]) | |
| 66 | ||
| 67 | np.clear() | |
| 68 | ||
| 69 | if hours >= 12: | |
| 70 | for pixel_id in range(7, 8): | |
| 71 | np[pixel_id] = (0, 0, 255) | |
| 72 | else: | |
| 73 | for pixel_id in range(6, 7): | |
| 74 | np[pixel_id] = (0, 0, 255) | |
| 75 | #set oclock, past, to | |
| 76 | if minutes > 57 or minutes < 3: | |
| 77 | for pixel_id in range(0, 6): | |
| 78 | np[pixel_id] = (0, 255, 0) | |
| 79 | elif minutes > 2 and minutes < 33: | |
| 80 | for pixel_id in range(43, 47): | |
| 81 | np[pixel_id] = (255, 0, 0) | |
| 82 | elif minutes > 32 and minutes < 58: | |
| 83 | hours += 1 | |
| 84 | for pixel_id in range(42, 44): | |
| 85 | np[pixel_id] = (0, 0, 255) | |
| 86 | ||
| 87 | if hours >=12: | |
| 88 | hours -= 12 | |
| 89 | ||
| 90 | if hours == 0: | |
| 91 | for pixel_id in range(16, 20): | |
| 92 | np[pixel_id] = (255, 0, 0) | |
| 93 | for pixel_id in range(21, 23): | |
| 94 | np[pixel_id] = (255, 0, 0) | |
| 95 | elif hours == 1: | |
| 96 | for pixel_id in range(8, 10): | |
| 97 | np[pixel_id] = (0, 255, 0) | |
| 98 | for pixel_id in range(14, 15): | |
| 99 | np[pixel_id] = (0, 255, 0) | |
| 100 | elif hours == 2: | |
| 101 | for pixel_id in range(16, 18): | |
| 102 | np[pixel_id] = (0, 0, 255) | |
| 103 | for pixel_id in range(14, 15): | |
| 104 | np[pixel_id] = (0, 0, 255) | |
| 105 | elif hours == 3: | |
| 106 | for pixel_id in range(24, 29): | |
| 107 | np[pixel_id] = (0, 0, 255) | |
| 108 | elif hours == 4: | |
| 109 | for pixel_id in range(12, 16): | |
| 110 | np[pixel_id] = (255, 255, 0) | |
| 111 | elif hours == 5: | |
| 112 | for pixel_id in range(32, 36): | |
| 113 | np[pixel_id] = (255, 165, 0) | |
| 114 | elif hours == 6: | |
| 115 | for pixel_id in range(29, 32): | |
| 116 | np[pixel_id] = (255, 20, 147) | |
| 117 | elif hours == 7: | |
| 118 | for pixel_id in range(31, 32): | |
| 119 | np[pixel_id] = (160, 32, 240) | |
| 120 | for pixel_id in range(20, 24): | |
| 121 | np[pixel_id] = (160, 32, 240) | |
| 122 | elif hours == 8: | |
| 123 | for pixel_id in range(35, 40): | |
| 124 | np[pixel_id] = (0, 0, 205) | |
| 125 | elif hours == 9: | |
| 126 | for pixel_id in range(8, 12): | |
| 127 | np[pixel_id] = (50, 205, 50) | |
| 128 | elif hours == 10: | |
| 129 | for pixel_id in range(28, 29): | |
| 130 | np[pixel_id] = (255, 0, 0) | |
| 131 | for pixel_id in range(22, 24): | |
| 132 | np[pixel_id] = (255, 0, 0) | |
| 133 | elif hours == 11: | |
| 134 | for pixel_id in range(18, 24): | |
| 135 | np[pixel_id] = (0, 255, 255) | |
| 136 | ||
| 137 | if (minutes > 2 and minutes < 8) or (minutes > 52 and minutes < 58): | |
| 138 | for pixel_id in range(48, 52): | |
| 139 | np[pixel_id] = (255, 255, 0) | |
| 140 | elif (minutes > 7 and minutes < 13) or (minutes > 47 and minutes < 53): | |
| 141 | for pixel_id in range(65, 66): | |
| 142 | np[pixel_id] = (0, 255, 255) | |
| 143 | for pixel_id in range(67, 69): | |
| 144 | np[pixel_id] = (0, 255, 255) | |
| 145 | elif (minutes > 12 and minutes < 18) or (minutes > 42 and minutes < 48): | |
| 146 | for pixel_id in range(57, 64): | |
| 147 | np[pixel_id] = (50, 205, 50) | |
| 148 | elif (minutes > 17 and minutes < 23) or (minutes > 37 and minutes < 43): | |
| 149 | for pixel_id in range(65, 71): | |
| 150 | np[pixel_id] = (255, 0, 255) | |
| 151 | elif (minutes > 22 and minutes < 28) or (minutes > 32 and minutes < 38): | |
| 152 | for pixel_id in range(65, 71): | |
| 153 | np[pixel_id] = (255, 255, 255) | |
| 154 | for pixel_id in range(48, 52): | |
| 155 | np[pixel_id] = (255, 255, 255) | |
| 156 | elif (minutes > 27 and minutes < 33): | |
| 157 | for pixel_id in range(52, 56): | |
| 158 | np[pixel_id] = (255, 165, 0) | |
| 159 | elif minutes == 0: | |
| 160 | sleep(1) | |
| 161 | music.play(music.PUNCHLINE) | |
| 162 | for i in range (0,hours): | |
| 163 | sleep(1500) | |
| 164 | speech.say("chiss", speed=120, pitch=100, throat=100, mouth=200)
| |
| 165 | ||
| 166 | np.show() | |
| 167 | #display.scroll(hours) | |
| 168 | #display.scroll(":")
| |
| 169 | #display.scroll(minutes) | |
| 170 | sleep(200) |