from machine import LCD, Pin, Map, ADC
# from math import cos, sin
import math
from time import sleep
# ประกาศ
lcd = LCD()
light = ADC(Pin(Map.WIO_LIGHT, Pin.IN))
# ฟังก์ชั่นแสดงข้อความ
def stext(txt, px1, py1, c1, b1):
lcd.setTextColor(c1, b1)
px1 = int((320 - (len(txt) * 14)) / 2)
lcd.drawString(txt+" ", px1, py1)
# ฟังก์ชั่นแสดงเข็มมิเตอร์
def needle(xx, yy, valu1):
x1 = xx + int(125*math.cos((valu1 - 90) * 0.0174532925))
y1 = yy + int(125*math.sin((valu1 - 90) * 0.0174532925))
x0 = xx
y0 = yy
lcd.drawLine(x0+1, y0, x1+1, y1, lcd.color.BLACK)
lcd.drawLine(x0, y0, x1, y1, lcd.color.BLACK)
sleep(0.05)
lcd.drawLine(x0+1, y0, x1+1, y1, lcd.color.LIGHTGREY)
lcd.drawLine(x0, y0, x1, y1, lcd.color.LIGHTGREY)
# ฟังก์ชั่นแสดงหน้าปัดมิเตอร์ พร้อมสเกล
def meter1():
lcd.fillScreen(lcd.color.YELLOW)
lcd.fillRect(5, 3, 310, 180, lcd.color.LIGHTGREY)
lcd.drawRect(5, 3, 310, 180, lcd.color.BLACK)
stext(" LIGHT METER", 0, 10, lcd.color.BLACK, lcd.color.LIGHTGREY)
lcd.setTextSize(1)
lcd.setTextColor(lcd.color.BLACK, lcd.color.YELLOW)
lcd.drawString("plakemphet.blogspot.com", 10, 230)
# draw ticks every 5 degrees from -50 to +50 degrees
for i in range(-60, 61, 5):
# long scale tick length
tl = 20
# coordinates of tick to draw
sx = math.cos((i - 90) * 0.0174532925)
sy = math.sin((i - 90) * 0.0174532925)
x0 = int(sx * (133 + tl) + 160)
y0 = int(sy * (133 + tl) + 186)
x1 = int(sx * 133 + 160)
y1 = int(sy * 133 + 186)
# coordinates of next tick for zone fill
sx2 = math.cos((i + 5 - 90) * 0.0174532925)
sy2 = math.sin((i + 5 - 90) * 0.0174532925)
x2 = int(sx2 * (133 + tl) + 160)
y2 = int(sy2 * (133 + tl) + 186)
x3 = int(sx2 * 133 + 160)
y3 = int(sy2 * 133 + 186)
# Green zone limits
if (i >= -60 and i < 0):
lcd.fillTriangle(x0, y0, x1, y1, x2, y2, lcd.color.GREEN)
lcd.fillTriangle(x1, y1, x2, y2, x3, y3, lcd.color.GREEN)
# Yellow zone limits
if (i >= 0 and i < 25):
lcd.fillTriangle(x0, y0, x1, y1, x2, y2, lcd.color.YELLOW)
lcd.fillTriangle(x1, y1, x2, y2, x3, y3, lcd.color.YELLOW)
# Red zone limits
if (i >= 25 and i < 60):
lcd.fillTriangle(x0, y0, x1, y1, x2, y2, lcd.color.RED)
lcd.fillTriangle(x1, y1, x2, y2, x3, y3, lcd.color.RED)
# short scale tick length
if (i % 25 != 0):
tl = 8
# recalculate coordinates incase tick length has changed
x0 = sx * (133 + tl) + 160
y0 = sy * (133 + tl) + 186
x1 = sx * 133 + 160
y1 = sy * 133 + 186
# ส่วนโปรแกรมหลัก
def main():
value1 = 0
meter1()
while True:
for i in range(10):
value1 = light.read() + value1
value1 = int(value1/10) # เฉลี่ย 10 ครั้ง max = 170
value1 = value1 - 55
# แสดงข้อความบนจอ
lcd.setTextSize(3)
stext(" " + str(value1 + 60) + " ", 140, 200, lcd.color.BLUE, lcd.color.YELLOW)
lcd.setTextSize(1)
needle(160, 180, value1)
if __name__ == "__main__":
main()