from machine import Pin
import time
import math
from neopixel import Neopixel
# 設定硬體參數
LED_PIN = 13 # NeoPixel 矩陣的資料輸入腳位
MATRIX_WIDTH = 8 # 矩陣寬度
MATRIX_HEIGHT = 8 # 矩陣高度
NUM_PIXELS = MATRIX_WIDTH * MATRIX_HEIGHT # LED 總數
# Create a NeoPixel object
pixels = Neopixel(NUM_PIXELS, 0, LED_PIN, "GRB")
# 清除所有 LED
def clear():
for i in range(64):
pixels.set_pixel(i, (0, 0, 0))
pixels.show()
# 根據矩陣走線方式轉換 (x, y) 為 LED index
def xy_to_index(x, y):
if y % 2 == 0:
return y * 8 + x
else:
return y * 8 + (7 - x)
# 顯示 x 軸與 y 軸
def draw_axes():
# x 軸:y = 0(藍色)
for x in range(8):
idx = xy_to_index(x, 0)
pixels.set_pixel(idx, (0, 0, 255))
# y 軸:x = 0(紅色)
for y in range(8):
idx = xy_to_index(0, y)
pixels.set_pixel(idx, (255, 0, 0))
# 顯示一元一次方程式 y = ax + b(綠色)
def draw_line(a, b, color=(0, 255, 0)):
for x in range(8):
y = a * x + b
if 0 <= y < 8:
idx = xy_to_index(x, int(y))
pixels.set_pixel(idx, color)
# 主程式
def plot_equation(a, b):
clear()
draw_axes()
draw_line(a, b)
pixels.show()
# 示例:y = 0.5x + 1
plot_equation(0.5, 1)