UNIT_COLOR「M5Stack用カラーセンサユニット」を動かす
2021/12/30
初版
UNIT_COLOR
UNIT_COLOR
概要
UNIT_COLOR「M5Stack用カラーセンサユニット」を動かす。
ソースは共通になっており、ターゲットのM5StickCPlus,M5Core2をplatformio.iniの内容で切り替えてビルドする。
ソース
src/COLOR_TCS3472.ino
/*
*******************************************************************************
* Copyright (c) 2021 by M5Stack
* Equipped with M5Core2 sample source code
* 配套 M5Core2 示例源代码
* Visit the website for more information:https://docs.m5stack.com/en/core/core2
* 获取更多资料请访问:https://docs.m5stack.com/zh_CN/core/core2
*
* describe:COLOR_TCS3472. 颜色识别
* date:2021/8/11
*******************************************************************************
Please connect to Port A,Use COLOR Unit to read C, R, G, B values
请连接端口A,使用COLOR Unit读取C, R, G, B值
*/
#ifdef M5C2
#include <M5Core2.h>
#endif
#ifdef M5STICK_CP
#include <M5StickCPlus.h>
#endif
#include "Adafruit_TCS34725.h"
#define commonAnode true //set to false if using a common cathode LED. //如果使用普通阴极LED,则设置为false
byte gammatable[256]; // our RGB -> eye-recognized gamma color
static uint16_t color16(uint16_t r, uint16_t g, uint16_t b) {
uint16_t _color;
_color = (uint16_t)(r & 0xF8) << 8;
_color |= (uint16_t)(g & 0xFC) << 3;
_color |= (uint16_t)(b & 0xF8) >> 3;
return _color;
}
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X);
void setup() {
#ifdef M5STICK_CP
M5.begin();
M5.Axp.ScreenBreath(12);
M5.Lcd.setRotation(3);
M5.lcd.setTextSize(2);
#endif
#ifndef M5STICK_CP
M5.begin(); //Init M5Core2. 初始化 M5Core2
M5.lcd.setTextSize(2); //Set the text size to 2. 设置文字大小为2
#endif
M5.lcd.println("Color View Test!");
while(!tcs.begin()){ //如果color unit未能初始化
M5.lcd.println("No TCS34725 found ... check your connections");
M5.Lcd.drawString("No Found sensor.",50, 100, 4);
delay(1000);
}
tcs.setIntegrationTime(TCS34725_INTEGRATIONTIME_154MS); //Sets the integration time for the TC34725. 设置TC34725的集成时间
tcs.setGain(TCS34725_GAIN_4X); //Adjusts the gain on the TCS34725. 调整TCS34725上的增益
}
void loop() {
uint16_t clear, red, green, blue;
tcs.getRawData(&red, &green, &blue, &clear); //Reads the raw red, green, blue and clear channel values. 读取原始的红、绿、蓝和清晰的通道值
// Figure out some basic hex code for visualization. 生成对应的十六进制代码
uint32_t sum = clear;
float r, g, b;
r = red; r /= sum;
g = green; g /= sum;
b = blue; b /= sum;
r *= 256; g *= 256; b *= 256;
uint16_t _color = color16((int)r, (int)g, (int)b);
M5.lcd.setCursor(0,20); //Place the cursor at (0,0). 将光标固定在(0,0)
M5.lcd.fillRect(0,20,120,80,BLACK); //Fill the screen with a black rectangle. 将屏幕填充黑色矩形
M5.Lcd.print("C:"); M5.Lcd.println(clear);
M5.Lcd.print("R:"); M5.Lcd.println(red);
M5.Lcd.print("G:"); M5.Lcd.println(green);
M5.Lcd.print("B:"); M5.Lcd.println(blue);
M5.Lcd.print("0x");
M5.Lcd.print((int)r, HEX); M5.Lcd.print((int)g, HEX); M5.Lcd.print((int)b, HEX);
delay(1000);
}
platformio.ini
M5Core2用
[env:m5core2]
platform = espressif32
board = m5stack-core2
framework = arduino
upload_speed = 2000000
monitor_speed = 115200
board_build.partitions = default_16MB.csv
build_flags =
-DM5C2
-DCORE_DEBUG_LEVEL=4
-DBOARD_HAS_PSRAM
-mfix-esp32-psram-cache-issue
lib_deps =
https://github.com/m5stack/M5Core2.git
https://github.com/FastLED/FastLED
;https://github.com/m5stack/M5Core2/blob/master/examples/core2_for_aws/ArduinoECCX08.zip
lovyan03/LovyanGFX
adafruit/Adafruit BMP280 Library @ ^2.5.0
adafruit/Adafruit Unified Sensor @ ^1.1.4
m5stack/UNIT_ENV @ ^0.0.2
adafruit/Adafruit TCS34725 @ ^1.4.1
lib_ldf_mode = deep+
M5StickCPlus用
[env:M5StickCPlus]
platform = espressif32
board = m5stick-c
framework = arduino
build_flags = -DM5STICK_CP
monitor_speed = 115200
lib_ldf_mode = deep+
lib_deps =
m5stack/M5StickCPlus @ ^0.0.5
tinyu-zhao/FFT @ ^0.0.1
m5stack/UNIT_ENV @ ^0.0.2
adafruit/Adafruit BMP280 Library @ ^2.5.0
adafruit/Adafruit Unified Sensor @ ^1.1.4
adafruit/Adafruit TCS34725 @ ^1.4.1
動作
LCDに、TCS3472カラーセンサで検出した色成分が表示される。
参考情報
terminal関連:
Bootterm – a developer-friendly serial terminal program
platformio関連:
arduinoフレームワーク用platformio.ini集
Building Core2 FactoryDemo in PlatformIO
VSCodeとPlatformIOでM5Stack Core2開発
M5Stack Core2とVSCode + PlatformIOとでM5Stackプログラミングを始めてみた。
Arduino-IDE関連:
Arduino IDE environment - M5Paper
Arduino IDEのインストールと設定 (Windows, Mac, Linux対応)
M5Stackファミリ関連:
M5Core2 Arduino Install
M5Paper Arduino Install
M5CoreInk Arduino Install
M5Stamp-PICO Arduino Install
M5Stamp-C3 Arduino Install
Wio-Terminal/ESP8622/ESP32ボードを共通のスケッチで動かす(NTP-CLIENT編)
Wio-Terminal/M5Atom/ESP8622/ESP32ボードを共通のスケッチで動かす(v2)(STARWARS編)
Wio-Terminal/M5Atom/ESP8622/ESP32ボードを共通のスケッチで動かす(v2)(MQTT編)
Wio-Terminal/M5Atom/ESP8622/ESP32ボードを共通のスケッチで動かす(v2)(REST-API編)
Wio-Terminal/M5Atom/ESP8622/ESP32ボードを共通のスケッチで動かす(v2)(OSC編)
以上
| 固定リンク
« UNIT_ENV_III「M5Stack用温湿度気圧センサユニット Ver.3(ENV Ⅲ)」を動かす | トップページ | UNIT_TOF「M5Stack用ToF測距センサユニット」を動かす »
「PlatformIO」カテゴリの記事
- NuEVI/NuRADのビルド(2022.08.18)
- Wio_ExtFlashLoad(WriteSampleMenu.ino)スケッチをplatformioでビルドする(2022.02.03)
- uncannyeyesスケッチをplatformioでビルドする(2022.01.31)
- LovyanGFX-Display ライブラリを使用したスケッチをplatformioでビルドする(2022.01.30)
- Wio-Terminal/M5Core2のWiFiAnallyzer(2022.01.24)
この記事へのコメントは終了しました。


コメント