from machine import Pin, PWM, SoftI2C
import time ,ssd1306 ,random ,math
i2c = SoftI2C(scl=Pin(22), sda=Pin(21))
#
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
#ปุ่มกดสีเหลืองใช้วัดค่าวัตถุสีต้นแบบ
btn_yellow = Pin(18, Pin.IN, Pin.PULL_UP)
btn_yellow_state = 1 #default (no push)
#ปุ่มกดสีเขียวใช้วัดค่าสีวัตถุเป้าหมาย
btn_green = Pin(2, Pin.IN, Pin.PULL_UP)
btn_green_state = 1 #default (no push)
#ปุ่มกดสีน้ำเงินใช้วิเคราะห์ค่าสี โดยใช้สูตรคำนวณ ของค่าสีวัตถุสีต้นแบบกับค่าสีวัตถุเป้าหมาย
btn_blue = Pin(4, Pin.IN, Pin.PULL_UP)
btn_blue_state = 1 #default (no push)
#ปุ่มกดสีน้ำเงินใช้วิเคราะห์ค่าสี โดยใช้สูตรคำนวณ ของค่าสีวัตถุสีต้นแบบกับค่าสีวัตถุเป้าหมาย แบบ delta
btn_gray = Pin(19, Pin.IN, Pin.PULL_UP)
btn_gray_state = 1 #default (no push)
#ปุ่มกดสีแดง ใช้รีเซต
btn_red = Pin(23, Pin.IN, Pin.PULL_UP)
btn_red_state = 1 #default (no push)
#Update OLED Display
def update_oled():
#ล้างหน้าจอ
oled.fill(0)
#text line 1
global color_prototype
len_cp = len(color_prototype)
print("str(color_prototype) = "+str(color_prototype))
print("len_cp = "+str(len_cp))
if len_cp != 0 :
oled.text("RGB1"+str(color_prototype).replace(" ",""), 0, 0)
else :
oled.text("RGB1[-,-,-]", 0, 0)
#text line 2 to 4
global color
len_c = len(color)
print("str(color) = "+str(color))
print("len_c = "+str(len_c))
if len_c > 0 :
oled.text("RGB2"+str(color[0]).replace(" ",""), 0, 10)
if len_c > 1 :
oled.text("RGB2"+str(color[1]).replace(" ",""), 0, 20)
if len_c > 2 :
oled.text("RGB2"+str(color[2]).replace(" ",""), 0, 30)
else :
oled.text("RGB2[-,-,-]", 0, 30)
else :
oled.text("RGB2[-,-,-]", 0, 20)
oled.text("RGB2[-,-,-]", 0, 30)
else :
oled.text("RGB2[-,-,-]", 0, 10)
oled.text("RGB2[-,-,-]", 0, 20)
oled.text("RGB2[-,-,-]", 0, 30)
#text line 5
if len_c == 3 :
avg_rgb = avg_rgb_color()
print("avg_rgb = "+str(avg_rgb))
oled.text("AVG"+str(avg_rgb).replace(" ",""), 0, 40)
else :
oled.text("AVG[-,-,-]", 0, 40)
#text line 6
oled.text("Push Button ...", 0, 50)
for i in range(3):
oled.poweroff()
time.sleep(0.5)
oled.poweron()
time.sleep(0.5)
print(i)
oled.show()
def read_color_prototype(rgb) :
global color_prototype
color_prototype = rgb
def read_color(rgb) :
global color
print("color (before) = "+str(color))
len_c = len(color)
if len_c > 0 :
color.append(rgb)
if len_c == 3 :
color.pop(0)
else :
color = [rgb]
print("color (now) = "+str(color))
def avg_rgb_color() :
sum_r = 0
sum_g = 0
sum_b = 0
global color
for c in color :
r = c[0]
g = c[1]
b = c[2]
sum_r += r
sum_g += g
sum_b += b
avg_r = int(sum_r/3)
avg_g = int(sum_g/3)
avg_b = int(sum_b/3)
return [avg_r, avg_g, avg_b]
def random_rgb() :
red = random.randint(0,255)
green = random.randint(0,255)
blue = random.randint(0,255)
return [red, green, blue]
def clear() :
init()
oled.fill(0)
oled.text("RGB1[-,-,-]",0,0)
oled.text("RGB2[-,-,-]",0,10)
oled.text("RGB2[-,-,-]",0,20)
oled.text("RGB2[-,-,-]",0,30)
oled.text("AVG[-,-,-]",0,40)
oled.text("Push Button ...",0,50)
oled.show()
def init() :
global color_prototype
global color
color_prototype = []
color = []
def analyze1():
global avg_rgb
global color_prototype
avg_rgb = avg_rgb_color()
oled.fill(0)
oled.text("RGB1"+str(color_prototype).replace(" ",""),0,0)
oled.text("AVG"+str(avg_rgb).replace(" ",""),0,10)
if (abs(avg_rgb[0]-color_prototype[0])<=10 and abs(avg_rgb[1]-color_prototype[1])<=10 and abs(avg_rgb[2]-color_prototype[2]) <=10) :
oled.text("Analyze 1",0,30)
oled.text("Identical",0,40)
else:
oled.text("Analyze 1",0,30)
oled.text("Different",0,40)
oled.show()
def analyze():
global color_prototype
global avg_rgb
avg_rgb = avg_rgb_color()
oled.fill(0)
oled.text("RGB1"+str(color_prototype).replace(" ",""),0,0)
oled.text("AVG"+str(avg_rgb).replace(" ",""),0,10)
rgb = int(math.sqrt(pow(avg_rgb[0]-color_prototype[0],2)+pow(avg_rgb[1]-color_prototype[1],2)+pow(avg_rgb[2]-color_prototype[2],2)))
print("rgb delta = ",rgb)
if rgb<=10:
oled.text("Analyze 2",0,30)
oled.text("Identical",0,40)
else:
oled.text("Analyze 2",0,30)
oled.text("Different",0,40)
oled.show()
if __name__ == "__main__":
init()
update_oled()
while True:
btn1_last_state = btn1_state
btn1_state = btn1.value()
#print("btn1_state/last = "+str(btn1_state)+"/"+str(btn1_last_state))
btn2_last_state = btn2_state
btn2_state = btn2.value()
#print("btn2_state/last = "+str(btn2_state)+"/"+str(btn2_last_state))
btn3_last_state = btn3_state
btn3_state = btn3.value()
#print("btn3_state/last = "+str(btn3_state)+"/"+str(btn3_last_state))
btn4_last_state = btn4_state
btn4_state = btn4.value()
#print("btn4_state/last = "+str(btn4_state)+"/"+str(btn4_last_state))
btn5_last_state = btn5_state
btn5_state = btn5.value()
#print("btn5_state/last = "+str(btn5_state)+"/"+str(btn5_last_state))
#Push Botton 1
if btn1_state == 0 and btn1_last_state == 1 :
print("Push Button 1 (Yellow)")
read_color_prototype(random_rgb())
update_oled()
#Push Botton 2
if btn2_state == 0 and btn2_last_state == 1 :
print("Push Button 2 (Green)")
read_color(random_rgb())
update_oled()
#Push Botton 3
if btn3_state == 0 and btn3_last_state == 1 :
print("Push Button 3 (Blue)")
analyze1()
#Push Botton 4
if btn4_state == 0 and btn4_last_state == 1 :
print("Push Button 4 (Gray)")
analyze2()
#Push Botton 5
if btn5_state == 0 and btn5_last_state == 1 :
print("Push Button 5 (Red)")
clear()
time.sleep(0.5)Loading
esp32-devkit-v1
esp32-devkit-v1