init
This commit is contained in:
115
main.py
Normal file
115
main.py
Normal file
@@ -0,0 +1,115 @@
|
||||
from machine import I2C, Pin
|
||||
import ssd1306, credentials
|
||||
from utime import sleep_ms, time
|
||||
|
||||
# setup i2c and pin for counter interrupt
|
||||
i2c = I2C(sda=Pin(4), scl=Pin(5))
|
||||
count_pin = Pin(14, Pin.IN, Pin.PULL_UP)
|
||||
|
||||
|
||||
# Constants
|
||||
COUNTS = 0
|
||||
COUNT_ROLLING = []
|
||||
LOCAL_IP = ""
|
||||
|
||||
|
||||
LOGO = [[0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0],
|
||||
[0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0],
|
||||
[0,0,1,1,1,1,1,0,0,0,0,0,0,0,1,1,1,1,1,0,0],
|
||||
[0,1,1,1,1,1,1,1,0,0,0,0,0,0,1,1,1,1,1,1,0],
|
||||
[0,1,1,1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,1,1,1],
|
||||
[1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1],
|
||||
[1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1],
|
||||
[1,1,1,1,1,1,1,1,0,0,1,0,0,1,1,1,1,1,1,1,1],
|
||||
[0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0],
|
||||
[0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0],
|
||||
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
|
||||
[0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0],
|
||||
[0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0],
|
||||
[0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0],
|
||||
[0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0],
|
||||
[0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0],
|
||||
[0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0],
|
||||
[0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0],
|
||||
[0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0]]
|
||||
|
||||
|
||||
# SSD1306_I2C(width, height, i2c-object, address)
|
||||
display = ssd1306.SSD1306_I2C(128, 64, i2c)
|
||||
display.fill(0) # Fill Black
|
||||
display.show() # show on display
|
||||
|
||||
# Offset for the 5 rows of text
|
||||
# 0, 12, 24, 36, 48
|
||||
|
||||
|
||||
|
||||
# Function to connect to network, look at my hardcoded credientals.
|
||||
def do_connect():
|
||||
global LOCAL_IP
|
||||
import network
|
||||
wlan = network.WLAN(network.STA_IF)
|
||||
wlan.active(True)
|
||||
if not wlan.isconnected():
|
||||
#print('connecting to network...')
|
||||
wlan.connect(credentials.WLAN_SSID, credentials.WLAN_PASS)
|
||||
while not wlan.isconnected():
|
||||
pass
|
||||
LOCAL_IP = str(wlan.ifconfig()[0])
|
||||
|
||||
def draw_logo(x_off, y_off):
|
||||
for row in range(len(LOGO)):
|
||||
for px in range(len(LOGO[row])):
|
||||
display.pixel(px+y_off, row+x_off, LOGO[row][px])
|
||||
|
||||
# Configure interrupt
|
||||
def irq_handler(p):
|
||||
global COUNTS, COUNT_ROLLING
|
||||
COUNTS += 1
|
||||
COUNT_ROLLING.append(time())
|
||||
draw_logo(24, 100)
|
||||
display.show()
|
||||
|
||||
|
||||
def draw_bar_graph(fill_grade, offset = 36):
|
||||
# Clamp size to display size
|
||||
if fill_grade > 128:
|
||||
fill_grade = 128
|
||||
if fill_grade < 0:
|
||||
fill_grade = 0
|
||||
# Draw border
|
||||
display.rect(0, offset, 128, 10, 1)
|
||||
# Draw filling
|
||||
display.fill_rect(0, offset+1, fill_grade, 8, 1)
|
||||
return True
|
||||
|
||||
|
||||
def setup():
|
||||
count_pin.irq(handler=irq_handler, trigger=Pin.IRQ_FALLING)
|
||||
do_connect()
|
||||
|
||||
|
||||
def main():
|
||||
setup()
|
||||
peak = 0
|
||||
CPM = 0
|
||||
while True:
|
||||
display.fill(0)
|
||||
# Calculate CPM from rolling average of 20s
|
||||
for i in COUNT_ROLLING:
|
||||
if i < (time() - 20):
|
||||
COUNT_ROLLING.remove(i)
|
||||
CPM = len(COUNT_ROLLING)*3
|
||||
# Calculate new peak CPM
|
||||
peak = CPM if CPM > peak else peak
|
||||
display.text(LOCAL_IP ,0,0,1)
|
||||
display.text("Counts: " + str(COUNTS), 0, 12, 1)
|
||||
display.text("CPM: " + str(CPM), 0, 24, 1)
|
||||
display.text("Peak: " + str(peak), 0, 36, 1)
|
||||
draw_bar_graph(CPM, 48)
|
||||
for i in range(49, 58, 2):
|
||||
display.pixel(peak, i, 1)
|
||||
display.show()
|
||||
sleep_ms(300)
|
||||
|
||||
main()
|
||||
Reference in New Issue
Block a user