from machine import Pin, I2C, ADC
import ssd1306
import framebuf
import time
import random
# I2C setup for OLED
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
# Bitmap data
bitmap13 = bytearray([
0x00, 0x7e, 0x00, 0x01, 0xc3, 0x80, 0x03, 0x00, 0xc0, 0x46, 0x14, 0x66,
0x34, 0x7e, 0x2e, 0x1c, 0xff, 0x28, 0x0c, 0xbd, 0x30, 0xec, 0x99, 0x37,
0x0c, 0xdb, 0x30, 0x14, 0xdb, 0x28, 0x76, 0x7e, 0x6e, 0x42, 0x00, 0x42,
0x03, 0x00, 0xc0, 0x01, 0x3d, 0x80, 0x01, 0xa5, 0x80, 0x03, 0xff, 0xc0,
0x03, 0xff, 0xc0, 0x03, 0xff, 0xc0, 0x01, 0xff, 0x80, 0x01, 0xff, 0x80,
0x01, 0xff, 0x80, 0x00, 0xfe, 0x00, 0x00, 0x42, 0x00, 0x00, 0x7e, 0x00
])
# Keypad setup
rows = [Pin(x, Pin.IN, Pin.PULL_UP) for x in [4, 5, 18, 19]]
cols = [Pin(x, Pin.OUT) for x in [23, 22, 13, 12]]
keys = [
['1', '2', '3', 'A'],
['4', '5', '6', 'B'],
['7', '8', '9', 'C'],
['*', '0', '#', 'D']
]
def scan_keypad():
for col_num, col in enumerate(cols):
col.value(0)
for row_num, row in enumerate(rows):
if row.value() == 0:
col.value(1)
return keys[row_num][col_num]
col.value(1)
return None
# Joystick setup
x_pin = ADC(Pin(32))
y_pin = ADC(Pin(33))
button = Pin(25, Pin.IN, Pin.PULL_UP)
# I2C setup for communication with secondary ESP32
i2c_secondary = I2C(1, scl=Pin(22), sda=Pin(21))
def send_data_to_secondary(data):
i2c_secondary.writeto(0x42, data)
def draw_bitmap(x, y, bitmap, w, h):
for j in range(h):
for i in range(w):
byte_index = (j * w + i) // 8
bit_index = 7 - (i % 8)
if bitmap[byte_index] & (1 << bit_index):
oled.pixel(x + i, y + j, 1)
def show_welcome_screen():
oled.fill(0) # Clear the display
oled.text('WELCOME TO', 36, 12)
oled.text('BRAIN TEASER', 19, 22)
draw_bitmap(50, 36, bitmap13, 24, 24)
oled.show()
def random_operation():
operations = ['+', '-', '*']
return random.choice(operations)
def generate_question():
num1 = random.randint(1, 10)
num2 = random.randint(1, 10)
operation = random_operation()
question = f"{num1} {operation} {num2} = ?"
answer = eval(f"{num1}{operation}{num2}")
return question, answer
def show_question(question, user_input=""):
oled.fill(0) # Clear the display
display_question = question.replace("?", user_input)
oled.text(display_question, 10, 26)
oled.show()
def show_feedback(feedback):
oled.fill(0)
oled.text(feedback, 40, 26)
oled.show()
time.sleep(2)
# Show the welcome screen for 3 seconds
show_welcome_screen()
time.sleep(3)
# Main loop to display math questions and handle input
while True:
question, correct_answer = generate_question()
user_input = ""
show_question(question, user_input)
start_time = time.time()
while time.time() - start_time < 10:
key = scan_keypad()
if key:
if key == '#':
if user_input.isdigit() and int(user_input) == correct_answer:
show_feedback("Correct!")
else:
show_feedback("Wrong!")
break
elif key == '*':
user_input = ""
else:
user_input += key
show_question(question, user_input)
time.sleep(0.1)
x_val = x_pin.read()
y_val = y_pin.read()
button_val = button.value()
joystick_data = f"{x_val},{y_val},{button_val}"
send_data_to_secondary(joystick_data.encode())