Skip to content

Instantly share code, notes, and snippets.

@c0nrad
Last active June 29, 2025 14:55
Show Gist options
  • Save c0nrad/ad15086d213dd5e1d04d42016d883f62 to your computer and use it in GitHub Desktop.
Save c0nrad/ad15086d213dd5e1d04d42016d883f62 to your computer and use it in GitHub Desktop.
from machine import Pin, I2C
from ssd1306 import SSD1306_I2C
import framebuf,sys
from writer import Writer
import time
SDA_PIN = 0
SCL_PIN = 1
UP_PIN = 16
DOWN_PIN = 17
BUTTON_PRESS = 0
up_button = Pin(UP_PIN, Pin.IN, Pin.PULL_UP)
down_button = Pin(DOWN_PIN, Pin.IN, Pin.PULL_UP)
def setup_oled() -> SSD1306_I2C:
global oled
i2c_dev = I2C(1,scl=Pin(27),sda=Pin(26),freq=200000) # start I2C on I2C1 (GPIO 26/27)
i2c_addr = [hex(ii) for ii in i2c_dev.scan()] # get I2C address in hex format
if i2c_addr==[]:
print('No I2C Display Found')
sys.exit()
else:
print("I2C Address : {}".format(i2c_addr[0])) # I2C device address
print("I2C Configuration: {}".format(i2c_dev)) # print I2C params
pix_res_x = 128 # SSD1306 horizontal resolution
pix_res_y = 64 # SSD1306 vertical resolution
oled = SSD1306_I2C(pix_res_x, pix_res_y, i2c_dev) # oled controller
oled.fill(0)
oled.text("SWU Counter v.4",5,5)
return oled
oled = setup_oled()
import swu48
wri = Writer(oled, swu48)
def loop() -> None:
wasUpActive = False
wasDownActive = False
pendingUpdate = True
score = 0
while True:
time.sleep(.01)
if pendingUpdate:
Writer.set_textpos(oled, 16, 32)
oled.fill(0)
oled.text("SWU Counter v.4",5,5)
wri.printstring(str(score))
oled.show()
pendingUpdate = False
if up_button.value() == BUTTON_PRESS:
if wasUpActive:
pass
else:
score += 1
wasUpActive = True
pendingUpdate = True
else:
wasUpActive = False
if down_button.value() == BUTTON_PRESS:
if wasDownActive:
pass
else:
score -= 1
wasDownActive = True
pendingUpdate = True
else:
wasDownActive = False
loop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment