本过程贴记录如下字任务目标的完成:
基础任务2 编程实现数字光传感器BH1750的数据读取
基础任务4 实现光强信息的屏幕显示
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_ST7789.h> // Hardware-specific library for ST7789
#include <SPI.h>
#include <M5_DLight.h>
// Use dedicated hardware SPI pins
Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);
M5_DLight m5ligntSensor;
void setup(void) {
Serial.begin(115200);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
// turn on backlite
pinMode(TFT_BACKLITE, OUTPUT);
digitalWrite(TFT_BACKLITE, HIGH);
// turn on the TFT / I2C power supply
pinMode(TFT_I2C_POWER, OUTPUT);
digitalWrite(TFT_I2C_POWER, HIGH);
delay(10);
// initialize TFT
tft.init(135, 240); // Init ST7789 240x135
tft.setRotation(3);
tft.fillScreen(ST77XX_BLACK);
tft.setTextColor(ST77XX_WHITE);
delay(1000);
m5ligntSensor.begin();
m5ligntSensor.setMode(CONTINUOUSLY_H_RESOLUTION_MODE);
}
void loop() {
tft.setCursor(0, 0);
tft.setTextSize(3);
tft.println("HELLO EEPW");
if (digitalRead(5) == LOW) {
tft.setCursor(0, 20);
tft.setTextSize(3);
tft.print("BTN 1 pressed");
} else if (digitalRead(6) == LOW) {
tft.setCursor(0, 20);
tft.setTextSize(3);
tft.print("BTN 2 pressed");
}
delay(100);
tft.fillRect(0, 20, 240, 60, ST77XX_BLACK);
tft.println();
tft.print("LUX = ");
tft.print(m5ligntSensor.getLUX());
}这里实现的主要思路就是采用M5STACK官方提供的库,首先调用begin()方法进行传感器初始化:
/*! @brief Initialize the EXTIO2.*/
void M5_DLight::begin(TwoWire *wire, uint8_t sda, uint8_t scl, uint32_t freq) {
_wire = wire;
_sda = sda;
_scl = scl;
_freq = freq;
_wire->begin((int)_sda, (int)_scl, _freq);
powerOn();
}然后通过setmode()方法设置为连续转换模式。
/*! @brief Set the mode.*/
void M5_DLight::setMode(byte mode) {
writeByte(mode);
}最后getLux()方法获取I2C总线传递的光照传感器数据。
/*! @brief Reading light intensity.
@return Light intensity value read.. */
uint16_t M5_DLight::getLUX() {
uint16_t lux;
uint8_t buffer[2];
readBytes(buffer, 2);
lux = buffer[0] << 8 | buffer[1];
return lux;
}演示视频:
我要赚赏金
