指间ESP光敏与Mic传感器操作
502748957@qq.com(讨论 | 贡献)2019年7月25日 (四) 07:28的版本 (创建页面,内容为“{| style="width: 800px;" |- | <p style="color: #4F4E4E;font-size:220%">'''指间ESP光敏与Mic传感器操作'''</p> <br> 指间ESP开发板板载了mic声音传感…”)
指间ESP光敏与Mic传感器操作
import machine, display
from machine import Pin, ADC
adcmic = ADC(Pin(37))
adclight = ADC(Pin(38))
bkcolor = 0x333333
tft = display.TFT()
tft.init(tft.ST7789)
tft.clear()
tft.rect(0, 0, 240, 240, bkcolor, bkcolor)
def mapTo(x, in_min, in_max, out_min, out_max):
return int((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min)
while True:
micval = adcmic.read()
lightval = adclight.read()
# mic
tft.line(195, 62, 215, 62, 0xFFFFFF)
tft.line(205, 53, 205, 63, 0xFFFFFF)
tft.ellipse(205, 43, 10, 10, 12, 0xFFFFFF, bkcolor)
tft.roundrect(198, 15, 15, 32, 6, 0xFFFFFF, bkcolor)
tft.roundrect(
199,
47 - mapTo(micval, 142, 3200, 6, 32),
13,
mapTo(micval, 142, 3200, 6, 32) - 1,
6,
bkcolor,
0xFFFFFF,
)
tft.rect(185, 68, 50, 15, bkcolor, bkcolor)
tft.text(185, 70, str(micval), 0xFFFFFF, transparent=True)
# light
tft.line(25, 57, 45, 57, 0xFFFFFF)
tft.line(35, 47, 35, 58, 0xFFFFFF)
tft.line(27, 55, 21, 61, 0xFFFFFF)
tft.line(38, 55, 32, 61, 0xFFFFFF)
tft.line(47, 55, 41, 61, 0xFFFFFF)
tft.roundrect(28, 15, 15, 32, 6, 0xFFFFFF, bkcolor) # light
tft.roundrect(
29,
47 - mapTo(lightval, 142, 3200, 6, 32),
13,
mapTo(lightval, 142, 3200, 6, 32) - 1,
6,
bkcolor,
0xFFFFFF,
)
tft.rect(15, 68, 50, 15, bkcolor, bkcolor)
tft.text(15, 70, str(lightval), 0xFFFFFF, transparent=True)
import machine, display
from machine import Pin
之后声明ADC,并命名为adcmic与adclight adcmic = ADC(Pin(37))
adclight = ADC(Pin(38))
初始化TFT并使用背景色"bkcolor"填充屏幕 bkcolor = 0x333333
tft = display.TFT()
tft.init(tft.ST7789)
tft.clear()
tft.rect(0, 0, 240, 240, bkcolor, bkcolor)
自制map函数,用于将ADC值映射到绘制图形的尺寸上 def mapTo(x, in_min, in_max, out_min, out_max):
return int((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min)
读取ADC micval = adcmic.read()
lightval = adclight.read()
绘制两个麦克风图形并将ADC数值显示在下面,具体图形绘制函数参考[TFT图形绘制Wiki] tft.line(195, 62, 215, 62, 0xFFFFFF)
tft.line(205, 53, 205, 63, 0xFFFFFF)
tft.ellipse(205, 43, 10, 10, 12, 0xFFFFFF, bkcolor)
tft.roundrect(198, 15, 15, 32, 6, 0xFFFFFF, bkcolor)
tft.roundrect(
199,
47 - mapTo(micval, 142, 3200, 6, 32),
13,
mapTo(micval, 142, 3200, 6, 32) - 1,
6,
bkcolor,
0xFFFFFF,
)
tft.rect(185, 68, 50, 15, bkcolor, bkcolor)
tft.text(185, 70, str(micval), 0xFFFFFF, transparent=True)
# light
tft.line(25, 57, 45, 57, 0xFFFFFF)
tft.line(35, 47, 35, 58, 0xFFFFFF)
tft.line(27, 55, 21, 61, 0xFFFFFF)
tft.line(38, 55, 32, 61, 0xFFFFFF)
tft.line(47, 55, 41, 61, 0xFFFFFF)
tft.roundrect(28, 15, 15, 32, 6, 0xFFFFFF, bkcolor) # light
tft.roundrect(
29,
47 - mapTo(lightval, 142, 3200, 6, 32),
13,
mapTo(lightval, 142, 3200, 6, 32) - 1,
6,
bkcolor,
0xFFFFFF,
)
tft.rect(15, 68, 50, 15, bkcolor, bkcolor)
tft.text(15, 70, str(lightval), 0xFFFFFF, transparent=True)
|