//1・#include <M5Core2.h>にする
//2・void setupのポート番号を31,32に変える
// TODO 1・#include <M5Core2.h>にする既にしてある
#include <M5Core2.h>
#include "M5_STHS34PF80.h"
#define STHS34PF80_I2C_ADDRESS 0x5A
M5_STHS34PF80 TMOS;
uint8_t motionHysteresis = 0;
int16_t motionVal = 0, presenceVal = 0;
uint16_t motionThresholdVal = 0, precenceThresholdVal = 0;
sths34pf80_gain_mode_t gainMode;
void setup() {
M5.begin();
Wire.begin(32, 33); // I2C初期化 SDA=32, SCL=33 for M5Stack Core2
M5.Lcd.setTextColor(YELLOW);
M5.Lcd.setTextSize(2);
M5.Lcd.setCursor(80, 10);
M5.Lcd.println("Unit-TMOS TEST");
Serial.println("Unit-TMOS TEST");
M5.Lcd.setCursor(120, 35);
M5.Lcd.setTextColor(WHITE);
// Initialise I2C and devices
// TODO 2・void setupのポート番号を31,32に変える。このコードではしてある
while (TMOS.begin(&Wire, STHS34PF80_I2C_ADDRESS, 32, 33) == false) {
M5.Lcd.setCursor(60, 35);
M5.Lcd.setTextColor(RED);
M5.Lcd.println("Device init error");
Serial.println("I2C Error - check I2C Address");
Serial.println("Error setting up device - please check wiring.");
delay(200);
}
Serial.println("Open the Serial Plotter for graphical viewing");
TMOS.setTmosODR(STHS34PF80_TMOS_ODR_AT_2Hz);
// TMOS.setMotionThreshold(0xFF);
TMOS.setPresenceThreshold(0xC8); // Default value
TMOS.setMotionThreshold(0xC8);
TMOS.setPresenceHysteresis(0x32);
TMOS.setMotionHysteresis(0x32);
////Default mode, may cause inaccurate detection of MotionValFlag bit.
// TMOS.setGainMode(STHS34PF80_GAIN_DEFAULT_MODE);
// Decreasing gain mode, detection distance decreases
TMOS.setGainMode(STHS34PF80_GAIN_WIDE_MODE);
TMOS.setTmosSensitivity(0xff);
TMOS.resetAlgo();
TMOS.getGainMode(&gainMode);
TMOS.getMotionThreshold(&motionThresholdVal);
TMOS.getPresenceThreshold(&precenceThresholdVal);
TMOS.getMotionHysteresis(&motionHysteresis);
Serial.printf("precenceThresholdVal:%x, motionThresholdVal:%x, motionHysteresis:%x GainMode:%x\n",
precenceThresholdVal, motionThresholdVal, motionHysteresis, gainMode);
delay(1000);
}
void loop() {
sths34pf80_tmos_drdy_status_t dataReady;
TMOS.getDataReady(&dataReady);
if (dataReady.drdy == 1) {
// Real-time detection of presence and motion infrared energy values
sths34pf80_tmos_func_status_t status;
TMOS.getPresenceValue(&presenceVal);
TMOS.getMotionValue(&motionVal);
TMOS.getStatus(&status);
M5.Lcd.fillRect(104, 68, 80, 20, BLACK);
M5.Lcd.setCursor(0, 70);
M5.Lcd.printf("Presence:%d", presenceVal);
M5.Lcd.fillRect(165, 90, 100, 20, BLACK);
M5.Lcd.setCursor(0, 90);
M5.Lcd.printf("PrescenceFlag:%s", status.pres_flag ? "Detected" : "None");
M5.Lcd.fillRect(83, 108, 60, 20, BLACK);
M5.Lcd.setCursor(0, 110);
M5.Lcd.printf("Motion:%d", motionVal);
M5.Lcd.fillRect(165, 130, 100, 20, BLACK);
M5.Lcd.setCursor(0, 130);
M5.Lcd.printf("MotionValFlag:%s", status.mot_flag ? "Detected" : "None");
// Output only if presence/motion is detected
if (status.pres_flag == 1) {
TMOS.getPresenceValue(&presenceVal);
Serial.printf("Prescence Detected! PrescenceValue:%\n", presenceVal);
}
if (status.mot_flag == 1) {
TMOS.getMotionValue(&motionVal);
Serial.printf("Motion Detected! MotionValue:%d\n", motionVal);
}
}
}
↓↓↓↓↓Unifiedversion
#include <M5Unified.h>
#include "M5_STHS34PF80.h"
#define STHS34PF80_I2C_ADDRESS 0x5A
M5_STHS34PF80 TMOS;
uint8_t motionHysteresis = 0;
int16_t motionVal = 0, presenceVal = 0;
uint16_t motionThresholdVal = 0, precenceThresholdVal = 0;
sths34pf80_gain_mode_t gainMode;
void setup() {
auto cfg = M5.config();
M5.begin(cfg);
// I2C 初期化 (Core2 の Grove A 用)
Wire.begin(32, 33);
M5.Display.setTextColor(YELLOW);
M5.Display.setTextSize(2);
M5.Display.setCursor(80, 10);
M5.Display.println("Unit-TMOS TEST");
Serial.begin(115200);
Serial.println("Unit-TMOS TEST");
M5.Display.setCursor(120, 35);
M5.Display.setTextColor(WHITE);
// STHS34PF80 初期化
while (TMOS.begin(&Wire, STHS34PF80_I2C_ADDRESS, 32, 33) == false) {
M5.Display.setCursor(60, 35);
M5.Display.setTextColor(RED);
M5.Display.println("Device init error");
Serial.println("I2C Error - check I2C Address");
Serial.println("Error setting up device - please check wiring.");
delay(200);
}
Serial.println("Open the Serial Plotter for graphical viewing");
TMOS.setTmosODR(STHS34PF80_TMOS_ODR_AT_2Hz);
TMOS.setPresenceThreshold(0xC8);
TMOS.setMotionThreshold(0xC8);
TMOS.setPresenceHysteresis(0x32);
TMOS.setMotionHysteresis(0x32);
TMOS.setGainMode(STHS34PF80_GAIN_WIDE_MODE);
TMOS.setTmosSensitivity(0xff);
TMOS.resetAlgo();
TMOS.getGainMode(&gainMode);
TMOS.getMotionThreshold(&motionThresholdVal);
TMOS.getPresenceThreshold(&precenceThresholdVal);
TMOS.getMotionHysteresis(&motionHysteresis);
Serial.printf("precenceThresholdVal:%x, motionThresholdVal:%x, motionHysteresis:%x GainMode:%x\n",
precenceThresholdVal, motionThresholdVal, motionHysteresis, gainMode);
delay(1000);
}
void loop() {
sths34pf80_tmos_drdy_status_t dataReady;
TMOS.getDataReady(&dataReady);
if (dataReady.drdy == 1) {
sths34pf80_tmos_func_status_t status;
TMOS.getPresenceValue(&presenceVal);
TMOS.getMotionValue(&motionVal);
TMOS.getStatus(&status);
// 表示更新
M5.Display.fillRect(104, 68, 80, 20, BLACK);
M5.Display.setCursor(0, 70);
M5.Display.printf("Presence:%d", presenceVal);
M5.Display.fillRect(165, 90, 100, 20, BLACK);
M5.Display.setCursor(0, 90);
M5.Display.printf("PrescenceFlag:%s", status.pres_flag ? "Detected" : "None");
M5.Display.fillRect(83, 108, 60, 20, BLACK);
M5.Display.setCursor(0, 110);
M5.Display.printf("Motion:%d", motionVal);
M5.Display.fillRect(165, 130, 100, 20, BLACK);
M5.Display.setCursor(0, 130);
M5.Display.printf("MotionValFlag:%s", status.mot_flag ? "Detected" : "None");
// 検出時のみシリアル出力
if (status.pres_flag == 1) {
TMOS.getPresenceValue(&presenceVal);
Serial.printf("Presence Detected! PresenceValue:%d\n", presenceVal);
}
if (status.mot_flag == 1) {
TMOS.getMotionValue(&motionVal);
Serial.printf("Motion Detected! MotionValue:%d\n", motionVal);
}
}
}