|
| 1 | +import board |
| 2 | +import displayio |
| 3 | +import adafruit_displayio_ssd1306 |
| 4 | +import terminalio |
| 5 | +import adafruit_ds3231 |
| 6 | +from adafruit_display_text import label |
| 7 | + |
| 8 | +displayio.release_displays() |
| 9 | + |
| 10 | +i2c = board.I2C() |
| 11 | +display_bus = displayio.I2CDisplay(i2c, device_address=0x3c) |
| 12 | +oled = adafruit_displayio_ssd1306.SSD1306(display_bus, width=128, height=32) |
| 13 | + |
| 14 | +rtc = adafruit_ds3231.DS3231(i2c) |
| 15 | + |
| 16 | + |
| 17 | +# The first time you run this code, you must set the time! |
| 18 | +# You must set year, month, date, hour, minute, second and weekday. |
| 19 | +# struct_time order: year, month, day (date), hour, minute, second, weekday , yearday, isdst |
| 20 | +# yearday is not supported, isdst can be set but we don't do anything with it at this time |
| 21 | + |
| 22 | +# UNCOMMENT THE FOLLOWING FOUR LINES THE FIRST TIME YOU RUN THE CODE TO SET THE TIME! |
| 23 | +# import time |
| 24 | +# set_time = time.struct_time((2019, 8, 16, 23, 59, 45, 4, -1, -1)) |
| 25 | +# print("Setting time to:", set_time) |
| 26 | +# rtc.datetime = set_time |
| 27 | + |
| 28 | +# Comment out the above four lines again after setting the time! |
| 29 | + |
| 30 | + |
| 31 | +font = terminalio.FONT |
| 32 | + |
| 33 | +while True: |
| 34 | + current = rtc.datetime |
| 35 | + |
| 36 | + hour = current.tm_hour % 12 |
| 37 | + if hour == 0: |
| 38 | + hour = 12 |
| 39 | + |
| 40 | + am_pm = "AM" |
| 41 | + if current.tm_hour / 12 >= 1: |
| 42 | + am_pm = "PM" |
| 43 | + |
| 44 | + time_display = "%d:%02d:%02d %s" % (hour, current.tm_min, current.tm_sec, am_pm) |
| 45 | + date_display = "%d/%d/%d" % (current.tm_mon, current.tm_mday, current.tm_year) |
| 46 | + text_display = "CircuitPython Time" |
| 47 | + |
| 48 | + clock = label.Label(font, text=time_display) |
| 49 | + date = label.Label(font, text=date_display) |
| 50 | + text = label.Label(font, text=text_display) |
| 51 | + |
| 52 | + (_, _, width, _) = clock.bounding_box |
| 53 | + clock.x = 128 // 2 - width // 2 |
| 54 | + clock.y = 5 |
| 55 | + |
| 56 | + (_, _, width, _) = date.bounding_box |
| 57 | + date.x = 128 // 2 - width // 2 |
| 58 | + date.y = 15 |
| 59 | + |
| 60 | + (_, _, width, _) = text.bounding_box |
| 61 | + text.x = 128 // 2 - width // 2 |
| 62 | + text.y = 25 |
| 63 | + |
| 64 | + watch_group = displayio.Group() |
| 65 | + watch_group.append(clock) |
| 66 | + watch_group.append(date) |
| 67 | + watch_group.append(text) |
| 68 | + |
| 69 | + oled.show(watch_group) |
0 commit comments