from machine import Pin, SoftI2C
import time
from ir_rx import NEC_16
from ssd1306 import SSD1306_I2C
# button array
buttons = {
0xa2:"POWER",
0xe2:"MENU",
0x22:"TEST",
0x20:"PLUS",
0xc2:"BACK",
0xe0:"PREV",
0xa8:"PLAY",
0x90:"NEXT",
0x68:"0",
0x98:"MINUS",
0xb0:"C",
0x30:"1",
0x18:"2",
0x7a:"3",
0x10:"4",
0x38:"5",
0x5a:"6",
0x42:"7",
0x4a:"8",
0x52:"9",
}
# OLED screen configuration
i2c = SoftI2C(scl=Pin(18), sda=Pin(19))
oled = SSD1306_I2C(128, 64, i2c)
# global variables
keep_running = True
option = "0"
icon = [
[1,1,0,1,0,1,1],
[1,0,1,1,1,0,1],
[1,0,1,1,1,0,1],
[1,0,1,1,1,0,1],
[1,0,0,1,0,0,1],
[1,0,0,0,0,0,1],
[1,1,0,1,0,1,1],
]
scale = 1
# Function to scale the alert matrix by a specific factor
def scale_matrix(matrix, factor):
output = []
for row in matrix:
new_row = []
for value in row:
new_row.extend([value] * factor)
for _ in range(factor):
output.append(list(new_row))
return output
# Function to draw a pixel matrix on the OLED at a specific position
def draw_symbol(oled, originx, originy, pic):
x = originx
y = originy
for r in range(len(pic)):
for c in range(len(pic[r])):
oled.pixel(x,y,pic[r][c])
x += 1
x = originx
y += 1
# program functions
def menu():
global oled
oled.fill(0)
oled.text("IR control menu",0,0)
oled.text("****************",0,10)
oled.text("1.- view keys",0,20)
oled.text("2.- scale icon",0,30)
oled.text("3.- view info.",0,40)
oled.text("4.- exit",0,50)
oled.show()
def show_buttons(data):
global oled
oled.fill(0)
oled.text("detecting IR...",0,0)
if data in buttons.keys():
oled.text(f"button: {buttons[data]}",0,12)
oled.text(f"code: {hex(data)}",0,24)
oled.show()
def show_icon(data):
global oled, scale, icon
oled.fill(0)
if buttons[data] != "PREV" and buttons[data] != "NEXT":
draw_symbol(oled, 0, 0, scale_matrix(icon, scale))
oled.show()
return
if buttons[data] == "NEXT":
scale = scale + 1 if (len(icon)*scale) < 64 else scale
if buttons[data] == "PREV":
scale = scale - 1 if scale > 1 else scale
draw_symbol(oled, 0, 0, scale_matrix(icon, scale))
oled.show()
def show_info():
global oled
oled.fill(0)
oled.text('Programmable Systems', 0, 0)
oled.text('ISC', 0, 10)
oled.text('Team:', 0, 20)
oled.text('Samuel J. Juarez B.', 0, 30)
oled.text('Ivan A. Cadena L.', 0, 40)
oled.text('22/09/2025', 0, 50)
oled.show()
def farewell():
global oled
oled.fill(0)
oled.text("Shutting down",0,0)
oled.text("system...",0,10)
oled.show()
# IR callback
def ir_callback(data, addr, ctrl):
global option
if option == "0":
option = buttons[data]
if buttons[data] == "BACK":
option = "0"
menu()
return
if option == "1":
show_buttons(data)
return
if option == "2":
show_icon(data)
return
if option == "3":
show_info()
return
if option == "4":
global keep_running
keep_running = False
farewell()
return
# IR receiver instance
receiver = NEC_16(Pin(21, Pin.IN), callback=ir_callback)
# show menu
menu()
# main loop
while keep_running:
time.sleep(0.5)