Skip to content

Commit 99c9e8a

Browse files
committed
Adding Feather TFT I2C and TFT code.
1 parent f142568 commit 99c9e8a

File tree

8 files changed

+253
-0
lines changed

8 files changed

+253
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
"""
4+
CircuitPython Adafruit IO Example for LC709203 Sensor
5+
"""
6+
import time
7+
import ssl
8+
import alarm
9+
import board
10+
import digitalio
11+
import wifi
12+
import socketpool
13+
import adafruit_requests
14+
from adafruit_io.adafruit_io import IO_HTTP
15+
from adafruit_lc709203f import LC709203F, PackSize
16+
try:
17+
from secrets import secrets
18+
except ImportError:
19+
print("WiFi and Adafruit IO credentials are kept in secrets.py, please add them there!")
20+
raise
21+
22+
# Duration of sleep in seconds. Default is 600 seconds (10 minutes).
23+
# Feather will sleep for this duration between sensor readings / sending data to AdafruitIO
24+
sleep_duration = 600
25+
26+
# Update to match the mAh of your battery for more accurate readings.
27+
# Can be MAH100, MAH200, MAH400, MAH500, MAH1000, MAH2000, MAH3000.
28+
# Choose the closest match. Include "PackSize." before it, as shown.
29+
battery_pack_size = PackSize.MAH400
30+
31+
# Setup the little red LED
32+
led = digitalio.DigitalInOut(board.LED)
33+
led.switch_to_output()
34+
35+
# Set up the LC709203 sensor
36+
battery_monitor = LC709203F(board.I2C())
37+
battery_monitor.pack_size = battery_pack_size
38+
39+
# Collect the sensor data values and format the data
40+
battery_voltage = "{:.2f}".format(battery_monitor.cell_voltage)
41+
battery_percent = "{:.1f}".format(battery_monitor.cell_percent)
42+
43+
44+
def go_to_sleep(sleep_period):
45+
# Create a an alarm that will trigger sleep_period number of seconds from now.
46+
time_alarm = alarm.time.TimeAlarm(monotonic_time=time.monotonic() + sleep_period)
47+
# Exit and deep sleep until the alarm wakes us.
48+
alarm.exit_and_deep_sleep_until_alarms(time_alarm)
49+
50+
51+
# Send the data. Requires a feed name and a value to send.
52+
def send_io_data(feed, value):
53+
return io.send_data(feed["key"], value)
54+
55+
56+
# Wi-Fi connections can have issues! This ensures the code will continue to run.
57+
try:
58+
# Connect to Wi-Fi
59+
wifi.radio.connect(secrets["ssid"], secrets["password"])
60+
print("Connected to {}!".format(secrets["ssid"]))
61+
print("IP:", wifi.radio.ipv4_address)
62+
63+
pool = socketpool.SocketPool(wifi.radio)
64+
requests = adafruit_requests.Session(pool, ssl.create_default_context())
65+
66+
# Wi-Fi connectivity fails with error messages, not specific errors, so this except is broad.
67+
except Exception as e: # pylint: disable=broad-except
68+
print(e)
69+
go_to_sleep(60)
70+
71+
# Set your Adafruit IO Username and Key in secrets.py
72+
# (visit io.adafruit.com if you need to create an account,
73+
# or if you need your Adafruit IO key.)
74+
aio_username = secrets["aio_username"]
75+
aio_key = secrets["aio_key"]
76+
77+
# Initialize an Adafruit IO HTTP API object
78+
io = IO_HTTP(aio_username, aio_key, requests)
79+
80+
# Turn on the LED to indicate data is being sent.
81+
led.value = True
82+
# Print data values to the serial console. Not necessary for Adafruit IO.
83+
print("Current battery voltage: {0} V".format(battery_voltage))
84+
print("Current battery percent: {0} %".format(battery_percent))
85+
86+
# Adafruit IO sending can run into issues if the network fails!
87+
# This ensures the code will continue to run.
88+
try:
89+
print("Sending data to AdafruitIO...")
90+
# Send data to Adafruit IO
91+
send_io_data(io.create_and_get_feed("battery-voltage"), battery_voltage)
92+
send_io_data(io.create_and_get_feed("battery-percent"), battery_percent)
93+
print("Data sent!")
94+
# Turn off the LED to indicate data sending is complete.
95+
led.value = False
96+
97+
# Adafruit IO can fail with multiple errors depending on the situation, so this except is broad.
98+
except Exception as e: # pylint: disable=broad-except
99+
print(e)
100+
go_to_sleep(60)
101+
102+
go_to_sleep(sleep_duration)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
"""
4+
CircuitPython Simple Example for LC709203 Sensor
5+
"""
6+
import time
7+
import board
8+
from adafruit_lc709203f import LC709203F, PackSize
9+
10+
# Create sensor object, using the board's default I2C bus.
11+
battery_monitor = LC709203F(board.I2C())
12+
13+
# Update to match the mAh of your battery for more accurate readings.
14+
# Can be MAH100, MAH200, MAH400, MAH500, MAH1000, MAH2000, MAH3000.
15+
# Choose the closest match. Include "PackSize." before it, as shown.
16+
battery_monitor.pack_size = PackSize.MAH400
17+
18+
while True:
19+
print("Battery Percent: {:.2f} %".format(battery_monitor.cell_percent))
20+
print("Battery Voltage: {:.2f} V".format(battery_monitor.cell_voltage))
21+
time.sleep(2)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
"""
4+
CircuitPython GitHub Stars viewer
5+
"""
6+
import time
7+
import ssl
8+
import wifi
9+
import socketpool
10+
import displayio
11+
import board
12+
from adafruit_display_text import bitmap_label
13+
from adafruit_bitmap_font import bitmap_font
14+
import adafruit_requests
15+
16+
# Get WiFi details secrets.py file
17+
try:
18+
from secrets import secrets
19+
except ImportError:
20+
print("WiFi secrets are kept in secrets.py, please add them there!")
21+
raise
22+
23+
display = board.DISPLAY
24+
25+
# URL to fetch from
26+
JSON_STARS_URL = "https://api.github.com/repos/adafruit/circuitpython"
27+
28+
# Set up background image and text
29+
bitmap = displayio.OnDiskBitmap("/images/stars_background.bmp")
30+
tile_grid = displayio.TileGrid(bitmap, pixel_shader=bitmap.pixel_shader)
31+
group = displayio.Group()
32+
group.append(tile_grid)
33+
font = bitmap_font.load_font("/fonts/Arial-Bold-24.bdf")
34+
text_area = bitmap_label.Label(font, text="----", color=0xFFFFFF)
35+
text_area.x = 125
36+
text_area.y = 90
37+
group.append(text_area)
38+
display.show(group)
39+
40+
# Connect to WiFi
41+
print("Connecting to %s"%secrets["ssid"])
42+
wifi.radio.connect(secrets["ssid"], secrets["password"])
43+
print("Connected to %s!"%secrets["ssid"])
44+
print("My IP address is", wifi.radio.ipv4_address)
45+
46+
pool = socketpool.SocketPool(wifi.radio)
47+
requests = adafruit_requests.Session(pool, ssl.create_default_context())
48+
49+
while True:
50+
print("Fetching and parsing json from", JSON_STARS_URL)
51+
response = requests.get(JSON_STARS_URL)
52+
stars = response.json()["stargazers_count"]
53+
print("-" * 40)
54+
print("CircuitPython GitHub Stars", stars)
55+
print("-" * 40)
56+
text_area.text = str(stars)
57+
time.sleep(120)
Binary file not shown.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# SPDX-FileCopyrightText: 2021 Tim C for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
"""
4+
CircuitPython simple text display demo
5+
"""
6+
import board
7+
import terminalio
8+
from adafruit_display_text import bitmap_label
9+
10+
# Update this to change the text displayed.
11+
text = "Hello, World!"
12+
# Update this to change the size of the text displayed. Must be a whole number.
13+
scale = 3
14+
15+
text_area = bitmap_label.Label(terminalio.FONT, text=text, scale=scale)
16+
text_area.x = 10
17+
text_area.y = 10
18+
board.DISPLAY.show(text_area)
19+
20+
while True:
21+
pass
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# SPDX-FileCopyrightText: 2021 Tim C for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
"""
4+
CircuitPython simple bitmap display demo
5+
"""
6+
import board
7+
from displayio import OnDiskBitmap, TileGrid, Group
8+
9+
main_group = Group()
10+
blinka_img = OnDiskBitmap("images/adafruit_blinka.bmp")
11+
12+
tile_grid = TileGrid(bitmap=blinka_img, pixel_shader=blinka_img.pixel_shader)
13+
main_group.append(tile_grid)
14+
15+
board.DISPLAY.show(main_group)
16+
17+
tile_grid.x = board.DISPLAY.width // 2 - blinka_img.width // 2
18+
19+
while True:
20+
pass
Binary file not shown.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# SPDX-FileCopyrightText: 2021 Tim C for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
"""
4+
CircuitPython simple sensor data display demo using LC709203 battery monitor and TFT display
5+
"""
6+
import time
7+
import board
8+
import terminalio
9+
from displayio import Group
10+
from adafruit_display_text import bitmap_label
11+
from adafruit_lc709203f import LC709203F
12+
13+
text_area = bitmap_label.Label(terminalio.FONT, scale=2)
14+
text_area.anchor_point = (0.5, 0.5)
15+
text_area.anchored_position = (board.DISPLAY.width // 2, board.DISPLAY.height // 2)
16+
17+
main_group = Group()
18+
19+
main_group.append(text_area)
20+
21+
print("LC709203F test")
22+
print("Make sure LiPoly battery is plugged into the board!")
23+
24+
sensor = LC709203F(board.I2C())
25+
26+
print("IC version:", hex(sensor.ic_version))
27+
28+
board.DISPLAY.show(main_group)
29+
30+
while True:
31+
text_area.text = "Battery:\n{:.1f} Volts \n{}%".format(sensor.cell_voltage, sensor.cell_percent)
32+
time.sleep(1)

0 commit comments

Comments
 (0)