This commit is contained in:
lewisxhe
2024-09-21 08:52:30 +08:00
parent b6f0e57fc9
commit 35d38d2526
168 changed files with 125247 additions and 0 deletions
@@ -0,0 +1,88 @@
/**
*
* @license MIT License
*
* Copyright (c) 2022 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file BAM423_Accelerometer.ino
* @author Lewis He (lewishe@outlook.com)
* @date 2023-04-01
*
*/
#include <Wire.h>
#include <SPI.h>
#include <Arduino.h>
#include "SensorBMA423.hpp"
#ifndef SENSOR_SDA
#define SENSOR_SDA 21
#endif
#ifndef SENSOR_SCL
#define SENSOR_SCL 22
#endif
#ifndef SENSOR_IRQ
#define SENSOR_IRQ 39
#endif
SensorBMA423 accel;
uint32_t lastMillis;
void setup()
{
Serial.begin(115200);
while (!Serial);
pinMode(SENSOR_IRQ, INPUT);
if (!accel.begin(Wire, BMA423_SLAVE_ADDRESS, SENSOR_SDA, SENSOR_SCL)) {
Serial.println("Failed to find BMA423 - check your wiring!");
while (1) {
delay(1000);
}
}
Serial.println("Init BAM423 Sensor success!");
//Default 4G ,200HZ
accel.configAccelerometer();
accel.enableAccelerometer();
}
void loop()
{
int16_t x = 0, y = 0, z = 0;
accel.getAccelerometer(x, y, z);
Serial.print("X:");
Serial.print(x); Serial.print(" ");
Serial.print("Y:");
Serial.print(y); Serial.print(" ");
Serial.print("Z:");
Serial.print(z);
Serial.println();
delay(50);
}
@@ -0,0 +1,142 @@
/**
*
* @license MIT License
*
* Copyright (c) 2022 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file BAM423_Orientation.ino
* @author Lewis He (lewishe@outlook.com)
* @date 2023-04-03
*
*/
#include <Wire.h>
#include <SPI.h>
#include <Arduino.h>
#include "SensorBMA423.hpp"
#ifndef SENSOR_SDA
#define SENSOR_SDA 21
#endif
#ifndef SENSOR_SCL
#define SENSOR_SCL 22
#endif
#ifndef SENSOR_IRQ
#define SENSOR_IRQ 39
#endif
SensorBMA423 accel;
char report[256];
void setup()
{
Serial.begin(115200);
while (!Serial);
pinMode(SENSOR_IRQ, INPUT);
if (!accel.begin(Wire, BMA423_SLAVE_ADDRESS, SENSOR_SDA, SENSOR_SCL)) {
Serial.println("Failed to find BMA423 - check your wiring!");
while (1) {
delay(1000);
}
}
Serial.println("Init BAM423 Sensor success!");
//Default 4G ,200HZ
accel.configAccelerometer();
accel.enableAccelerometer();
Serial.println("Output ...");
}
void loop()
{
uint8_t direction = accel.direction();
switch (direction) {
case SensorBMA423::DIRECTION_BOTTOM_LEFT:
sprintf( report, "\r\n ________________ " \
"\r\n | | " \
"\r\n | | " \
"\r\n | | " \
"\r\n | | " \
"\r\n | | " \
"\r\n | * | " \
"\r\n |________________| \r\n" );
break;
case SensorBMA423::DIRECTION_TOP_RIGHT:
sprintf( report, "\r\n ________________ " \
"\r\n | | " \
"\r\n | * | " \
"\r\n | | " \
"\r\n | | " \
"\r\n | | " \
"\r\n | | " \
"\r\n |________________| \r\n" );
break;
case SensorBMA423::DIRECTION_TOP_LEFT:
sprintf( report, "\r\n ________________ " \
"\r\n | | " \
"\r\n | * | " \
"\r\n | | " \
"\r\n | | " \
"\r\n | | " \
"\r\n | | " \
"\r\n |________________| \r\n" );
break;
case SensorBMA423::DIRECTION_BOTTOM_RIGHT:
sprintf( report, "\r\n ________________ " \
"\r\n | | " \
"\r\n | | " \
"\r\n | | " \
"\r\n | | " \
"\r\n | | " \
"\r\n | * | " \
"\r\n |________________| \r\n" );
break;
case SensorBMA423::DIRECTION_BOTTOM:
sprintf( report, "\r\n ________________ " \
"\r\n |________________| " \
"\r\n * \r\n" );
break;
case SensorBMA423::DIRECTION_TOP:
sprintf( report, "\r\n __*_____________ " \
"\r\n |________________| \r\n" );
break;
default:
sprintf( report, "None of the 3D orientation axes is set in BMA423 - accelerometer.\r\n" );
break;
}
Serial.println(direction);
Serial.println(report);
delay(1000);
}
@@ -0,0 +1,77 @@
/**
*
* @license MIT License
*
* Copyright (c) 2022 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file BAM423_Temperature.ino
* @author Lewis He (lewishe@outlook.com)
* @date 2023-04-01
*
*/
#include <Wire.h>
#include <SPI.h>
#include <Arduino.h>
#include "SensorBMA423.hpp"
#ifndef SENSOR_SDA
#define SENSOR_SDA 21
#endif
#ifndef SENSOR_SCL
#define SENSOR_SCL 22
#endif
#ifndef SENSOR_IRQ
#define SENSOR_IRQ 39
#endif
SensorBMA423 accel;
void setup()
{
Serial.begin(115200);
while (!Serial);
pinMode(SENSOR_IRQ, INPUT);
if (!accel.begin(Wire, BMA423_SLAVE_ADDRESS, SENSOR_SDA, SENSOR_SCL)) {
Serial.println("Failed to find BMA423 - check your wiring!");
while (1) {
delay(1000);
}
}
Serial.println("Init BAM423 Sensor success!");
}
void loop()
{
Serial.print("getTemperature:");
Serial.print(accel.getTemperature(SensorBMA423::TEMP_DEG));
Serial.print("*C ");
Serial.print(accel.getTemperature(SensorBMA423::TEMP_FAHREN));
Serial.print("*F");
delay(1000);
}
@@ -0,0 +1,140 @@
/**
*
* @license MIT License
*
* Copyright (c) 2023 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file BHI260AP_6DoF.ino
* @author Lewis He (lewishe@outlook.com)
* @date 2023-09-06
* @note Changed from Boschsensortec API https://github.com/boschsensortec/BHY2_SensorAPI
*/
#include <Wire.h>
#include <SPI.h>
#include <Arduino.h>
#include "SensorBHI260AP.hpp"
#ifdef BHY2_USE_I2C
#define BHI260AP_SDA 21
#define BHI260AP_SCL 22
#define BHI260AP_IRQ 39
#define BHI260AP_RST -1
#else
#define BHI260AP_MOSI 33
#define BHI260AP_MISO 34
#define BHI260AP_SCK 35
#define BHI260AP_CS 36
#define BHI260AP_IRQ 37
#define BHI260AP_RST 47
#endif
SensorBHI260AP bhy;
void accel_process_callback(uint8_t sensor_id, uint8_t *data_ptr, uint32_t len, uint64_t *timestamp)
{
struct bhy2_data_xyz data;
float scaling_factor = get_sensor_default_scaling(sensor_id);
bhy2_parse_xyz(data_ptr, &data);
Serial.print(bhy.getSensorName(sensor_id));
Serial.print(":");
Serial.printf("x: %f, y: %f, z: %f;\r\n",
data.x * scaling_factor,
data.y * scaling_factor,
data.z * scaling_factor
);
}
void gyro_process_callback(uint8_t sensor_id, uint8_t *data_ptr, uint32_t len, uint64_t *timestamp)
{
struct bhy2_data_xyz data;
float scaling_factor = get_sensor_default_scaling(sensor_id);
bhy2_parse_xyz(data_ptr, &data);
Serial.print(bhy.getSensorName(sensor_id));
Serial.print(":");
Serial.printf("x: %f, y: %f, z: %f;\r\n",
data.x * scaling_factor,
data.y * scaling_factor,
data.z * scaling_factor
);
}
void setup()
{
Serial.begin(115200);
while (!Serial);
// Set the reset pin and interrupt pin, if any
bhy.setPins(BHI260AP_RST, BHI260AP_IRQ);
#ifdef BHY2_USE_I2C
// Using I2C interface
// BHI260AP_SLAVE_ADDRESS_L = 0x28
// BHI260AP_SLAVE_ADDRESS_H = 0x29
if (!bhy.init(Wire, BHI260AP_SDA, BHI260AP_SCL, BHI260AP_SLAVE_ADDRESS_L)) {
Serial.print("Failed to init BHI260AP - ");
Serial.println(bhy.getError());
while (1) {
delay(1000);
}
}
#else
// Using SPI interface
if (!bhy.init(SPI, BHI260AP_CS, BHI260AP_MOSI, BHI260AP_MISO, BHI260AP_SCK)) {
Serial.print("Failed to init BHI260AP - ");
Serial.println(bhy.getError());
while (1) {
delay(1000);
}
}
#endif
Serial.println("Init BHI260AP Sensor success!");
// Output all available sensors to Serial
bhy.printSensors(Serial);
float sample_rate = 100.0; /* Read out hintr_ctrl measured at 100Hz */
uint32_t report_latency_ms = 0; /* Report immediately */
// Enable acceleration
bhy.configure(SENSOR_ID_ACC_PASS, sample_rate, report_latency_ms);
// Enable gyroscope
bhy.configure(SENSOR_ID_GYRO_PASS, sample_rate, report_latency_ms);
// Set the acceleration sensor result callback function
bhy.onResultEvent(SENSOR_ID_ACC_PASS, accel_process_callback);
// Set the gyroscope sensor result callback function
bhy.onResultEvent(SENSOR_ID_GYRO_PASS, gyro_process_callback);
}
void loop()
{
// Update sensor fifo
bhy.update();
delay(50);
}
@@ -0,0 +1,107 @@
/**
*
* @license MIT License
*
* Copyright (c) 2023 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file BHI260AP_DebugInfo.ino
* @author Lewis He (lewishe@outlook.com)
* @date 2023-10-08
* @note Changed from Boschsensortec API https://github.com/boschsensortec/BHY2_SensorAPI
*/
#include <Wire.h>
#include <SPI.h>
#include <Arduino.h>
#include "SensorBHI260AP.hpp"
#ifdef BHY2_USE_I2C
#define BHI260AP_SDA 21
#define BHI260AP_SCL 22
#define BHI260AP_IRQ 39
#define BHI260AP_RST -1
#else
#define BHI260AP_MOSI 33
#define BHI260AP_MISO 34
#define BHI260AP_SCK 35
#define BHI260AP_CS 36
#define BHI260AP_IRQ 37
#define BHI260AP_RST 47
#endif
SensorBHI260AP bhy;
void quaternion_process_callback(uint8_t sensor_id, uint8_t *data_ptr, uint32_t len, uint64_t *timestamp)
{
Serial.println(bhy.getSensorName(sensor_id));
}
void setup()
{
Serial.begin(115200);
while (!Serial);
// Set the reset pin and interrupt pin, if any
bhy.setPins(BHI260AP_RST, BHI260AP_IRQ);
#ifdef BHY2_USE_I2C
// Using I2C interface
// BHI260AP_SLAVE_ADDRESS_L = 0x28
// BHI260AP_SLAVE_ADDRESS_H = 0x29
if (!bhy.init(Wire, BHI260AP_SDA, BHI260AP_SCL, BHI260AP_SLAVE_ADDRESS_L)) {
Serial.print("Failed to init BHI260AP - ");
Serial.println(bhy.getError());
while (1) {
delay(1000);
}
}
#else
// Using SPI interface
if (!bhy.init(SPI, BHI260AP_CS, BHI260AP_MOSI, BHI260AP_MISO, BHI260AP_SCK)) {
Serial.print("Failed to init BHI260AP - ");
Serial.println(bhy.getError());
while (1) {
delay(1000);
}
}
#endif
Serial.println("Init BHI260AP Sensor success!");
// Output all current sensor information
bhy.printInfo(Serial);
// Output interrupt configuration information to Serial
bhy.printInterruptCtrl(Serial);
}
void loop()
{
// Update sensor fifo
bhy.update();
delay(50);
}
@@ -0,0 +1,163 @@
/**
*
* @license MIT License
*
* Copyright (c) 2023 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file BHI260AP_Orientation.ino
* @author Lewis He (lewishe@outlook.com)
* @date 2023-10-07
* @note Changed from Boschsensortec API https://github.com/boschsensortec/BHY2_SensorAPI
*/
#include <Wire.h>
#include <SPI.h>
#include <Arduino.h>
#include "SensorBHI260AP.hpp"
#ifdef BHY2_USE_I2C
#define BHI260AP_SDA 21
#define BHI260AP_SCL 22
#define BHI260AP_IRQ 39
#define BHI260AP_RST -1
#else
#define BHI260AP_MOSI 33
#define BHI260AP_MISO 34
#define BHI260AP_SCK 35
#define BHI260AP_CS 36
#define BHI260AP_IRQ 37
#define BHI260AP_RST 47
#endif
void orientation_process_callback(uint8_t sensor_id, uint8_t *data_ptr, uint32_t len, uint64_t *timestamp);
SensorBHI260AP bhy;
void setup()
{
Serial.begin(115200);
while (!Serial);
// Set the reset pin and interrupt pin, if any
bhy.setPins(BHI260AP_RST, BHI260AP_IRQ);
#ifdef BHY2_USE_I2C
// Using I2C interface
// BHI260AP_SLAVE_ADDRESS_L = 0x28
// BHI260AP_SLAVE_ADDRESS_H = 0x29
if (!bhy.init(Wire, BHI260AP_SDA, BHI260AP_SCL, BHI260AP_SLAVE_ADDRESS_L)) {
Serial.print("Failed to init BHI260AP - ");
Serial.println(bhy.getError());
while (1) {
delay(1000);
}
}
#else
// Using SPI interface
if (!bhy.init(SPI, BHI260AP_CS, BHI260AP_MOSI, BHI260AP_MISO, BHI260AP_SCK)) {
Serial.print("Failed to init BHI260AP - ");
Serial.println(bhy.getError());
while (1) {
delay(1000);
}
}
#endif
Serial.println("Init BHI260AP Sensor success!");
// Output all available sensors to Serial
bhy.printSensors(Serial);
float sample_rate = 100.0; /* Read out hintr_ctrl measured at 100Hz */
uint32_t report_latency_ms = 0; /* Report immediately */
// Direction detection relies on the accelerometer, and the accelerometer needs to be turned on first.
bhy.configure(SENSOR_ID_ACC_PASS, sample_rate, report_latency_ms);
// Enable direction detection
bhy.configure(SENSOR_ID_DEVICE_ORI, sample_rate, report_latency_ms);
// Set the direction detection result output processing function
bhy.onResultEvent(SENSOR_ID_DEVICE_ORI, orientation_process_callback);
}
void loop()
{
// Update sensor fifo
bhy.update();
delay(50);
}
void orientation_process_callback(uint8_t sensor_id, uint8_t *data_ptr, uint32_t len, uint64_t *timestamp)
{
char report[256];
uint8_t direction = *data_ptr;
switch (direction) {
case BHY2_DIRECTION_BOTTOM_LEFT:
sprintf( report, "\r\n ________________ " \
"\r\n | | " \
"\r\n | | " \
"\r\n | | " \
"\r\n | | " \
"\r\n | | " \
"\r\n | * | " \
"\r\n |________________| \r\n" );
break;
case BHY2_DIRECTION_TOP_RIGHT:
sprintf( report, "\r\n ________________ " \
"\r\n | | " \
"\r\n | * | " \
"\r\n | | " \
"\r\n | | " \
"\r\n | | " \
"\r\n | | " \
"\r\n |________________| \r\n" );
break;
case BHY2_DIRECTION_TOP_LEFT:
sprintf( report, "\r\n ________________ " \
"\r\n | | " \
"\r\n | * | " \
"\r\n | | " \
"\r\n | | " \
"\r\n | | " \
"\r\n | | " \
"\r\n |________________| \r\n" );
break;
case BHY2_DIRECTION_BOTTOM_RIGHT:
sprintf( report, "\r\n ________________ " \
"\r\n | | " \
"\r\n | | " \
"\r\n | | " \
"\r\n | | " \
"\r\n | | " \
"\r\n | * | " \
"\r\n |________________| \r\n" );
break;
default:
sprintf( report, "None of the 3D orientation axes is set in BHI260 - accelerometer.\r\n" );
break;
}
Serial.print(bhy.getSensorName(sensor_id));
Serial.print(":");
Serial.println(direction);
Serial.println(report);
}
@@ -0,0 +1,125 @@
/**
*
* @license MIT License
*
* Copyright (c) 2023 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file BHI260AP_StepCounter.ino
* @author Lewis He (lewishe@outlook.com)
* @date 2023-10-08
* @note Changed from Boschsensortec API https://github.com/boschsensortec/BHY2_SensorAPI
*/
#include <Wire.h>
#include <SPI.h>
#include <Arduino.h>
#include "SensorBHI260AP.hpp"
#ifdef BHY2_USE_I2C
#define BHI260AP_SDA 21
#define BHI260AP_SCL 22
#define BHI260AP_IRQ 39
#define BHI260AP_RST -1
#else
#define BHI260AP_MOSI 33
#define BHI260AP_MISO 34
#define BHI260AP_SCK 35
#define BHI260AP_CS 36
#define BHI260AP_IRQ 37
#define BHI260AP_RST 47
#endif
SensorBHI260AP bhy;
void step_detector_process_callback(uint8_t sensor_id, uint8_t *data_ptr, uint32_t len, uint64_t *timestamp)
{
Serial.print(bhy.getSensorName(sensor_id));
Serial.println(" detected.");
}
void step_counter_process_callback(uint8_t sensor_id, uint8_t *data_ptr, uint32_t len, uint64_t *timestamp)
{
Serial.print(bhy.getSensorName(sensor_id));
Serial.print(":");
Serial.println(bhy2_parse_step_counter(data_ptr));
}
void setup()
{
Serial.begin(115200);
while (!Serial);
// Set the reset pin and interrupt pin, if any
bhy.setPins(BHI260AP_RST, BHI260AP_IRQ);
#ifdef BHY2_USE_I2C
// Using I2C interface
// BHI260AP_SLAVE_ADDRESS_L = 0x28
// BHI260AP_SLAVE_ADDRESS_H = 0x29
if (!bhy.init(Wire, BHI260AP_SDA, BHI260AP_SCL, BHI260AP_SLAVE_ADDRESS_L)) {
Serial.print("Failed to init BHI260AP - ");
Serial.println(bhy.getError());
while (1) {
delay(1000);
}
}
#else
// Using SPI interface
if (!bhy.init(SPI, BHI260AP_CS, BHI260AP_MOSI, BHI260AP_MISO, BHI260AP_SCK)) {
Serial.print("Failed to init BHI260AP - ");
Serial.println(bhy.getError());
while (1) {
delay(1000);
}
}
#endif
Serial.println("Init BHI260AP Sensor success!");
// Output all available sensors to Serial
bhy.printSensors(Serial);
float sample_rate = 100.0; /* Read out hintr_ctrl measured at 100Hz */
uint32_t report_latency_ms = 0; /* Report immediately */
// Direction Step count relies on the accelerometer, and the accelerometer needs to be turned on first.
bhy.configure(SENSOR_ID_ACC_PASS, sample_rate, report_latency_ms);
// Enable Step detector
bhy.configure(SENSOR_ID_STD, sample_rate, report_latency_ms);
// Enable Step counter
bhy.configure(SENSOR_ID_STC, sample_rate, report_latency_ms);
// Set the number of steps to detect the callback function
bhy.onResultEvent(SENSOR_ID_STD, step_detector_process_callback);
bhy.onResultEvent(SENSOR_ID_STC, step_counter_process_callback);
}
void loop()
{
// Update sensor fifo
bhy.update();
delay(50);
}
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,167 @@
/**
*
* @license MIT License
*
* Copyright (c) 2024 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file BHI260AP_aux_BMM150.ino
* @author Lewis He (lewishe@outlook.com)
* @date 2024-07-22
* @note Changed from Boschsensortec API https://github.com/boschsensortec/BHY2_SensorAPI
*/
#include <Wire.h>
#include <SPI.h>
#include <Arduino.h>
#include "SensorBHI260AP.hpp"
/*
Write the firmware containing the BMM150 magnetometer function into the flash.
This function requires the BHI260AP external SPI Flash.
If there is no Flash, it can only be written and run in RAM.
Example firmware source: https://github.com/boschsensortec/BHY2_SensorAPI/tree/master/firmware
You can also compile custom firmware to write
How to build custom firmware see : https://www.bosch-sensortec.com/media/boschsensortec/downloads/application_notes_1/bst-bhi260ab-an000.pdf
*/
#define WRITE_TO_FLASH 1 //Set 1 write fw to flash ,set 0 write fw to ram
#if WRITE_TO_FLASH
#include "BHI260AP_aux_BMM150-flash.fw.h"
const uint8_t *firmware = bhi26ap_aux_bmm150_flash_fw;
const size_t fw_size = sizeof(bhi26ap_aux_bmm150_flash_fw);
#else
#include "BHI260AP_aux_BMM150.fw.h"
const uint8_t *firmware = bhi26ap_aux_bmm150_fw;
const size_t fw_size = sizeof(bhi26ap_aux_bmm150_fw);
#endif
#ifdef BHY2_USE_I2C
#define BHI260AP_SDA 21
#define BHI260AP_SCL 22
#define BHI260AP_IRQ 39
#define BHI260AP_RST -1
#else
#define BHI260AP_MOSI 27
#define BHI260AP_MISO 46
#define BHI260AP_SCK 3
#define BHI260AP_CS 28
#define BHI260AP_IRQ 30
#define BHI260AP_RST -1
#endif
SensorBHI260AP bhy;
void bhy_process_callback(uint8_t sensor_id, uint8_t *data_ptr, uint32_t len, uint64_t *timestamp)
{
struct bhy2_data_xyz data;
float scaling_factor = get_sensor_default_scaling(sensor_id);
bhy2_parse_xyz(data_ptr, &data);
Serial.print(bhy.getSensorName(sensor_id));
Serial.print(":");
Serial.printf("x: %f, y: %f, z: %f;\r\n",
data.x * scaling_factor,
data.y * scaling_factor,
data.z * scaling_factor
);
}
void setup()
{
Serial.begin(115200);
while (!Serial);
// Set the reset pin and interrupt pin, if any
bhy.setPins(BHI260AP_RST, BHI260AP_IRQ);
// Set the firmware array address and firmware size
bhy.setFirmware(firmware, fw_size, WRITE_TO_FLASH);
#if WRITE_TO_FLASH
// Set to load firmware from flash
bhy.setBootFormFlash(true);
#endif
#ifdef BHY2_USE_I2C
// Using I2C interface
// BHI260AP_SLAVE_ADDRESS_L = 0x28
// BHI260AP_SLAVE_ADDRESS_H = 0x29
if (!bhy.init(Wire, BHI260AP_SDA, BHI260AP_SCL, BHI260AP_SLAVE_ADDRESS_L)) {
Serial.print("Failed to init BHI260AP - ");
Serial.println(bhy.getError());
while (1) {
delay(1000);
}
}
#else
// Using SPI interface
if (!bhy.init(SPI, BHI260AP_CS, BHI260AP_MOSI, BHI260AP_MISO, BHI260AP_SCK)) {
Serial.print("Failed to init BHI260AP - ");
Serial.println(bhy.getError());
while (1) {
delay(1000);
}
}
#endif
Serial.println("Init BHI260AP Sensor success!");
// Output all available sensors to Serial
bhy.printSensors(Serial);
float sample_rate = 100.0; /* Read out hintr_ctrl measured at 100Hz */
uint32_t report_latency_ms = 0; /* 0 = report immediately */
// Enable acceleration
report_latency_ms = 1000; // Report once per second
bhy.configure(SENSOR_ID_ACC_PASS, sample_rate, report_latency_ms);
// Enable gyroscope
report_latency_ms = 1000; //Report once per second
bhy.configure(SENSOR_ID_GYRO_PASS, sample_rate, report_latency_ms);
// Enable magnetometer
report_latency_ms = 500; //Report every 500 milliseconds
bhy.configure(SENSOR_ID_MAG_PASS, sample_rate, report_latency_ms);
// Set the acceleration sensor result callback function
bhy.onResultEvent(SENSOR_ID_ACC_PASS, bhy_process_callback);
// Set the gyroscope sensor result callback function
bhy.onResultEvent(SENSOR_ID_GYRO_PASS, bhy_process_callback);
// Set the magnetometer sensor result callback function
bhy.onResultEvent(SENSOR_ID_MAG_PASS, bhy_process_callback);
}
void loop()
{
// Update sensor fifo
bhy.update();
delay(50);
}
@@ -0,0 +1,168 @@
/**
*
* @license MIT License
*
* Copyright (c) 2024 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file BHI260AP_aux_BMM150_BME280.ino
* @author Lewis He (lewishe@outlook.com)
* @date 2024-07-24
* @note Changed from Boschsensortec API https://github.com/boschsensortec/BHY2_SensorAPI
*/
#include <Wire.h>
#include <SPI.h>
#include <Arduino.h>
#include "SensorBHI260AP.hpp"
/*
Write the firmware containing the BMM150 magnetometer function into the flash.
This function requires the BHI260AP external SPI Flash.
If there is no Flash, it can only be written and run in RAM.
Example firmware source: https://github.com/boschsensortec/BHY2_SensorAPI/tree/master/firmware
You can also compile custom firmware to write
How to build custom firmware see : https://www.bosch-sensortec.com/media/boschsensortec/downloads/application_notes_1/bst-bhi260ab-an000.pdf
*/
#define WRITE_TO_FLASH 1 //Set 1 write fw to flash ,set 0 write fw to ram
#if WRITE_TO_FLASH
#include "BHI260_aux_BMM150_BME280-flash.fw.h"
const uint8_t *firmware = bhi26ap_aux_bmm150_bme280_flash_fw;
const size_t fw_size = sizeof(bhi26ap_aux_bmm150_bme280_flash_fw);
#else
#include "BHI260_aux_BMM150_BME280.fw.h"
const uint8_t *firmware = bhi26ap_aux_bmm150_bme280_fw;
const size_t fw_size = sizeof(bhi26ap_aux_bmm150_bme280_fw);
#endif
#ifdef BHY2_USE_I2C
#define BHI260AP_SDA 21
#define BHI260AP_SCL 22
#define BHI260AP_IRQ 39
#define BHI260AP_RST -1
#else
#define BHI260AP_MOSI 27
#define BHI260AP_MISO 46
#define BHI260AP_SCK 3
#define BHI260AP_CS 28
#define BHI260AP_IRQ 30
#define BHI260AP_RST -1
#endif
SensorBHI260AP bhy;
void parse_bme280_sensor_data(uint8_t sensor_id, uint8_t *data_ptr, uint32_t len, uint64_t *timestamp)
{
float humidity = 0;
float temperature = 0;
float pressure = 0;
switch (sensor_id) {
case SENSOR_ID_HUM:
bhy2_parse_humidity(data_ptr, &humidity);
Serial.print("humidity:"); Serial.print(humidity); Serial.println("%");
break;
case SENSOR_ID_TEMP:
bhy2_parse_temperature_celsius(data_ptr, &temperature);
Serial.print("temperature:"); Serial.print(temperature); Serial.println("*C");
break;
case SENSOR_ID_BARO:
bhy2_parse_pressure(data_ptr, &pressure);
Serial.print("pressure:"); Serial.print(pressure); Serial.println("hPa");
break;
default:
break;
}
}
void setup()
{
Serial.begin(115200);
while (!Serial);
// Set the reset pin and interrupt pin, if any
bhy.setPins(BHI260AP_RST, BHI260AP_IRQ);
// Set the firmware array address and firmware size
bhy.setFirmware(firmware, fw_size, WRITE_TO_FLASH);
#if WRITE_TO_FLASH
// Set to load firmware from flash
bhy.setBootFormFlash(true);
#endif
#ifdef BHY2_USE_I2C
// Using I2C interface
// BHI260AP_SLAVE_ADDRESS_L = 0x28
// BHI260AP_SLAVE_ADDRESS_H = 0x29
if (!bhy.init(Wire, BHI260AP_SDA, BHI260AP_SCL, BHI260AP_SLAVE_ADDRESS_L)) {
Serial.print("Failed to init BHI260AP - ");
Serial.println(bhy.getError());
while (1) {
delay(1000);
}
}
#else
// Using SPI interface
if (!bhy.init(SPI, BHI260AP_CS, BHI260AP_MOSI, BHI260AP_MISO, BHI260AP_SCK)) {
Serial.print("Failed to init BHI260AP - ");
Serial.println(bhy.getError());
while (1) {
delay(1000);
}
}
#endif
Serial.println("Init BHI260AP Sensor success!");
// Output all available sensors to Serial
bhy.printSensors(Serial);
float sample_rate = 0.0; /* Read out hintr_ctrl measured at 100Hz */
uint32_t report_latency_ms = 0; /* Report immediately */
/*
* Enable BME280 function
* Function depends on BME280.
* If the hardware is not connected to BME280, the function cannot be used.
* * */
bhy.configure(SENSOR_ID_TEMP, sample_rate, report_latency_ms);
bhy.configure(SENSOR_ID_HUM, sample_rate, report_latency_ms);
bhy.configure(SENSOR_ID_BARO, sample_rate, report_latency_ms);
// Register BME280 data parse callback function
bhy.onResultEvent(SENSOR_ID_TEMP, parse_bme280_sensor_data);
bhy.onResultEvent(SENSOR_ID_HUM, parse_bme280_sensor_data);
bhy.onResultEvent(SENSOR_ID_BARO, parse_bme280_sensor_data);
}
void loop()
{
// Update sensor fifo
bhy.update();
delay(50);
}
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,195 @@
/**
*
* @license MIT License
*
* Copyright (c) 2024 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file BHI260AP_aux_BMM150_euler.ino
* @author Lewis He (lewishe@outlook.com)
* @date 2024-07-23
* @note Changed from Boschsensortec API https://github.com/boschsensortec/BHY2_SensorAPI
*/
#include <Wire.h>
#include <SPI.h>
#include <Arduino.h>
#include "SensorBHI260AP.hpp"
/*
Write the firmware containing the BMM150 magnetometer function into the flash.
This function requires the BHI260AP external SPI Flash.
If there is no Flash, it can only be written and run in RAM.
Example firmware source: https://github.com/boschsensortec/BHY2_SensorAPI/tree/master/firmware
You can also compile custom firmware to write
How to build custom firmware see : https://www.bosch-sensortec.com/media/boschsensortec/downloads/application_notes_1/bst-bhi260ab-an000.pdf
*/
#define WRITE_TO_FLASH 1 //Set 1 write fw to flash ,set 0 write fw to ram
#if WRITE_TO_FLASH
#include "BHI260AP_aux_BMM150-flash.fw.h"
const uint8_t *firmware = bhi26ap_aux_bmm150_flash_fw;
const size_t fw_size = sizeof(bhi26ap_aux_bmm150_flash_fw);
#else
#include "BHI260AP_aux_BMM150.fw.h"
const uint8_t *firmware = bhi26ap_aux_bmm150_fw;
const size_t fw_size = sizeof(bhi26ap_aux_bmm150_fw);
#endif
#ifdef BHY2_USE_I2C
#define BHI260AP_SDA 21
#define BHI260AP_SCL 22
#define BHI260AP_IRQ 39
#define BHI260AP_RST -1
#else
#define BHI260AP_MOSI 27
#define BHI260AP_MISO 46
#define BHI260AP_SCK 3
#define BHI260AP_CS 28
#define BHI260AP_IRQ 30
#define BHI260AP_RST -1
#endif
SensorBHI260AP bhy;
void parse_euler(uint8_t sensor_id, uint8_t *data_ptr, uint32_t len, uint64_t *timestamp)
{
struct bhy2_data_orientation data;
uint32_t s, ns;
uint64_t tns;
// Function to parse FIFO frame data into orientation
bhy2_parse_orientation(data_ptr, &data);
uint64_t _timestamp = *timestamp;
time_to_s_ns(_timestamp, &s, &ns, &tns);
uint8_t accuracy = bhy.getAccuracy();
if (accuracy) {
Serial.print("SID:"); Serial.print(sensor_id);
Serial.print(" T:"); Serial.print(s);
Serial.print("."); Serial.print(ns);
Serial.print(" x:"); Serial.print(data.heading * 360.0f / 32768.0f);
Serial.print(" y:"); Serial.print(data.pitch * 360.0f / 32768.0f);
Serial.print(" x:"); Serial.print(data.roll * 360.0f / 32768.0f);
Serial.print(" acc:"); Serial.print(accuracy);
/*
Serial.printf("SID: %u; T: %u.%09u; h: %f, p: %f, r: %f; acc: %u\r\n",
sensor_id,
s,
ns,
data.heading * 360.0f / 32768.0f,
data.pitch * 360.0f / 32768.0f,
data.roll * 360.0f / 32768.0f,
accuracy);
*/
} else {
Serial.print("SID:"); Serial.print(sensor_id);
Serial.print(" T:"); Serial.print(s);
Serial.print("."); Serial.print(ns);
Serial.print(" x:"); Serial.print(data.heading * 360.0f / 32768.0f);
Serial.print(" y:"); Serial.print(data.pitch * 360.0f / 32768.0f);
Serial.print(" x:"); Serial.print(data.roll * 360.0f / 32768.0f);
/*
Serial.printf("SID: %u; T: %u.%09u; h: %f, p: %f, r: %f\r\n",
sensor_id,
s,
ns,
data.heading * 360.0f / 32768.0f,
data.pitch * 360.0f / 32768.0f,
data.roll * 360.0f / 32768.0f);
*/
}
}
void setup()
{
Serial.begin(115200);
while (!Serial);
// Set the reset pin and interrupt pin, if any
bhy.setPins(BHI260AP_RST, BHI260AP_IRQ);
// Set the firmware array address and firmware size
bhy.setFirmware(firmware, fw_size, WRITE_TO_FLASH);
#if WRITE_TO_FLASH
// Set to load firmware from flash
bhy.setBootFormFlash(true);
#endif
#ifdef BHY2_USE_I2C
// Using I2C interface
// BHI260AP_SLAVE_ADDRESS_L = 0x28
// BHI260AP_SLAVE_ADDRESS_H = 0x29
if (!bhy.init(Wire, BHI260AP_SDA, BHI260AP_SCL, BHI260AP_SLAVE_ADDRESS_L)) {
Serial.print("Failed to init BHI260AP - ");
Serial.println(bhy.getError());
while (1) {
delay(1000);
}
}
#else
// Using SPI interface
if (!bhy.init(SPI, BHI260AP_CS, BHI260AP_MOSI, BHI260AP_MISO, BHI260AP_SCK)) {
Serial.print("Failed to init BHI260AP - ");
Serial.println(bhy.getError());
while (1) {
delay(1000);
}
}
#endif
Serial.println("Init BHI260AP Sensor success!");
// Output all available sensors to Serial
bhy.printSensors(Serial);
float sample_rate = 100.0; /* Read out hintr_ctrl measured at 100Hz */
uint32_t report_latency_ms = 0; /* Report immediately */
/*
* Enable Euler function
* The Euler function depends on BMM150.
* If the hardware is not connected to BMM150, the Euler function cannot be used.
* * */
bhy.configure(SENSOR_EULER_ID, sample_rate, report_latency_ms);
// Register event callback function
bhy.onResultEvent(SENSOR_EULER_ID, parse_euler);
}
void loop()
{
// Update sensor fifo
bhy.update();
delay(50);
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,179 @@
/**
*
* @license MIT License
*
* Copyright (c) 2024 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file BHI260AP_aux_BMM150_quaternion.ino
* @author Lewis He (lewishe@outlook.com)
* @date 2024-07-23
* @note Changed from Boschsensortec API https://github.com/boschsensortec/BHY2_SensorAPI
*/
#include <Wire.h>
#include <SPI.h>
#include <Arduino.h>
#include "SensorBHI260AP.hpp"
/*
Write the firmware containing the BMM150 magnetometer function into the flash.
This function requires the BHI260AP external SPI Flash.
If there is no Flash, it can only be written and run in RAM.
Example firmware source: https://github.com/boschsensortec/BHY2_SensorAPI/tree/master/firmware
You can also compile custom firmware to write
How to build custom firmware see : https://www.bosch-sensortec.com/media/boschsensortec/downloads/application_notes_1/bst-bhi260ab-an000.pdf
*/
#define WRITE_TO_FLASH 1 //Set 1 write fw to flash ,set 0 write fw to ram
#if WRITE_TO_FLASH
#include "BHI260AP_aux_BMM150-flash.fw.h"
const uint8_t *firmware = bhi26ap_aux_bmm150_flash_fw;
const size_t fw_size = sizeof(bhi26ap_aux_bmm150_flash_fw);
#else
#include "BHI260AP_aux_BMM150.fw.h"
const uint8_t *firmware = bhi26ap_aux_bmm150_fw;
const size_t fw_size = sizeof(bhi26ap_aux_bmm150_fw);
#endif
#ifdef BHY2_USE_I2C
#define BHI260AP_SDA 21
#define BHI260AP_SCL 22
#define BHI260AP_IRQ 39
#define BHI260AP_RST -1
#else
#define BHI260AP_MOSI 27
#define BHI260AP_MISO 46
#define BHI260AP_SCK 3
#define BHI260AP_CS 28
#define BHI260AP_IRQ 30
#define BHI260AP_RST -1
#endif
SensorBHI260AP bhy;
void parse_quaternion(uint8_t sensor_id, uint8_t *data_ptr, uint32_t len, uint64_t *timestamp)
{
struct bhy2_data_quaternion data;
uint32_t s, ns;
uint64_t tns;
// Function to parse FIFO frame data into orientation
bhy2_parse_quaternion(data_ptr, &data);
uint64_t _timestamp = *timestamp;
time_to_s_ns(_timestamp, &s, &ns, &tns);
Serial.print("SID:"); Serial.print(sensor_id);
Serial.print(" T:"); Serial.print(s);
Serial.print("."); Serial.print(ns);
Serial.print(" x:"); Serial.print(data.x / 16384.0f);
Serial.print(" y:"); Serial.print(data.y / 16384.0f);
Serial.print(" x:"); Serial.print(data.z / 16384.0f);
Serial.print(" w:"); Serial.print(data.w / 16384.0f);
Serial.print(" acc:"); Serial.print(((data.accuracy * 180.0f) / 16384.0f) / 3.141592653589793f);
/*
Serial.printf("SID: %u; T: %u.%09u; x: %f, y: %f, z: %f, w: %f; acc: %.2f\r\n",
sensor_id,
s,
ns,
data.x / 16384.0f,
data.y / 16384.0f,
data.z / 16384.0f,
data.w / 16384.0f,
((data.accuracy * 180.0f) / 16384.0f) / 3.141592653589793f);
*/
}
void setup()
{
Serial.begin(115200);
while (!Serial);
// Set the reset pin and interrupt pin, if any
bhy.setPins(BHI260AP_RST, BHI260AP_IRQ);
// Set the firmware array address and firmware size
bhy.setFirmware(firmware, fw_size, WRITE_TO_FLASH);
#if WRITE_TO_FLASH
// Set to load firmware from flash
bhy.setBootFormFlash(true);
#endif
#ifdef BHY2_USE_I2C
// Using I2C interface
// BHI260AP_SLAVE_ADDRESS_L = 0x28
// BHI260AP_SLAVE_ADDRESS_H = 0x29
if (!bhy.init(Wire, BHI260AP_SDA, BHI260AP_SCL, BHI260AP_SLAVE_ADDRESS_L)) {
Serial.print("Failed to init BHI260AP - ");
Serial.println(bhy.getError());
while (1) {
delay(1000);
}
}
#else
// Using SPI interface
if (!bhy.init(SPI, BHI260AP_CS, BHI260AP_MOSI, BHI260AP_MISO, BHI260AP_SCK)) {
Serial.print("Failed to init BHI260AP - ");
Serial.println(bhy.getError());
while (1) {
delay(1000);
}
}
#endif
Serial.println("Init BHI260AP Sensor success!");
// Output all available sensors to Serial
bhy.printSensors(Serial);
float sample_rate = 100.0; /* Read out hintr_ctrl measured at 100Hz */
uint32_t report_latency_ms = 0; /* Report immediately */
/*
* Enable Euler function
* The Euler function depends on BMM150.
* If the hardware is not connected to BMM150, the Euler function cannot be used.
* * */
bhy.configure(QUAT_SENSOR_ID, sample_rate, report_latency_ms);
// Register event callback function
bhy.onResultEvent(QUAT_SENSOR_ID, parse_quaternion);
}
void loop()
{
// Update sensor fifo
bhy.update();
delay(50);
}
@@ -0,0 +1,140 @@
/**
*
* @license MIT License
*
* Copyright (c) 2022 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file BMA423_Feature.ino
* @author Lewis He (lewishe@outlook.com)
* @date 2023-04-03
*
*/
#include <Wire.h>
#include <SPI.h>
#include <Arduino.h>
#include "SensorBMA423.hpp"
#ifndef SENSOR_SDA
#define SENSOR_SDA 21
#endif
#ifndef SENSOR_SCL
#define SENSOR_SCL 22
#endif
#ifndef SENSOR_IRQ
#define SENSOR_IRQ 39
#endif
SensorBMA423 accel;
uint32_t lastMillis;
bool sensorIRQ = false;
void setFlag()
{
sensorIRQ = true;
}
void setup()
{
Serial.begin(115200);
while (!Serial);
pinMode(SENSOR_IRQ, INPUT);
attachInterrupt(SENSOR_IRQ, setFlag, RISING);
if (!accel.begin(Wire, BMA423_SLAVE_ADDRESS, SENSOR_SDA, SENSOR_SCL)) {
Serial.println("Failed to find BMA423 - check your wiring!");
while (1) {
delay(1000);
}
}
Serial.println("Init BAM423 Sensor success!");
//Default 4G ,200HZ
accel.configAccelerometer();
// Enable acceleration sensor
accel.enableAccelerometer();
// Enable pedometer steps
accel.enablePedometer();
// Emptying the pedometer steps
accel.resetPedometer();
// Enable sensor features
accel.enableFeature(SensorBMA423::FEATURE_STEP_CNTR |
SensorBMA423::FEATURE_ANY_MOTION |
SensorBMA423::FEATURE_ACTIVITY |
SensorBMA423::FEATURE_TILT |
SensorBMA423::FEATURE_WAKEUP,
true);
// Pedometer interrupt enable
accel.enablePedometerIRQ();
// Tilt interrupt enable
accel.enableTiltIRQ();
// DoubleTap interrupt enable
accel.enableWakeupIRQ();
// Any motion / no motion interrupt enable
accel.enableAnyNoMotionIRQ();
// Activity interruption enable
accel.enableActivityIRQ();
// Chip interrupt function enable
accel.configInterrupt();
}
void loop()
{
if (sensorIRQ) {
sensorIRQ = false;
// The interrupt status must be read after an interrupt is detected
uint16_t status = accel.readIrqStatus();
Serial.printf("Accelerometer interrupt mask : 0x%x\n", status);
if (accel.isPedometer()) {
uint32_t stepCounter = accel.getPedometerCounter();
Serial.print("Step count interrupt,step Counter:");
Serial.println(stepCounter);
}
if (accel.isActivity()) {
Serial.println("Activity interrupt");
}
if (accel.isTilt()) {
Serial.println("Tilt interrupt");
}
if (accel.isDoubleTap()) {
Serial.println("DoubleTap interrupt");
}
if (accel.isAnyNoMotion()) {
Serial.println("Any motion / no motion interrupt");
}
}
delay(50);
}
@@ -0,0 +1,102 @@
/**
*
* @license MIT License
*
* Copyright (c) 2023 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file BMM150_GetDataExample.ino
* @author Lewis He (lewishe@outlook.com)
* @date 2023-10-10
*
*/
#include <Wire.h>
#include <SPI.h>
#include <Arduino.h>
#include "SensorBMM150.hpp"
#include "SensorWireHelper.h"
#ifndef SENSOR_SDA
#define SENSOR_SDA 33
#endif
#ifndef SENSOR_SCL
#define SENSOR_SCL 35
#endif
#ifndef SENSOR_IRQ
#define SENSOR_IRQ -1
#endif
#ifndef SENSOR_RST
#define SENSOR_RST -1
#endif
SensorBMM150 bmm;
void setup()
{
Serial.begin(115200);
while (!Serial);
// Using I2C interface
if (!bmm.init(Wire, SENSOR_SDA, SENSOR_SCL, BMM150_I2C_ADDRESS_CSB_HIGH_SDO_LOW)) {
Serial.print("Failed to init BMM150 - check your wiring!");
while (1) {
delay(1000);
}
}
Serial.println("Init BMM150 Sensor success!");
/* Set magnetometer run mode */
/**
* * POWERMODE_NORMAL,
* * POWERMODE_FORCED,
* * POWERMODE_SLEEP,
* * POWERMODE_SUSPEND,
*/
bmm.setMode(SensorBMM150::POWERMODE_NORMAL);
}
void loop()
{
int16_t x, y, z;
/* Read mag data */
/* Unit for magnetometer data is microtesla(uT) */
if (bmm.getMag(x, y, z)) {
Serial.print("X:");
Serial.print(x);
Serial.print("uT,");
Serial.print("Y:");
Serial.print(y);
Serial.print("uT,");
Serial.print("Z:");
Serial.print(z);
Serial.println("uT");
}
delay(50);
}
@@ -0,0 +1,97 @@
/**
*
* @license MIT License
*
* Copyright (c) 2022 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file CM32181_LightSensor.ino
* @author Lewis He (lewishe@outlook.com)
* @date 2023-04-14
*
*/
#include <Wire.h>
#include <SPI.h>
#include <Arduino.h>
#include "SensorCM32181.hpp"
#ifndef SENSOR_SDA
#define SENSOR_SDA 39
#endif
#ifndef SENSOR_SCL
#define SENSOR_SCL 40
#endif
#ifndef SENSOR_IRQ
#define SENSOR_IRQ 1
#endif
SensorCM32181 light;
void setup()
{
Serial.begin(115200);
while (!Serial);
pinMode(SENSOR_IRQ, INPUT_PULLUP);
if (!light.begin(Wire, CM32181_SLAVE_ADDRESS, SENSOR_SDA, SENSOR_SCL)) {
Serial.println("Failed to find CM32181 - check your wiring!");
while (1) {
delay(1000);
}
}
Serial.println("Init CM32181 Sensor success!");
int id = light.getChipID();
Serial.print("CM32181 ID = "); Serial.println(id);
/*
Sensitivity mode selection
SAMPLING_X1
SAMPLING_X2
SAMPLING_X1_8
SAMPLING_X1_4
*/
light.setSampling(SensorCM32181::SAMPLING_X2);
//Power On sensor
light.powerOn();
}
void loop()
{
// Get raw data
uint16_t raw = light.getRaw();
// Get conversion data , The manual does not provide information on how to obtain the
// calibration value, now use the calibration value 0.28 provided by the manual
float lux = light.getLux();
Serial.printf("RAW:%u Lux:%.2f\n", raw, lux);
delay(500);
}
@@ -0,0 +1,134 @@
/**
*
* @license MIT License
*
* Copyright (c) 2023 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file CM32181_LightSensorInterrupt.ino
* @author Lewis He (lewishe@outlook.com)
* @date 2023-09-13
*
*/
#include <Wire.h>
#include <SPI.h>
#include <Arduino.h>
#include "SensorCM32181.hpp"
#ifndef SENSOR_SDA
#define SENSOR_SDA 39
#endif
#ifndef SENSOR_SCL
#define SENSOR_SCL 40
#endif
#ifndef SENSOR_IRQ
#define SENSOR_IRQ 1
#endif
SensorCM32181 light;
void setup()
{
Serial.begin(115200);
while (!Serial);
pinMode(SENSOR_IRQ, INPUT_PULLUP);
if (!light.begin(Wire, CM32181_SLAVE_ADDRESS, SENSOR_SDA, SENSOR_SCL)) {
Serial.println("Failed to find CM32181 - check your wiring!");
while (1) {
delay(1000);
}
}
Serial.println("Init CM32181 Sensor success!");
int id = light.getChipID();
Serial.print("CM32181 ID = "); Serial.println(id);
/*
Sensitivity mode selection
SAMPLING_X1
SAMPLING_X2
SAMPLING_X1_8
SAMPLING_X1_4
*/
light.setSampling(SensorCM32181::SAMPLING_X2,
SensorCM32181::INTEGRATION_TIME_400MS
);
// Set high and low thresholds. If the threshold is higher or lower than the set threshold, an interrupt will be triggered.
uint16_t lowThresholdRaw = 100;
uint16_t highThresholdRaw = 250;
light.setIntThreshold(lowThresholdRaw, highThresholdRaw);
//Power On sensor
light.powerOn();
// Turn on interrupt
light.enableINT();
}
void loop()
{
int pinVal = digitalRead(SENSOR_IRQ) ;
if (pinVal == LOW) {
// After triggering the interrupt, the interrupt status must be read
SensorCM32181::InterruptEvent event = light.getIrqStatus();
// Turn off interrupts
light.disableINT();
// Get Status
switch (event) {
case SensorCM32181::ALS_EVENT_LOW_TRIGGER:
Serial.println("Low interrupt event");
break;
case SensorCM32181::ALS_EVENT_HIGH_TRIGGER:
Serial.println("High interrupt event");
break;
default:
Serial.println("This is an impossible place to reach");
break;
}
// Get raw data
uint16_t raw = light.getRaw();
// Get conversion data , The manual does not provide information on how to obtain the
// calibration value, now use the calibration value 0.28 provided by the manual
float lux = light.getLux();
Serial.printf("IRQ:%d RAW:%u Lux:%.2f\n", pinVal, raw, lux);
// Turn on interrupts
light.enableINT();
}
delay(800);
}
@@ -0,0 +1,468 @@
/**
*
* @license MIT License
*
* Copyright (c) 2022 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file DRV2605_Basic.ino
* @author Lewis He (lewishe@outlook.com)
* @date 2023-04-03
*
*/
#include <Wire.h>
#include <SPI.h>
#include <Arduino.h>
#include "SensorDRV2605.hpp"
#ifndef SENSOR_SDA
#define SENSOR_SDA 21
#endif
#ifndef SENSOR_SCL
#define SENSOR_SCL 22
#endif
#ifndef SENSOR_IRQ
#define SENSOR_IRQ 39
#endif
SensorDRV2605 drv;
uint8_t effect = 1;
void setup()
{
Serial.begin(115200);
while (!Serial);
pinMode(SENSOR_IRQ, INPUT);
if (!drv.begin(Wire, DRV2605_SLAVE_ADDRESS, SENSOR_SDA, SENSOR_SCL)) {
Serial.println("Failed to find DRV2605 - check your wiring!");
while (1) {
delay(1000);
}
}
Serial.println("Init DRV2605 Sensor success!");
drv.selectLibrary(1);
// I2C trigger by sending 'run' command
// default, internal trigger when sending RUN command
drv.setMode(DRV2605_MODE_INTTRIG);
}
void loop()
{
Serial.print("Effect #"); Serial.println(effect);
if (effect == 1) {
Serial.println("11.2 Waveform Library Effects List");
}
if (effect == 1) {
Serial.println(F("1 - Strong Click - 100%"));
}
if (effect == 2) {
Serial.println(F("2 - Strong Click - 60%"));
}
if (effect == 3) {
Serial.println(F("3 - Strong Click - 30%"));
}
if (effect == 4) {
Serial.println(F("4 - Sharp Click - 100%"));
}
if (effect == 5) {
Serial.println(F("5 - Sharp Click - 60%"));
}
if (effect == 6) {
Serial.println(F("6 - Sharp Click - 30%"));
}
if (effect == 7) {
Serial.println(F("7 - Soft Bump - 100%"));
}
if (effect == 8) {
Serial.println(F("8 - Soft Bump - 60%"));
}
if (effect == 9) {
Serial.println(F("9 - Soft Bump - 30%"));
}
if (effect == 10) {
Serial.println(F("10 - Double Click - 100%"));
}
if (effect == 11) {
Serial.println(F("11 - Double Click - 60%"));
}
if (effect == 12) {
Serial.println(F("12 - Triple Click - 100%"));
}
if (effect == 13) {
Serial.println(F("13 - Soft Fuzz - 60%"));
}
if (effect == 14) {
Serial.println(F("14 - Strong Buzz - 100%"));
}
if (effect == 15) {
Serial.println(F("15 - 750 ms Alert 100%"));
}
if (effect == 16) {
Serial.println(F("16 - 1000 ms Alert 100%"));
}
if (effect == 17) {
Serial.println(F("17 - Strong Click 1 - 100%"));
}
if (effect == 18) {
Serial.println(F("18 - Strong Click 2 - 80%"));
}
if (effect == 19) {
Serial.println(F("19 - Strong Click 3 - 60%"));
}
if (effect == 20) {
Serial.println(F("20 - Strong Click 4 - 30%"));
}
if (effect == 21) {
Serial.println(F("21 - Medium Click 1 - 100%"));
}
if (effect == 22) {
Serial.println(F("22 - Medium Click 2 - 80%"));
}
if (effect == 23) {
Serial.println(F("23 - Medium Click 3 - 60%"));
}
if (effect == 24) {
Serial.println(F("24 - Sharp Tick 1 - 100%"));
}
if (effect == 25) {
Serial.println(F("25 - Sharp Tick 2 - 80%"));
}
if (effect == 26) {
Serial.println(F("26 - Sharp Tick 3 - 60%"));
}
if (effect == 27) {
Serial.println(F("27 - Short Double Click Strong 1 - 100%"));
}
if (effect == 28) {
Serial.println(F("28 - Short Double Click Strong 2 - 80%"));
}
if (effect == 29) {
Serial.println(F("29 - Short Double Click Strong 3 - 60%"));
}
if (effect == 30) {
Serial.println(F("30 - Short Double Click Strong 4 - 30%"));
}
if (effect == 31) {
Serial.println(F("31 - Short Double Click Medium 1 - 100%"));
}
if (effect == 32) {
Serial.println(F("32 - Short Double Click Medium 2 - 80%"));
}
if (effect == 33) {
Serial.println(F("33 - Short Double Click Medium 3 - 60%"));
}
if (effect == 34) {
Serial.println(F("34 - Short Double Sharp Tick 1 - 100%"));
}
if (effect == 35) {
Serial.println(F("35 - Short Double Sharp Tick 2 - 80%"));
}
if (effect == 36) {
Serial.println(F("36 - Short Double Sharp Tick 3 - 60%"));
}
if (effect == 37) {
Serial.println(F("37 - Long Double Sharp Click Strong 1 - 100%"));
}
if (effect == 38) {
Serial.println(F("38 - Long Double Sharp Click Strong 2 - 80%"));
}
if (effect == 39) {
Serial.println(F("39 - Long Double Sharp Click Strong 3 - 60%"));
}
if (effect == 40) {
Serial.println(F("40 - Long Double Sharp Click Strong 4 - 30%"));
}
if (effect == 41) {
Serial.println(F("41 - Long Double Sharp Click Medium 1 - 100%"));
}
if (effect == 42) {
Serial.println(F("42 - Long Double Sharp Click Medium 2 - 80%"));
}
if (effect == 43) {
Serial.println(F("43 - Long Double Sharp Click Medium 3 - 60%"));
}
if (effect == 44) {
Serial.println(F("44 - Long Double Sharp Tick 1 - 100%"));
}
if (effect == 45) {
Serial.println(F("45 - Long Double Sharp Tick 2 - 80%"));
}
if (effect == 46) {
Serial.println(F("46 - Long Double Sharp Tick 3 - 60%"));
}
if (effect == 47) {
Serial.println(F("47 - Buzz 1 - 100%"));
}
if (effect == 48) {
Serial.println(F("48 - Buzz 2 - 80%"));
}
if (effect == 49) {
Serial.println(F("49 - Buzz 3 - 60%"));
}
if (effect == 50) {
Serial.println(F("50 - Buzz 4 - 40%"));
}
if (effect == 51) {
Serial.println(F("51 - Buzz 5 - 20%"));
}
if (effect == 52) {
Serial.println(F("52 - Pulsing Strong 1 - 100%"));
}
if (effect == 53) {
Serial.println(F("53 - Pulsing Strong 2 - 60%"));
}
if (effect == 54) {
Serial.println(F("54 - Pulsing Medium 1 - 100%"));
}
if (effect == 55) {
Serial.println(F("55 - Pulsing Medium 2 - 60%"));
}
if (effect == 56) {
Serial.println(F("56 - Pulsing Sharp 1 - 100%"));
}
if (effect == 57) {
Serial.println(F("57 - Pulsing Sharp 2 - 60%"));
}
if (effect == 58) {
Serial.println(F("58 - Transition Click 1 - 100%"));
}
if (effect == 59) {
Serial.println(F("59 - Transition Click 2 - 80%"));
}
if (effect == 60) {
Serial.println(F("60 - Transition Click 3 - 60%"));
}
if (effect == 61) {
Serial.println(F("61 - Transition Click 4 - 40%"));
}
if (effect == 62) {
Serial.println(F("62 - Transition Click 5 - 20%"));
}
if (effect == 63) {
Serial.println(F("63 - Transition Click 6 - 10%"));
}
if (effect == 64) {
Serial.println(F("64 - Transition Hum 1 - 100%"));
}
if (effect == 65) {
Serial.println(F("65 - Transition Hum 2 - 80%"));
}
if (effect == 66) {
Serial.println(F("66 - Transition Hum 3 - 60%"));
}
if (effect == 67) {
Serial.println(F("67 - Transition Hum 4 - 40%"));
}
if (effect == 68) {
Serial.println(F("68 - Transition Hum 5 - 20%"));
}
if (effect == 69) {
Serial.println(F("69 - Transition Hum 6 - 10%"));
}
if (effect == 70) {
Serial.println(F("70 - Transition Ramp Down Long Smooth 1 - 100 to 0%"));
}
if (effect == 71) {
Serial.println(F("71 - Transition Ramp Down Long Smooth 2 - 100 to 0%"));
}
if (effect == 72) {
Serial.println(F("72 - Transition Ramp Down Medium Smooth 1 - 100 to 0%"));
}
if (effect == 73) {
Serial.println(F("73 - Transition Ramp Down Medium Smooth 2 - 100 to 0%"));
}
if (effect == 74) {
Serial.println(F("74 - Transition Ramp Down Short Smooth 1 - 100 to 0%"));
}
if (effect == 75) {
Serial.println(F("75 - Transition Ramp Down Short Smooth 2 - 100 to 0%"));
}
if (effect == 76) {
Serial.println(F("76 - Transition Ramp Down Long Sharp 1 - 100 to 0%"));
}
if (effect == 77) {
Serial.println(F("77 - Transition Ramp Down Long Sharp 2 - 100 to 0%"));
}
if (effect == 78) {
Serial.println(F("78 - Transition Ramp Down Medium Sharp 1 - 100 to 0%"));
}
if (effect == 79) {
Serial.println(F("79 - Transition Ramp Down Medium Sharp 2 - 100 to 0%"));
}
if (effect == 80) {
Serial.println(F("80 - Transition Ramp Down Short Sharp 1 - 100 to 0%"));
}
if (effect == 81) {
Serial.println(F("81 - Transition Ramp Down Short Sharp 2 - 100 to 0%"));
}
if (effect == 82) {
Serial.println(F("82 - Transition Ramp Up Long Smooth 1 - 0 to 100%"));
}
if (effect == 83) {
Serial.println(F("83 - Transition Ramp Up Long Smooth 2 - 0 to 100%"));
}
if (effect == 84) {
Serial.println(F("84 - Transition Ramp Up Medium Smooth 1 - 0 to 100%"));
}
if (effect == 85) {
Serial.println(F("85 - Transition Ramp Up Medium Smooth 2 - 0 to 100%"));
}
if (effect == 86) {
Serial.println(F("86 - Transition Ramp Up Short Smooth 1 - 0 to 100%"));
}
if (effect == 87) {
Serial.println(F("87 - Transition Ramp Up Short Smooth 2 - 0 to 100%"));
}
if (effect == 88) {
Serial.println(F("88 - Transition Ramp Up Long Sharp 1 - 0 to 100%"));
}
if (effect == 89) {
Serial.println(F("89 - Transition Ramp Up Long Sharp 2 - 0 to 100%"));
}
if (effect == 90) {
Serial.println(F("90 - Transition Ramp Up Medium Sharp 1 - 0 to 100%"));
}
if (effect == 91) {
Serial.println(F("91 - Transition Ramp Up Medium Sharp 2 - 0 to 100%"));
}
if (effect == 92) {
Serial.println(F("92 - Transition Ramp Up Short Sharp 1 - 0 to 100%"));
}
if (effect == 93) {
Serial.println(F("93 - Transition Ramp Up Short Sharp 2 - 0 to 100%"));
}
if (effect == 94) {
Serial.println(F("94 - Transition Ramp Down Long Smooth 1 - 50 to 0%"));
}
if (effect == 95) {
Serial.println(F("95 - Transition Ramp Down Long Smooth 2 - 50 to 0%"));
}
if (effect == 96) {
Serial.println(F("96 - Transition Ramp Down Medium Smooth 1 - 50 to 0%"));
}
if (effect == 97) {
Serial.println(F("97 - Transition Ramp Down Medium Smooth 2 - 50 to 0%"));
}
if (effect == 98) {
Serial.println(F("98 - Transition Ramp Down Short Smooth 1 - 50 to 0%"));
}
if (effect == 99) {
Serial.println(F("99 - Transition Ramp Down Short Smooth 2 - 50 to 0%"));
}
if (effect == 100) {
Serial.println(F("100 - Transition Ramp Down Long Sharp 1 - 50 to 0%"));
}
if (effect == 101) {
Serial.println(F("101 - Transition Ramp Down Long Sharp 2 - 50 to 0%"));
}
if (effect == 102) {
Serial.println(F("102 - Transition Ramp Down Medium Sharp 1 - 50 to 0%"));
}
if (effect == 103) {
Serial.println(F("103 - Transition Ramp Down Medium Sharp 2 - 50 to 0%"));
}
if (effect == 104) {
Serial.println(F("104 - Transition Ramp Down Short Sharp 1 - 50 to 0%"));
}
if (effect == 105) {
Serial.println(F("105 - Transition Ramp Down Short Sharp 2 - 50 to 0%"));
}
if (effect == 106) {
Serial.println(F("106 - Transition Ramp Up Long Smooth 1 - 0 to 50%"));
}
if (effect == 107) {
Serial.println(F("107 - Transition Ramp Up Long Smooth 2 - 0 to 50%"));
}
if (effect == 108) {
Serial.println(F("108 - Transition Ramp Up Medium Smooth 1 - 0 to 50%"));
}
if (effect == 109) {
Serial.println(F("109 - Transition Ramp Up Medium Smooth 2 - 0 to 50%"));
}
if (effect == 110) {
Serial.println(F("110 - Transition Ramp Up Short Smooth 1 - 0 to 50%"));
}
if (effect == 111) {
Serial.println(F("111 - Transition Ramp Up Short Smooth 2 - 0 to 50%"));
}
if (effect == 112) {
Serial.println(F("112 - Transition Ramp Up Long Sharp 1 - 0 to 50%"));
}
if (effect == 113) {
Serial.println(F("113 - Transition Ramp Up Long Sharp 2 - 0 to 50%"));
}
if (effect == 114) {
Serial.println(F("114 - Transition Ramp Up Medium Sharp 1 - 0 to 50%"));
}
if (effect == 115) {
Serial.println(F("115 - Transition Ramp Up Medium Sharp 2 - 0 to 50%"));
}
if (effect == 116) {
Serial.println(F("116 - Transition Ramp Up Short Sharp 1 - 0 to 50%"));
}
if (effect == 117) {
Serial.println(F("117 - Transition Ramp Up Short Sharp 2 - 0 to 50%"));
}
if (effect == 118) {
Serial.println(F("118 - Long buzz for programmatic stopping - 100%"));
}
if (effect == 119) {
Serial.println(F("119 - Smooth Hum 1 (No kick or brake pulse) - 50%"));
}
if (effect == 120) {
Serial.println(F("120 - Smooth Hum 2 (No kick or brake pulse) - 40%"));
}
if (effect == 121) {
Serial.println(F("121 - Smooth Hum 3 (No kick or brake pulse) - 30%"));
}
if (effect == 122) {
Serial.println(F("122 - Smooth Hum 4 (No kick or brake pulse) - 20%"));
}
if (effect == 123) {
Serial.println(F("123 - Smooth Hum 5 (No kick or brake pulse) - 10%"));
}
// set the effect to play
drv.setWaveform(0, effect); // play effect
drv.setWaveform(1, 0); // end waveform
// play the effect!
drv.run();
// wait a bit
delay(500);
effect++;
if (effect > 117) effect = 1;
}
@@ -0,0 +1,9 @@
# The following five lines of boilerplate have to be in your project's
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.5)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
set(EXTRA_COMPONENT_DIRS ../../../SensorLib)
project(ESP_IDF_TouchDrv_Example)
@@ -0,0 +1,10 @@
#
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
# project subdirectory.
#
PROJECT_NAME := ESP_IDF_TouchDrv_Example
EXTRA_COMPONENT_DIRS = ../../../SensorLib
include $(IDF_PATH)/make/project.mk
@@ -0,0 +1,181 @@
# ESP_IDF_TouchDrv_Example
```
The current example only writes the CSTxxx series touch application.
The other sensors have not been added, but the methods are the same.
```
### Configure the Project
Open the project configuration menu (`idf.py menuconfig`).
In the `SensorLib Example Configuration` menu:
* Select the PMU Type in the `SensorLib read and write methods` option,Choose according to the situation
1. Implemented using built-in read and write methods (Read and write methods are provided internally by SensorLib, supporting high version esp-idf >= 5.0, and low version methods (< 5.0))
2. Implemented using read and write callback methods (Implemented using externally provided methods, suitable for multiple platforms)
* In `Sensor SCL GPIO Num` select the clock pin to connect to the PMU,the default is 17
* In `Sensor SDA GPIO Num` select the data pin connected to the PMU,the default is 18
* Select the interrupt pin connected to the PMU in `Sensor Interrupt Pin`, the default is 16
* Select `Sensor reset Pin` , the default is 21
* `Master Frequency` The maximum communication frequency defaults to 100000HZ, and you can decide whether to change it according to the situation.
## How to Use Example
Before project configuration and build, be sure to set the correct chip target using `idf.py set-target <chip_name>`. default use **esp32s3**
### Build and Flash
Run `idf.py -p PORT flash monitor` to build, flash and monitor the project.
(To exit the serial monitor, type ``Ctrl-]``.)
See the [Getting Started Guide](https://docs.espressif.com/projects/esp-idf/en/latest/get-started/index.html) for full steps to configure and use ESP-IDF to build projects.
## Example Output
The output information is to configure the output voltage and enable status of the PMU
```
ESP-ROM:esp32s3-20210327
Build:Mar 27 2021
rst:0x15 (USB_UART_CHIP_RESET),boot:0xa (SPI_FAST_FLASH_BOOT)
Saved PC:0x403788f6
SPIWP:0xee
mode:DIO, clock div:1
load:0x3fce3810,len:0x178c
load:0x403c9700,len:0x4
load:0x403c9704,len:0xbfc
load:0x403cc700,len:0x2d64
entry 0x403c9900
I (26) boot: ESP-IDF v5.3-dev-1288-g5524b692ee 2nd stage bootloader
I (27) boot: compile time Jan 10 2024 22:49:17
I (27) boot: Multicore bootloader
I (31) boot: chip revision: v0.2
I (35) boot.esp32s3: Boot SPI Speed : 80MHz
I (40) boot.esp32s3: SPI Mode : DIO
I (45) boot.esp32s3: SPI Flash Size : 2MB
I (49) boot: Enabling RNG early entropy source...
I (55) boot: Partition Table:
I (58) boot: ## Label Usage Type ST Offset Length
I (66) boot: 0 nvs WiFi data 01 02 00009000 00006000
I (73) boot: 1 phy_init RF data 01 01 0000f000 00001000
I (81) boot: 2 factory factory app 00 00 00010000 00100000
I (88) boot: End of partition table
I (92) esp_image: segment 0: paddr=00010020 vaddr=3c020020 size=0e0dch ( 57564) map
I (111) esp_image: segment 1: paddr=0001e104 vaddr=3fc93100 size=01f14h ( 7956) load
I (113) esp_image: segment 2: paddr=00020020 vaddr=42000020 size=1acb0h (109744) map
I (137) esp_image: segment 3: paddr=0003acd8 vaddr=3fc95014 size=00994h ( 2452) load
I (138) esp_image: segment 4: paddr=0003b674 vaddr=40374000 size=0f0e0h ( 61664) load
I (163) boot: Loaded app from partition at offset 0x10000
I (163) boot: Disabling RNG early entropy source...
I (163) cpu_start: Multicore app
I (176) cpu_start: Pro cpu start user code
I (176) cpu_start: cpu freq: 240000000 Hz
I (177) cpu_start: Application information:
I (177) cpu_start: Project name: ESP_IDF_TouchDrv_Example
I (177) cpu_start: App version: v0.1.4-6-gfeaf675-dirty
I (177) cpu_start: Compile time: Jan 11 2024 14:01:31
I (177) cpu_start: ELF file SHA256: 3ce1527fc...
I (178) cpu_start: ESP-IDF: v5.3-dev-1288-g5524b692ee
I (178) cpu_start: Min chip rev: v0.0
I (178) cpu_start: Max chip rev: v0.99
I (178) cpu_start: Chip rev: v0.2
I (178) heap_init: Initializing. RAM available for dynamic allocation:
I (179) heap_init: At 3FC96290 len 00053480 (333 KiB): RAM
I (179) heap_init: At 3FCE9710 len 00005724 (21 KiB): RAM
I (179) heap_init: At 3FCF0000 len 00008000 (32 KiB): DRAM
I (180) heap_init: At 600FE010 len 00001FD8 (7 KiB): RTCRAM
I (181) spi_flash: detected chip: winbond
I (181) spi_flash: flash io: dio
W (181) spi_flash: Detected size(16384k) larger than the size in the binary image header(2048k). Using the size in the binary image header.
W (181) i2c: This driver is an old driver, please migrate your application code to adapt `driver/i2c_master.h`
I (182) sleep: Configure to isolate all GPIO pins in sleep state
I (182) sleep: Enable automatic switching of GPIO sleep configuration
I (183) main_task: Started on CPU0
I (193) main_task: Calling app_main()
I (193) gpio: GPIO[16]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:2
I (193) I2C: Implemented using read and write callback methods (Use lower version < 5.0 API)
I (193) mian: I2C initialized successfully
I (193) TOUCH: ----touch_drv_init----
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- 15 -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
I (303) TOUCH: Find touch address 0x15
I (303) TOUCH: Find touch address 0x15
I (303) TOUCH: Implemented using built-in read and write methods (Use lower version < 5.0 API)
Using ESP-IDF Driver interface.
E (303) i2c: i2c driver install error
I (303) gpio: GPIO[21]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0
I (303) gpio: GPIO[21]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0
Chip ID:0xb5
Version :0x2
Touch type:CST816T
I (383) TOUCH: Init touch SUCCESS!
I (383) TOUCH: Some CST816 will automatically enter sleep,can use the touch.disableAutoSleep() to turn off
I (383) gpio: GPIO[21]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0
I (513) mian: Run...
I (513) main_task: Returned from app_main()
X[0]:143 Y[0]:127
X[0]:143 Y[0]:127
X[0]:142 Y[0]:127
X[0]:141 Y[0]:126
X[0]:138 Y[0]:125
X[0]:135 Y[0]:123
X[0]:131 Y[0]:121
X[0]:126 Y[0]:119
X[0]:121 Y[0]:117
X[0]:84 Y[0]:146
X[0]:85 Y[0]:146
X[0]:86 Y[0]:145
X[0]:87 Y[0]:143
X[0]:88 Y[0]:140
X[0]:89 Y[0]:136
X[0]:90 Y[0]:132
X[0]:91 Y[0]:127
X[0]:92 Y[0]:123
X[0]:93 Y[0]:119
X[0]:94 Y[0]:115
X[0]:95 Y[0]:111
I (513563) TOUCH: Touch Home button pressed!
I (513573) TOUCH: Touch Home button pressed!
I (513583) TOUCH: Touch Home button pressed!
I (513603) TOUCH: Touch Home button pressed!
I (513613) TOUCH: Touch Home button pressed!
I (513633) TOUCH: Touch Home button pressed!
I (513643) TOUCH: Touch Home button pressed!
```
## Build process example
Assuming you don't have esp-idf yet
```
mkdir -p ~/esp
cd ~/esp
git clone --recursive https://github.com/espressif/esp-idf.git
git clone https://github.com/lewisxhe/XPowersLib.git
cd esp-idf
./install.sh
. ./export.sh
cd ..
cd SensorLib/examples/ESP_IDF_Example
Configure SDA,SCL,INT,RST,Pin
...
idf.py menuconfig
idf.py build
idf.py -b 921600 flash
idf.py monitor
```
@@ -0,0 +1,5 @@
idf_component_register(SRCS
"touch_drv.cpp"
"i2c_driver.cpp"
"main.cpp"
INCLUDE_DIRS ".")
@@ -0,0 +1,52 @@
menu "SensorLib Example Configuration"
choice I2C_COMMUNICATION_METHOD
prompt "SensorLib read and write methods"
default I2C_COMMUNICATION_METHOD_BUILTIN_RW
help
Define SensorLib read and write methods
config I2C_COMMUNICATION_METHOD_BUILTIN_RW
bool "Implemented using built-in read and write methods"
config I2C_COMMUNICATION_METHOD_CALLBACK_RW
bool "Implemented using read and write callback methods"
endchoice
config I2C_MASTER_PORT_NUM
int "Sensor I2C Port Number"
default 1
help
Port number for I2C Master device.
config I2C_MASTER_FREQUENCY
int "Master Frequency"
default 100000
help
I2C Speed of Master device.
config SENSOR_SCL
int "Sensor SCL GPIO Num"
default 17
help
GPIO number for I2C clock line.
config SENSOR_SDA
int "Sensor SDA GPIO Num"
default 18
help
GPIO number for I2C data line.
config SENSOR_IRQ
int "Sensor Interrupt Pin"
default 16
help
Sensor interrupt pin.
config SENSOR_RST
int "Sensor reset Pin"
default 21
help
Sensor reset pin.
endmenu
@@ -0,0 +1,4 @@
#
# "main" pseudo-component makefile.
#
# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.)
@@ -0,0 +1,253 @@
/**
* @file port_i2c.cpp
* @author Lewis He (lewishe@outlook.com)
* @license MIT
* @copyright Copyright (c) 2024 Shenzhen Xinyuan Electronic Technology Co., Ltd
* @date 2024-01-10
*
*/
#include <cstring>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "esp_log.h"
#include "sdkconfig.h"
#include "i2c_driver.h"
static const char *TAG = "I2C";
#define I2C_MASTER_NUM (i2c_port_t)CONFIG_I2C_MASTER_PORT_NUM
#define I2C_MASTER_SDA_IO (gpio_num_t)CONFIG_SENSOR_SDA
#define I2C_MASTER_SCL_IO (gpio_num_t)CONFIG_SENSOR_SCL
#define I2C_MASTER_FREQ_HZ CONFIG_I2C_MASTER_FREQUENCY /*!< I2C master clock frequency */
#define I2C_MASTER_TX_BUF_DISABLE 0 /*!< I2C master doesn't need buffer */
#define I2C_MASTER_RX_BUF_DISABLE 0 /*!< I2C master doesn't need buffer */
#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5,0,0)) && defined(CONFIG_SENSORLIB_ESP_IDF_NEW_API)
#include "soc/clk_tree_defs.h"
i2c_master_dev_handle_t i2c_device;
// * Using the new API of esp-idf 5.x, you need to pass the I2C BUS handle,
// * which is useful when the bus shares multiple devices.
i2c_master_bus_handle_t bus_handle;
#endif
#if CONFIG_I2C_COMMUNICATION_METHOD_CALLBACK_RW
/**
* @brief Read a sequence of bytes from a pmu registers
*/
int i2c_read_callback(uint8_t devAddr, uint8_t regAddr, uint8_t *data, uint8_t len)
{
esp_err_t ret;
if (len == 0) {
return ESP_OK;
}
if (data == NULL) {
return ESP_FAIL;
}
#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5,0,0)) && defined(CONFIG_SENSORLIB_ESP_IDF_NEW_API)
ret = i2c_master_transmit_receive(
i2c_device,
(const uint8_t *)&regAddr,
1,
data,
len,
-1);
#else
i2c_cmd_handle_t cmd;
cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (devAddr << 1) | I2C_MASTER_WRITE, true);
i2c_master_write_byte(cmd, regAddr, true);
i2c_master_stop(cmd);
ret = i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, pdTICKS_TO_MS(1000));
i2c_cmd_link_delete(cmd);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "PMU i2c_master_cmd_begin FAILED! > ");
return ESP_FAIL;
}
cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (devAddr << 1) | I2C_MASTER_READ, true);
if (len > 1) {
i2c_master_read(cmd, data, len - 1, I2C_MASTER_ACK);
}
i2c_master_read_byte(cmd, &data[len - 1], I2C_MASTER_NACK);
i2c_master_stop(cmd);
ret = i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, pdTICKS_TO_MS(1000));
i2c_cmd_link_delete(cmd);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "i2c_read_error.");
}
#endif
return ret == ESP_OK ? 0 : -1;
}
/**
* @brief Write a byte to a pmu register
*/
int i2c_write_callback(uint8_t devAddr, uint8_t regAddr, uint8_t *data, uint8_t len)
{
esp_err_t ret;
if (data == NULL) {
return ESP_FAIL;
}
#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5,0,0)) && defined(CONFIG_SENSORLIB_ESP_IDF_NEW_API)
uint8_t *write_buffer = (uint8_t *)malloc(sizeof(uint8_t) * (len + 1));
if (!write_buffer) {
return -1;
}
write_buffer[0] = regAddr;
memcpy(write_buffer + 1, data, len);
ret = i2c_master_transmit(
i2c_device,
write_buffer,
len + 1,
-1);
free(write_buffer);
#else
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (devAddr << 1) | I2C_MASTER_WRITE, true);
i2c_master_write_byte(cmd, regAddr, true);
i2c_master_write(cmd, data, len, true);
i2c_master_stop(cmd);
ret = i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, pdTICKS_TO_MS(1000));
i2c_cmd_link_delete(cmd);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "i2c_write_error.");
}
#endif
return ret == ESP_OK ? 0 : -1;
}
esp_err_t i2c_drv_device_init(uint8_t address)
{
#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5,0,0)) && defined(CONFIG_SENSORLIB_ESP_IDF_NEW_API)
i2c_device_config_t i2c_dev_conf = {
.dev_addr_length = I2C_ADDR_BIT_LEN_7,
.device_address = address,
.scl_speed_hz = I2C_MASTER_FREQ_HZ,
};
return i2c_master_bus_add_device(bus_handle, &i2c_dev_conf, &i2c_device);
#else
return ESP_OK;
#endif
}
#endif //CONFIG_I2C_COMMUNICATION_METHOD_CALLBACK_RW
/**
* @brief i2c master initialization
*/
esp_err_t i2c_drv_init(void)
{
#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5,0,0)) && defined(CONFIG_SENSORLIB_ESP_IDF_NEW_API)
ESP_LOGI(TAG, "Implemented using read and write callback methods (Use higher version >= 5.0 API)");
i2c_master_bus_config_t i2c_bus_config;
memset(&i2c_bus_config, 0, sizeof(i2c_bus_config));
i2c_bus_config.clk_source = I2C_CLK_SRC_DEFAULT;
i2c_bus_config.i2c_port = I2C_MASTER_NUM;
i2c_bus_config.scl_io_num = (gpio_num_t)I2C_MASTER_SCL_IO;
i2c_bus_config.sda_io_num = (gpio_num_t)I2C_MASTER_SDA_IO;
i2c_bus_config.glitch_ignore_cnt = 7;
ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_config, &bus_handle));
return ESP_OK;
#else
ESP_LOGI(TAG, "Implemented using read and write callback methods (Use lower version < 5.0 API)");
i2c_config_t i2c_conf ;
memset(&i2c_conf, 0, sizeof(i2c_conf));
i2c_conf.mode = I2C_MODE_MASTER;
i2c_conf.sda_io_num = I2C_MASTER_SDA_IO;
i2c_conf.scl_io_num = I2C_MASTER_SCL_IO;
i2c_conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
i2c_conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
i2c_conf.master.clk_speed = I2C_MASTER_FREQ_HZ;
i2c_param_config(I2C_MASTER_NUM, &i2c_conf);
return i2c_driver_install(I2C_MASTER_NUM, i2c_conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0);
#endif
}
void i2c_drv_scan()
{
#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5,0,0)) && defined(CONFIG_SENSORLIB_ESP_IDF_NEW_API)
esp_err_t err = ESP_OK;
uint8_t address = 0x00;
printf("Scand I2C Devices:\n");
printf(" 0 1 2 3 4 5 6 7 8 9 a b c d e f\r\n");
for (int i = 0; i < 128; i += 16) {
printf("%02x: ", i);
for (int j = 0; j < 16; j++) {
fflush(stdout);
address = i + j;
err = i2c_master_probe(bus_handle, address, 1000);
if (err == ESP_OK) {
printf("%02x ", address);
} else if (err == ESP_ERR_TIMEOUT) {
printf("UU ");
} else {
printf("-- ");
}
}
printf("\r\n");
}
printf("\n\n\n");
#else
uint8_t address;
printf(" 0 1 2 3 4 5 6 7 8 9 a b c d e f\r\n");
for (int i = 0; i < 128; i += 16) {
printf("%02x: ", i);
for (int j = 0; j < 16; j++) {
fflush(stdout);
address = i + j;
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (address << 1) | I2C_MASTER_WRITE, true);
i2c_master_stop(cmd);
esp_err_t ret = i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 50 / portTICK_PERIOD_MS);
i2c_cmd_link_delete(cmd);
if (ret == ESP_OK) {
printf("%02x ", address);
} else if (ret == ESP_ERR_TIMEOUT) {
printf("UU ");
} else {
printf("-- ");
}
}
printf("\r\n");
}
#endif
}
bool i2c_drv_probe(uint8_t devAddr)
{
#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5,0,0)) && defined(CONFIG_SENSORLIB_ESP_IDF_NEW_API)
return ESP_OK == i2c_master_probe(bus_handle, devAddr, 1000);
#else
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (devAddr << 1) | I2C_MASTER_WRITE, true);
i2c_master_stop(cmd);
esp_err_t ret = i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 50 / portTICK_PERIOD_MS);
i2c_cmd_link_delete(cmd);
return (ret == ESP_OK);
#endif //ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5,0,0)
}
@@ -0,0 +1,32 @@
/**
* @file i2c_driver.h
* @author Lewis He (lewishe@outlook.com)
* @license MIT
* @copyright Copyright (c) 2024 Shenzhen Xinyuan Electronic Technology Co., Ltd
* @date 2024-01-10
*
*/
#pragma once
#include "esp_err.h"
#include "esp_log.h"
#include "driver/gpio.h"
#include "esp_idf_version.h"
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5,0,0) && defined(CONFIG_SENSORLIB_ESP_IDF_NEW_API)
#include "driver/i2c_master.h"
#else
#include "driver/i2c.h"
#endif //ESP_IDF_VERSION
esp_err_t i2c_drv_init(void);
void i2c_drv_scan();
bool i2c_drv_probe(uint8_t devAddr);
int i2c_read_callback(uint8_t devAddr, uint8_t regAddr, uint8_t *data, uint8_t len);
int i2c_write_callback(uint8_t devAddr, uint8_t regAddr, uint8_t *data, uint8_t len);
#if CONFIG_I2C_COMMUNICATION_METHOD_CALLBACK_RW
esp_err_t i2c_drv_device_init(uint8_t address);
#endif
@@ -0,0 +1,83 @@
/**
* @file main.cpp
* @author Lewis He (lewishe@outlook.com)
* @license MIT
* @copyright Copyright (c) 2024 Shenzhen Xinyuan Electronic Technology Co., Ltd
* @date 2024-01-10
*
*/
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "freertos/queue.h"
#include "i2c_driver.h"
#define SENSOR_INPUT_PIN (gpio_num_t)CONFIG_SENSOR_IRQ /*!< axp power chip interrupt Pin*/
#define SENSOR_INPUT_PIN_SEL (1ULL<<SENSOR_INPUT_PIN)
static const char *TAG = "mian";
extern esp_err_t sensor_drv_init();
extern esp_err_t touch_drv_init();
extern void touch_drv_isr_handler();
static void sensor_hander_task(void *);
static QueueHandle_t gpio_evt_queue = NULL;
static void IRAM_ATTR sensor_irq_handler(void *arg)
{
uint32_t gpio_num = (uint32_t) arg;
xQueueSendFromISR(gpio_evt_queue, &gpio_num, NULL);
}
static void irq_init()
{
gpio_config_t io_conf;
io_conf.intr_type = GPIO_INTR_NEGEDGE;
io_conf.mode = GPIO_MODE_INPUT;
io_conf.pin_bit_mask = SENSOR_INPUT_PIN_SEL;
io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
io_conf.pull_up_en = GPIO_PULLUP_ENABLE;
gpio_config(&io_conf);
gpio_set_intr_type(SENSOR_INPUT_PIN, GPIO_INTR_NEGEDGE);
//install gpio isr service
gpio_install_isr_service(0);
//hook isr handler for specific gpio pin
gpio_isr_handler_add(SENSOR_INPUT_PIN, sensor_irq_handler, (void *) SENSOR_INPUT_PIN);
}
extern "C" void app_main(void)
{
//create a queue to handle gpio event from isr
gpio_evt_queue = xQueueCreate(5, sizeof(uint32_t));
// Register Sensor interrupt pins
irq_init();
ESP_ERROR_CHECK(i2c_drv_init());
ESP_LOGI(TAG, "I2C initialized successfully");
ESP_ERROR_CHECK(touch_drv_init());
xTaskCreate(sensor_hander_task, "App/sen", 4 * 1024, NULL, 10, NULL);
ESP_LOGI(TAG, "Run...");
}
static void sensor_hander_task(void *args)
{
uint32_t io_num;
while (1) {
if (xQueueReceive(gpio_evt_queue, &io_num, portMAX_DELAY)) {
touch_drv_isr_handler();
}
}
}
@@ -0,0 +1,166 @@
/**
* @file touch_drv.cpp
* @author Lewis He (lewishe@outlook.com)
* @license MIT
* @copyright Copyright (c) 2024 Shenzhen Xinyuan Electronic Technology Co., Ltd
* @date 2024-01-10
*
*/
#include "sdkconfig.h"
#include "esp_log.h"
#include "esp_err.h"
#include "i2c_driver.h"
#include "freertos/FreeRTOS.h"
#include "TouchDrvCSTXXX.hpp"
static const char *TAG = "TOUCH";
TouchDrvCSTXXX touch;
static void touch_home_button_callback(void *user_data);
esp_err_t touch_drv_init()
{
uint8_t address = 0xFF;
ESP_LOGI(TAG, "----touch_drv_init----");
/*
* CST816, CST328 Some chips have automatic sleep function.
* If you want to scan the device address,
* must reset the chip first. If the reset pin is not connected,
* you may not be able to obtain the device address by scanning the bus.
*/
gpio_num_t gpio_num = (gpio_num_t)CONFIG_SENSOR_RST;
ESP_ERROR_CHECK(gpio_set_direction(gpio_num, GPIO_MODE_OUTPUT));
gpio_set_level(gpio_num, 0);
vTaskDelay(pdMS_TO_TICKS(15));
gpio_set_level(gpio_num, 1);
vTaskDelay(pdMS_TO_TICKS(80));
// Run bus scan
i2c_drv_scan();
// Get known address
if (i2c_drv_probe(CST816_SLAVE_ADDRESS)) {
address = CST816_SLAVE_ADDRESS;
ESP_LOGI(TAG, "Find touch address 0x%X", address);
}
if (i2c_drv_probe(CST226SE_SLAVE_ADDRESS)) {
address = CST226SE_SLAVE_ADDRESS;
ESP_LOGI(TAG, "Find touch address 0x%X", address);
}
if (i2c_drv_probe(CST328_SLAVE_ADDRESS)) {
address = CST328_SLAVE_ADDRESS;
ESP_LOGI(TAG, "Find touch address 0x%X", address);
}
if (address == 0xFF) {
ESP_LOGE(TAG, "Could't find touch chip!"); return ESP_FAIL;
}
ESP_LOGI(TAG, "Find touch address 0x%X", address);
// Set reset and interrupt pin
touch.setPins(CONFIG_SENSOR_RST, CONFIG_SENSOR_IRQ);
//* Implemented using read and write callback methods, applicable to other platforms
#if CONFIG_I2C_COMMUNICATION_METHOD_CALLBACK_RW
// * Provide the device address to the callback function
i2c_drv_device_init(address);
ESP_LOGI(TAG, "Implemented using read and write callback methods");
if (touch.begin(address, i2c_read_callback, i2c_write_callback)) {
ESP_LOGI(TAG, "Init touch SUCCESS!");
} else {
ESP_LOGE(TAG, "Init touch FAILED!");
return ESP_FAIL;
}
#endif
//* Use the built-in esp-idf communication method
#if CONFIG_I2C_COMMUNICATION_METHOD_BUILTIN_RW
#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5,0,0)) && defined(CONFIG_SENSORLIB_ESP_IDF_NEW_API)
ESP_LOGI(TAG, "Implemented using built-in read and write methods (Use higher version >= 5.0 API)");
// * Using the new API of esp-idf 5.x, you need to pass the I2C BUS handle,
// * which is useful when the bus shares multiple devices.
extern i2c_master_bus_handle_t bus_handle;
if (touch.begin(bus_handle, address)) {
ESP_LOGI(TAG, "Init touch SUCCESS!");
} else {
ESP_LOGE(TAG, "Init touch FAILED!");
return false;
}
#else
ESP_LOGI(TAG, "Implemented using built-in read and write methods (Use lower version < 5.0 API)");
if (touch.begin((i2c_port_t)CONFIG_I2C_MASTER_PORT_NUM, address, CONFIG_SENSOR_SDA, CONFIG_SENSOR_SCL)) {
ESP_LOGI(TAG, "Init touch SUCCESS!");
} else {
ESP_LOGE(TAG, "Init touch FAILED!");
return false;
}
#endif //ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5,0,0)
#endif //CONFIG_I2C_COMMUNICATION_METHOD_BUILTIN_RW
const char *model = touch.getModelName();
if (strncmp(model, "CST8", 3) == 0) {
ESP_LOGI(TAG, "Some CST816 will automatically enter sleep,can use the touch.disableAutoSleep() to turn off");
// Some CST816 will automatically enter sleep,
// can use the touch.disableAutoSleep() to turn off
touch.disableAutoSleep();
/**
* Some touch screens have touch buttons,
* and the touch coordinates need to be given
* before the touch button callback function can be used.
*/
// Example touch coordinates using T-Display-S3 touch
touch.setCenterButtonCoordinate(85, 360);
touch.setHomeButtonCallback(touch_home_button_callback);
}
// Set the maximum valid coordinates
// touch.setMaxCoordinates(320, 170);
// Swap XY coordinates
// touch.setSwapXY(true);
// Mirror XY coordinates
// touch.setMirrorXY(false, true);
return ESP_OK;
}
static void touch_home_button_callback(void *user_data)
{
ESP_LOGI(TAG, "Touch Home button pressed!");
}
void touch_drv_isr_handler()
{
int16_t x[5], y[5];
uint8_t touched = touch.getPoint(x, y, touch.getSupportTouchPoint());
if (touched) {
for (int i = 0; i < touched; ++i) {
printf("X[");
printf("%d", i);
printf("]:");
printf("%d", x[i]);
printf(" ");
printf(" Y[");
printf("%d", i);
printf("]:");
printf("%d", y[i]);
printf(" ");
}
printf("\n");
}
}
@@ -0,0 +1,6 @@
CONFIG_IDF_TARGET="esp32s3"
CONFIG_IDF_TARGET_ESP32S3=y
CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG=y
CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ_240=y
CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ=240
@@ -0,0 +1,193 @@
/**
*
* @license MIT License
*
* Copyright (c) 2023 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file LTR553ALS_Sensor.ino
* @author Lewis He (lewishe@outlook.com)
* @date 2023-09-09
*
*/
#include <Wire.h>
#include <SPI.h>
#include <Arduino.h>
#include "SensorLTR553.hpp"
#ifndef SENSOR_SDA
#define SENSOR_SDA 5
#endif
#ifndef SENSOR_SCL
#define SENSOR_SCL 6
#endif
#ifndef SENSOR_IRQ
#define SENSOR_IRQ 21
#endif
SensorLTR553 als;
void setup()
{
Serial.begin(115200);
while (!Serial);
pinMode(SENSOR_IRQ, INPUT_PULLUP);
if (!als.begin(Wire, LTR553_SLAVE_ADDRESS, SENSOR_SDA, SENSOR_SCL)) {
Serial.println("Failed to find LTR553 - check your wiring!");
while (1) {
delay(1000);
}
}
Serial.println("Init LTR553 Sensor success!");
// Set the ambient light high and low thresholds.
// If the value exceeds or falls below the set value, an interrupt will be triggered.
als.setLightSensorThreshold(10, 200);
// Set the high and low thresholds of the proximity sensor.
// If the value exceeds or falls below the set value, an interrupt will be triggered.
als.setProximityThreshold(10, 30);
// Controls the Light Sensor N number of times the measurement data is outside the range
// defined by the upper and lower threshold limits before asserting the interrupt.
als.setLightSensorPersists(5);
// Controls the Proximity N number of times the measurement data is outside the range
// defined by the upper and lower threshold limits before asserting the interrupt.
als.setProximityPersists(5);
/*
* ALS_IRQ_ACTIVE_LOW, // INT pin is considered active when it is a logic 0 (default)
* ALS_IRQ_ACTIVE_HIGH // INT pin is considered active when it is a logic 1
* * */
als.setIRQLevel(SensorLTR553::ALS_IRQ_ACTIVE_LOW);
/*
* ALS_IRQ_ONLY_PS, // Only PS measurement can trigger interrupt
* ALS_IRQ_ONLY_ALS, // Only ALS measurement can trigger interrupt
* ALS_IRQ_BOTH, // Both ALS and PS measurement can trigger interrupt
* * * */
als.enableIRQ(SensorLTR553::ALS_IRQ_BOTH);
/*
* ALS_GAIN_1X , -> 1 lux to 64k lux (default)
* ALS_GAIN_2X , -> 0.5 lux to 32k lux
* ALS_GAIN_4X , -> 0.25 lux to 16k lux
* ALS_GAIN_8X , -> 0.125 lux to 8k lux
* ALS_GAIN_48X , -> 0.02 lux to 1.3k lux
* ALS_GAIN_96X , -> 0.01 lux to 600 lux
* */
als.setLightSensorGain(SensorLTR553::ALS_GAIN_8X);
/*
* PS_LED_PLUSE_30KHZ,
* PS_LED_PLUSE_40KHZ,
* PS_LED_PLUSE_50KHZ,
* PS_LED_PLUSE_60KHZ,
* PS_LED_PLUSE_70KHZ,
* PS_LED_PLUSE_80KHZ,
* PS_LED_PLUSE_90KHZ,
* PS_LED_PLUSE_100KHZ,
* * * * * * * * * * */
als.setPsLedPulsePeriod(SensorLTR553::PS_LED_PLUSE_100KHZ);
/*
* PS_LED_DUTY_25,
* PS_LED_DUTY_50,
* PS_LED_DUTY_75,
* PS_LED_DUTY_100,
* * * */
als.setPsLedDutyCycle(SensorLTR553::PS_LED_DUTY_100);
/*
* PS_LED_CUR_5MA,
* PS_LED_CUR_10MA,
* PS_LED_CUR_20MA,
* PS_LED_CUR_50MA,
* PS_LED_CUR_100MA,
* * * * * * * */
als.setPsLedCurrnet(SensorLTR553::PS_LED_CUR_50MA);
/*
* PS_MEAS_RATE_50MS,
* PS_MEAS_RATE_70MS,
* PS_MEAS_RATE_100MS,
* PS_MEAS_RATE_200MS,
* PS_MEAS_RATE_500MS,
* PS_MEAS_RATE_1000MS,
* PS_MEAS_RATE_2000MS,
* PS_MEAS_RATE_10MS
* * * * * * * */
als.setProximityRate(SensorLTR553::PS_MEAS_RATE_200MS);
// Number of pulses
als.setPsLedPulses(1);
// Enable proximity sensor saturation indication
als.enablePsIndicator();
// Enable ambient light sensor
als.enableLightSensor();
// Enable proximity sensor
als.enableProximity();
}
bool canRead()
{
#if SENSOR_IRQ != -1
return digitalRead(SENSOR_IRQ) == LOW;
#else
static uint32_t lastReadMillis;
if (millis() > lastReadMillis) {
lastReadMillis = millis() + 500;
return true;
}
return false;
#endif
}
void loop()
{
/*
* PS Saturation Flag is used for monitoring the internal IC saturation.
* It will be flagged when the IC has reached saturation
* and not able to perform any further PS measurement
* */
bool saturated = false;
if (canRead()) {
Serial.print(" ALS: CH1:"); Serial.print(als.getLightSensor(1));
Serial.print(" - CH0:"); Serial.print(als.getLightSensor(0));
Serial.print(" - PS:"); Serial.print(als.getProximity(&saturated));
Serial.print(" - "); Serial.println(saturated ? "PS saturated" : "PS not saturated");
}
delay(5);
}
@@ -0,0 +1,214 @@
/**
*
* @license MIT License
*
* Copyright (c) 2022 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file PCF85063_AlarmByUnits.ino
* @author Lewis He (lewishe@outlook.com)
* @date 2023-12-11
*
*/
#include <Wire.h>
#include <SPI.h>
#include <Arduino.h>
#include <time.h>
#include "SensorPCF85063.hpp"
#ifndef SENSOR_SDA
#define SENSOR_SDA 4
#endif
#ifndef SENSOR_SCL
#define SENSOR_SCL 5
#endif
#ifndef SENSOR_IRQ
#define SENSOR_IRQ 14
#endif
SensorPCF85063 rtc;
uint32_t lastMillis = 0;
uint8_t nextHour = 22;
uint8_t nextMonth = 1;
uint8_t nextDay = 1;
uint8_t nextMinute = 59;
uint8_t nextSecond = 20;
void setup()
{
Serial.begin(115200);
while (!Serial);
if (!rtc.begin(Wire, PCF85063_SLAVE_ADDRESS, SENSOR_SDA, SENSOR_SCL)) {
Serial.println("Failed to find PCF8563 - check your wiring!");
while (1) {
delay(1000);
}
}
pinMode(SENSOR_IRQ, INPUT_PULLUP);
rtc.setDateTime(2022, nextMonth, nextDay, nextHour, nextMinute, nextSecond);
nextSecond = 55;
// First test alarm seconds
rtc.setAlarmBySecond(30);
rtc.enableAlarm();
}
void printDateTime()
{
if (millis() - lastMillis > 1000) {
/**
/// Format output time*
Option:
DATETIME_FORMAT_HM
DATETIME_FORMAT_HMS
DATETIME_FORMAT_YYYY_MM_DD
DATETIME_FORMAT_MM_DD_YYYY
DATETIME_FORMAT_DD_MM_YYYY
DATETIME_FORMAT_YYYY_MM_DD_H_M_S
default: DATETIME_FORMAT_YYYY_MM_DD_H_M_S_WEEK
*/
Serial.println(rtc.strftime());
lastMillis = millis();
}
}
// Test seconds timing
void testAlarmSeconds()
{
while (1) {
if (digitalRead(SENSOR_IRQ) == LOW) {
Serial.println("testAlarmSeconds Interrupt .... ");
if (rtc.isAlarmActive()) {
Serial.println("Alarm active");
rtc.resetAlarm();
rtc.setDateTime(2022, nextMonth, nextDay, nextHour, nextMinute, nextSecond);
rtc.setAlarmByMinutes(0);
return;
}
}
printDateTime();
}
}
// Test minute timing
void testAlarmMinute()
{
while (1) {
if (digitalRead(SENSOR_IRQ) == LOW) {
Serial.println("testAlarmMinute Interrupt .... ");
if (rtc.isAlarmActive()) {
Serial.println("Alarm active");
rtc.resetAlarm();
rtc.setDateTime(2022, nextMonth, nextDay, nextHour, nextMinute, nextSecond);
nextHour++;
if (nextHour >= 24) {
nextHour = 23;
nextDay = 25;
rtc.setAlarmByHours(0);
Serial.println("setAlarmByHours");
return;
}
}
}
printDateTime();
}
}
// Test hour timing
void testAlarmHour()
{
while (1) {
if (digitalRead(SENSOR_IRQ) == LOW) {
Serial.println("testAlarmHour Interrupt .... ");
if (rtc.isAlarmActive()) {
Serial.println("Alarm active");
rtc.resetAlarm();
rtc.setDateTime(2022, nextMonth, nextDay, nextHour, nextMinute, nextSecond);
nextDay++;
if (nextDay >= 30) {
nextMonth = 1;
nextHour = 23;
nextMinute = 59;
nextSecond = 55;
nextDay = rtc.getDaysInMonth(nextMonth, 2022);
rtc.setDateTime(2022, nextMonth, nextDay, nextHour, nextMinute, nextSecond);
rtc.setAlarmByDays(1);
Serial.println("setAlarmByDays");
return;
}
}
}
printDateTime();
}
}
// Test day timing
void testAlarmDay()
{
while (1) {
if (digitalRead(SENSOR_IRQ) == LOW) {
Serial.println("testAlarmDay Interrupt .... ");
if (rtc.isAlarmActive()) {
Serial.println("Alarm active");
rtc.resetAlarm();
nextDay = rtc.getDaysInMonth(nextMonth, 2022);
rtc.setDateTime(2022, nextMonth, nextDay, nextHour, nextMinute, nextSecond);
nextMonth++;
if (nextMonth >= 12) {
return;
}
}
}
printDateTime();
}
}
void loop()
{
testAlarmSeconds();
testAlarmMinute();
testAlarmHour();
testAlarmDay();
Serial.println("Test done ...");
while (1) {
delay(100);
}
}
@@ -0,0 +1,101 @@
/**
*
* @license MIT License
*
* Copyright (c) 2024 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file PCF85063_ClockOutput.ino
* @author Lewis He (lewishe@outlook.com)
* @date 2024-07-24
*
*/
#include <Wire.h>
#include <SPI.h>
#include <Arduino.h>
#include "SensorPCF85063.hpp"
#ifndef SENSOR_SDA
#define SENSOR_SDA 17
#endif
#ifndef SENSOR_SCL
#define SENSOR_SCL 18
#endif
#ifndef SENSOR_IRQ
#define SENSOR_IRQ 7
#endif
SensorPCF85063::ClockHz clock_array[] = {
SensorPCF85063::CLK_32768HZ,
SensorPCF85063::CLK_16384HZ,
SensorPCF85063::CLK_8192HZ,
SensorPCF85063::CLK_4096HZ,
SensorPCF85063::CLK_2048HZ,
SensorPCF85063::CLK_1024HZ,
SensorPCF85063::CLK_1HZ,
SensorPCF85063::CLK_LOW,
};
String freq_hz_str[] = {
"32.768KHZ",
"16.384KHZ",
"8.192KHZ",
"4.096KHZ",
"2.048KHZ",
"1.024KHZ",
"1HZ",
"LOW"
};
SensorPCF85063 rtc;
uint32_t lastMillis;
uint8_t i = 0;
void setup()
{
Serial.begin(115200);
while (!Serial);
if (!rtc.begin(Wire, PCF85063_SLAVE_ADDRESS, SENSOR_SDA, SENSOR_SCL)) {
Serial.println("Failed to find PCF8563 - check your wiring!");
while (1) {
delay(1000);
}
}
}
void loop()
{
if (millis() - lastMillis > 5000) {
lastMillis = millis();
Serial.print("Set freq : ");
Serial.println(freq_hz_str[i]);
rtc.setClockOutput(clock_array[i]);
i++;
i %= 8;
}
}
@@ -0,0 +1,92 @@
/**
*
* @license MIT License
*
* Copyright (c) 2023 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file PCF85063_SimpleTime.ino
* @author Lewis He (lewishe@outlook.com)
* @date 2023-09-07
*
*/
#include <Wire.h>
#include <SPI.h>
#include <Arduino.h>
#include "SensorPCF85063.hpp"
#ifndef SENSOR_SDA
#define SENSOR_SDA 17
#endif
#ifndef SENSOR_SCL
#define SENSOR_SCL 18
#endif
#ifndef SENSOR_IRQ
#define SENSOR_IRQ 7
#endif
SensorPCF85063 rtc;
uint32_t lastMillis;
void setup()
{
Serial.begin(115200);
while (!Serial);
pinMode(SENSOR_IRQ, INPUT_PULLUP);
if (!rtc.begin(Wire, PCF85063_SLAVE_ADDRESS, SENSOR_SDA, SENSOR_SCL)) {
Serial.println("Failed to find PCF8563 - check your wiring!");
while (1) {
delay(1000);
}
}
uint16_t year = 2023;
uint8_t month = 9;
uint8_t day = 7;
uint8_t hour = 11;
uint8_t minute = 24;
uint8_t second = 30;
rtc.setDateTime(year, month, day, hour, minute, second);
}
void loop()
{
if (millis() - lastMillis > 1000) {
lastMillis = millis();
RTC_DateTime datetime = rtc.getDateTime();
Serial.printf(" Year :"); Serial.print(datetime.year);
Serial.printf(" Month:"); Serial.print(datetime.month);
Serial.printf(" Day :"); Serial.print(datetime.day);
Serial.printf(" Hour:"); Serial.print(datetime.hour);
Serial.printf(" Minute:"); Serial.print(datetime.minute);
Serial.printf(" Sec :"); Serial.println(datetime.second);
}
}
@@ -0,0 +1,193 @@
/**
*
* @license MIT License
*
* Copyright (c) 2022 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file PCF8563_AlarmByUnits.ino
* @author Lewis He (lewishe@outlook.com)
* @date 2022-12-11
*
*/
#include <Wire.h>
#include <SPI.h>
#include <Arduino.h>
#include <time.h>
#include "SensorPCF8563.hpp"
#ifdef ESP32
#define SENSOR_SDA 42
#define SENSOR_SCL 41
#define SENSOR_IRQ 14
#else
#define _PINNUM(port, pin) ((port)*32 + (pin))
#define SENSOR_SDA _PINNUM(0,26)
#define SENSOR_SCL _PINNUM(0,27)
#define SENSOR_IRQ _PINNUM(0,16)
#endif
SensorPCF8563 rtc;
uint32_t lastMillis = 0;
uint8_t nextHour = 22;
uint8_t nextMonth = 1;
uint8_t nextDay = 1;
uint8_t nextMinute = 59;
uint8_t nextSecond = 55;
void setup()
{
Serial.begin(115200);
while (!Serial);
if (!rtc.begin(Wire, PCF8563_SLAVE_ADDRESS, SENSOR_SDA, SENSOR_SCL)) {
Serial.println("Failed to find PCF8563 - check your wiring!");
while (1) {
delay(1000);
}
}
pinMode(SENSOR_IRQ, INPUT_PULLUP);
rtc.setDateTime(2022, nextMonth, nextDay, nextHour, nextMinute, nextSecond);
// From minute timer
rtc.setAlarmByMinutes(0);
rtc.enableAlarm();
}
void printDateTime()
{
if (millis() - lastMillis > 1000) {
/**
/// Format output time*
Option:
DATETIME_FORMAT_HM
DATETIME_FORMAT_HMS
DATETIME_FORMAT_YYYY_MM_DD
DATETIME_FORMAT_MM_DD_YYYY
DATETIME_FORMAT_DD_MM_YYYY
DATETIME_FORMAT_YYYY_MM_DD_H_M_S
default: DATETIME_FORMAT_YYYY_MM_DD_H_M_S_WEEK
*/
Serial.println(rtc.strftime());
lastMillis = millis();
}
}
// Test minute timing
void testAlarmMinute()
{
while (1) {
if (digitalRead(SENSOR_IRQ) == LOW) {
Serial.println("testAlarmMinute Interrupt .... ");
if (rtc.isAlarmActive()) {
Serial.println("Alarm active");
rtc.resetAlarm();
rtc.setDateTime(2022, nextMonth, nextDay, nextHour, nextMinute, nextSecond);
nextHour++;
if (nextHour >= 24) {
nextHour = 23;
nextDay = 25;
rtc.setAlarmByHours(0);
Serial.println("setAlarmByHours");
return;
}
}
}
printDateTime();
}
}
// Test hour timing
void testAlarmHour()
{
while (1) {
if (digitalRead(SENSOR_IRQ) == LOW) {
Serial.println("testAlarmHour Interrupt .... ");
if (rtc.isAlarmActive()) {
Serial.println("Alarm active");
rtc.resetAlarm();
rtc.setDateTime(2022, nextMonth, nextDay, nextHour, nextMinute, nextSecond);
nextDay++;
if (nextDay >= 30) {
nextMonth = 1;
nextHour = 23;
nextMinute = 59;
nextSecond = 55;
nextDay = rtc.getDaysInMonth(nextMonth, 2022);
rtc.setDateTime(2022, nextMonth, nextDay, nextHour, nextMinute, nextSecond);
rtc.setAlarmByDays(1);
Serial.println("setAlarmByDays");
return;
}
}
}
printDateTime();
}
}
// Test day timing
void testAlarmDay()
{
while (1) {
if (digitalRead(SENSOR_IRQ) == LOW) {
Serial.println("testAlarmDay Interrupt .... ");
if (rtc.isAlarmActive()) {
Serial.println("Alarm active");
rtc.resetAlarm();
nextDay = rtc.getDaysInMonth(nextMonth, 2022);
rtc.setDateTime(2022, nextMonth, nextDay, nextHour, nextMinute, nextSecond);
nextMonth++;
if (nextMonth >= 12) {
return;
}
}
}
printDateTime();
}
}
void loop()
{
testAlarmMinute();
testAlarmHour();
testAlarmDay();
Serial.println("Test done ...");
while (1) {
delay(100);
}
}
@@ -0,0 +1,87 @@
/**
*
* @license MIT License
*
* Copyright (c) 2022 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file PCF8563_SimpleTime.ino
* @author Lewis He (lewishe@outlook.com)
* @date 2022-12-12
*
*/
#include <Wire.h>
#include <SPI.h>
#include <Arduino.h>
#include "SensorPCF8563.hpp"
// lilygo t-beam-s3-core pin
#define SENSOR_SDA 42
#define SENSOR_SCL 41
#define SENSOR_IRQ 14
SensorPCF8563 rtc;
uint32_t lastMillis;
void setup()
{
Serial.begin(115200);
while (!Serial);
pinMode(SENSOR_IRQ, INPUT_PULLUP);
if (!rtc.begin(Wire, PCF8563_SLAVE_ADDRESS, SENSOR_SDA, SENSOR_SCL)) {
Serial.println("Failed to find PCF8563 - check your wiring!");
while (1) {
delay(1000);
}
}
uint16_t year = 2022;
uint8_t month = 12;
uint8_t day = 12;
uint8_t hour = 21;
uint8_t minute = 00;
uint8_t second = 30;
rtc.setDateTime(year, month, day, hour, minute, second);
}
void loop()
{
if (millis() - lastMillis > 1000) {
lastMillis = millis();
RTC_DateTime datetime = rtc.getDateTime();
Serial.printf(" Year :"); Serial.print(datetime.year);
Serial.printf(" Month:"); Serial.print(datetime.month);
Serial.printf(" Day :"); Serial.print(datetime.day);
Serial.printf(" Hour:"); Serial.print(datetime.hour);
Serial.printf(" Minute:"); Serial.print(datetime.minute);
Serial.printf(" Sec :"); Serial.println(datetime.second);
}
}
@@ -0,0 +1,98 @@
/**
*
* @license MIT License
*
* Copyright (c) 2022 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file PCF8563_TimeLib.ino
* @author Lewis He (lewishe@outlook.com)
* @date 2022-12-12
*
*/
#include <Wire.h>
#include <SPI.h>
#include <Arduino.h>
#include "SensorPCF8563.hpp"
#include <time.h>
// lilygo t-beam-s3-core pin
#define SENSOR_SDA 42
#define SENSOR_SCL 41
#define SENSOR_IRQ 14
SensorPCF8563 rtc;
uint32_t lastMillis;
char buf[64];
void setup()
{
Serial.begin(115200);
while (!Serial);
pinMode(SENSOR_IRQ, INPUT_PULLUP);
if (!rtc.begin(Wire, PCF8563_SLAVE_ADDRESS, SENSOR_SDA, SENSOR_SCL)) {
Serial.println("Failed to find PCF8563 - check your wiring!");
while (1) {
delay(1000);
}
}
}
void loop()
{
if (millis() - lastMillis > 1000) {
lastMillis = millis();
struct tm timeinfo;
// Get the time C library structure
rtc.getDateTime(&timeinfo);
// Format the output using the strftime function
// For more formats, please refer to :
// https://man7.org/linux/man-pages/man3/strftime.3.html
size_t written = strftime(buf, 64, "%A, %B %d %Y %H:%M:%S", &timeinfo);
if (written != 0) {
Serial.println(buf);
}
written = strftime(buf, 64, "%b %d %Y %H:%M:%S", &timeinfo);
if (written != 0) {
Serial.println(buf);
}
written = strftime(buf, 64, "%A, %d. %B %Y %I:%M%p", &timeinfo);
if (written != 0) {
Serial.println(buf);
}
}
}
@@ -0,0 +1,161 @@
/**
*
* @license MIT License
*
* Copyright (c) 2022 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file PCF8563_TimeSynchronization.ino
* @author Lewis He (lewishe@outlook.com)
* @date 2022-12-12
*
*/
#include <Wire.h>
#include <SPI.h>
#include <Arduino.h>
#if defined(ARDUINO_ARCH_ESP32)
#include <time.h>
#include <WiFi.h>
#include <sntp.h>
#include "SensorPCF8563.hpp"
// lilygo t-beam-s3-core pin
#define SENSOR_SDA 42
#define SENSOR_SCL 41
#define SENSOR_IRQ 14
const char *ssid = "YOUR_SSID";
const char *password = "YOUR_PASS";
const char *ntpServer1 = "pool.ntp.org";
const char *ntpServer2 = "time.nist.gov";
const long gmtOffset_sec = 3600;
const int daylightOffset_sec = 3600;
const char *time_zone = "CST-8"; // TimeZone rule for Europe/Rome including daylight adjustment rules (optional)
SensorPCF8563 rtc;
uint32_t lastMillis;
// Callback function (get's called when time adjusts via NTP)
void timeavailable(struct timeval *t)
{
Serial.println("Got time adjustment from NTP, Write the hardware clock");
// Write synchronization time to hardware
rtc.hwClockWrite();
}
void setup()
{
Serial.begin(115200);
while (!Serial);
pinMode(SENSOR_IRQ, INPUT_PULLUP);
if (!rtc.begin(Wire, PCF8563_SLAVE_ADDRESS, SENSOR_SDA, SENSOR_SCL)) {
Serial.println("Failed to find PCF8563 - check your wiring!");
while (1) {
delay(1000);
}
}
// set notification call-back function
sntp_set_time_sync_notification_cb( timeavailable );
/**
* NTP server address could be aquired via DHCP,
*
* NOTE: This call should be made BEFORE esp32 aquires IP address via DHCP,
* otherwise SNTP option 42 would be rejected by default.
* NOTE: configTime() function call if made AFTER DHCP-client run
* will OVERRIDE aquired NTP server address
*/
sntp_servermode_dhcp(1); // (optional)
/**
* This will set configured ntp servers and constant TimeZone/daylightOffset
* should be OK if your time zone does not need to adjust daylightOffset twice a year,
* in such a case time adjustment won't be handled automagicaly.
*/
// configTime(gmtOffset_sec, daylightOffset_sec, ntpServer1, ntpServer2);
/**
* A more convenient approach to handle TimeZones with daylightOffset
* would be to specify a environmnet variable with TimeZone definition including daylight adjustmnet rules.
* A list of rules for your zone could be obtained from https://github.com/esp8266/Arduino/blob/master/cores/esp8266/TZ.h
*/
configTzTime(time_zone, ntpServer1, ntpServer2);
//connect to WiFi
Serial.printf("Connecting to %s ", ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(" CONNECTED");
}
void loop()
{
if (millis() - lastMillis > 1000) {
lastMillis = millis();
// hardware clock
struct tm hwTimeinfo;
rtc.getDateTime(&hwTimeinfo);
Serial.print("Hardware clock :");
Serial.println(&hwTimeinfo, "%A, %B %d %Y %H:%M:%S");
// system clock
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
Serial.println("No time available (yet)");
return;
}
Serial.print("System clock :");
Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
Serial.println();
}
}
#else
void setup()
{
Serial.begin(115200);
}
void loop()
{
Serial.println("Examples only ESP32"); delay(1000);
}
#endif
@@ -0,0 +1,272 @@
/**
*
* @license MIT License
*
* Copyright (c) 2022 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file QMC6310_CalibrateExample.ino
* @author Lewis He (lewishe@outlook.com)
* @date 2022-10-16
*
*/
#include <Wire.h>
#include <SPI.h>
#include <Arduino.h>
#include "SensorQMC6310.hpp"
#ifndef SENSOR_SDA
#define SENSOR_SDA 17
#endif
#ifndef SENSOR_SCL
#define SENSOR_SCL 18
#endif
#ifndef SENSOR_IRQ
#define SENSOR_IRQ -1
#endif
SensorQMC6310 qmc;
void calibrate()
{
qmc.setDataOutputRate(SensorQMC6310::DATARATE_200HZ);
int32_t x_min = 65535;
int32_t x_max = -65535;
int32_t y_min = 65535;
int32_t y_max = -65535;
int32_t z_min = 65535;
int32_t z_max = -65535;
Serial.println("Place the sensor on the plane and slowly rotate the sensor...");
int32_t range = 1000;
int32_t i = 0;
int32_t x = 0, y = 0, z = 0;;
float a = 0.5 ;
float x_offset = 0;
float y_offset = 0;
float z_offset = 0;
while (i < range) {
i += 1;
if (qmc.isDataReady()) {
qmc.readData();
x = a * qmc.getRawX() + (1 - a) * x;
y = a * qmc.getRawY() + (1 - a) * y;
z = a * qmc.getRawZ() + (1 - a) * z;
if (x < x_min) {
x_min = x;
i = 0;
}
if (x > x_max) {
x_max = x;
i = 0;
}
if (y < y_min) {
y_min = y;
i = 0;
}
if (y > y_max) {
y_max = y;
i = 0;
}
if (z < z_min) {
z_min = z;
i = 0;
}
if (z > z_max) {
z_max = z;
i = 0;
}
int j = round(10 * i / range);
Serial.print("[");
for (int k = 0; k < j; ++k) {
Serial.print("*");
}
Serial.println("]");
}
delay(5);
}
x_offset = (x_max + x_min) / 2;
y_offset = (y_max + y_min) / 2;
z_offset = (z_max + z_min) / 2;
Serial.print("x_min:");
Serial.print(x_min);
Serial.print("x_max:");
Serial.print(x_max);
Serial.print("y_min:");
Serial.print(y_min);
Serial.print("y_max:");
Serial.print(y_max);
Serial.print("z_min:");
Serial.print(z_min);
Serial.print("z_max:");
Serial.println(z_max);
Serial.print("x_offset:");
Serial.print(x_offset);
Serial.print("y_offset:");
Serial.print(y_offset);
Serial.print("z_offset:");
Serial.print(z_offset);
// Serial.printf("x_min:%d x_max:%d y_min:%d y_max:%d z_min:%d z_max:%d\n", x_min, x_max, y_min, y_max, z_min, z_max);
// Serial.printf("x_offset:%.2f y_offset:%.2f z_offset:%.2f \n", x_offset, y_offset, z_offset);
// Set the calibration value and the user calculates the deviation
qmc.setOffset(x_offset, y_offset, z_offset);
}
void setup()
{
Serial.begin(115200);
while (!Serial);
if (!qmc.begin(Wire, QMC6310_SLAVE_ADDRESS, SENSOR_SDA, SENSOR_SCL)) {
Serial.println("Failed to find QMC6310 - check your wiring!");
while (1) {
delay(1000);
}
}
/* Get Magnetometer chip id*/
Serial.print("Device ID:");
Serial.println(qmc.getChipID(), HEX);
/* Config Magnetometer */
qmc.configMagnetometer(
/*
* Run Mode
* MODE_SUSPEND
* MODE_NORMAL
* MODE_SINGLE
* MODE_CONTINUOUS
* * */
SensorQMC6310::MODE_CONTINUOUS,
/*
* Full Range
* RANGE_30G
* RANGE_12G
* RANGE_8G
* RANGE_2G
* * */
SensorQMC6310::RANGE_8G,
/*
* Output data rate
* DATARATE_10HZ
* DATARATE_50HZ
* DATARATE_100HZ
* DATARATE_200HZ
* * */
SensorQMC6310::DATARATE_200HZ,
/*
* Over sample Ratio1
* OSR_8
* OSR_4
* OSR_2
* OSR_1
* * * */
SensorQMC6310::OSR_1,
/*
* Down sample Ratio1
* DSR_8
* DSR_4
* DSR_2
* DSR_1
* * */
SensorQMC6310::DSR_1);
// Calibration algorithm reference from
// https://github.com/CoreElectronics/CE-PiicoDev-QMC6310-MicroPython-Module
calibrate();
Serial.println("Calibration done .");
delay(5000);
Serial.println("Read data now...");
}
void loop()
{
//Wait data ready
if (qmc.isDataReady()) {
qmc.readData();
Serial.print("GYR: ");
Serial.print("X:");
Serial.print(qmc.getX());
Serial.print(" Y:");
Serial.print(qmc.getY());
Serial.print(" Z:");
Serial.print(qmc.getZ());
Serial.println(" uT");
Serial.print("RAW: ");
Serial.print("X:");
Serial.print(qmc.getRawX());
Serial.print(" Y:");
Serial.print(qmc.getRawY());
Serial.print(" Z:");
Serial.println(qmc.getRawZ());
/*
float x, y, z;
qmc.getMag(x, y, z);
Serial.print("X:");
Serial.print(x);
Serial.print(" Y:");
Serial.print(y);
Serial.print(" Z:");
Serial.println(x);
*/
}
delay(100);
}
@@ -0,0 +1,235 @@
/**
*
* @license MIT License
*
* Copyright (c) 2022 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file QMC6310_GetDataExample.ino
* @author Lewis He (lewishe@outlook.com)
* @date 2022-10-16
*
*/
#include <Wire.h>
#include <SPI.h>
#include <Arduino.h>
#if defined(ARDUINO_ARCH_ESP32)
#include "SensorQMC6310.hpp"
#include "SH1106Wire.h" //Oled display from https://github.com/ThingPulse/esp8266-oled-ssd1306
#ifndef SENSOR_SDA
#define SENSOR_SDA 17
#endif
#ifndef SENSOR_SCL
#define SENSOR_SCL 18
#endif
#ifndef SENSOR_IRQ
#define SENSOR_IRQ -1
#endif
#define I2C1_SDA 22 //Display Wire SDA Pin
#define I2C1_SCL 21 //Display Wire SCL Pin
SH1106Wire display(0x3c, I2C1_SDA, I2C1_SCL);
SensorQMC6310 qmc;
int last_dx, last_dy, dx, dy, angle;
const int centreX = 32;
const int centreY = 30;
const int radius = 22;
//Compass application from https://github.com/G6EJD/ESP8266_micro_compass_HMC5883_OLED
void arrow(int x2, int y2, int x1, int y1, int alength, int awidth, OLEDDISPLAY_COLOR color)
{
display.setColor(color);
float distance;
int dx, dy, x2o, y2o, x3, y3, x4, y4, k;
distance = sqrt(pow((x1 - x2), 2) + pow((y1 - y2), 2));
dx = x2 + (x1 - x2) * alength / distance;
dy = y2 + (y1 - y2) * alength / distance;
k = awidth / alength;
x2o = x2 - dx;
y2o = dy - y2;
x3 = y2o * k + dx;
y3 = x2o * k + dy;
x4 = dx - y2o * k;
y4 = dy - x2o * k;
display.drawLine(x1, y1, x2, y2);
display.drawLine(x1, y1, dx, dy);
display.drawLine(x3, y3, x4, y4);
display.drawLine(x3, y3, x2, y2);
display.drawLine(x2, y2, x4, y4);
}
void setup()
{
Serial.begin(115200);
while (!Serial);
if (!qmc.begin(Wire, QMC6310_SLAVE_ADDRESS, SENSOR_SDA, SENSOR_SCL)) {
Serial.println("Failed to find QMC6310 - check your wiring!");
while (1) {
delay(1000);
}
}
display.init();
last_dx = centreX;
last_dy = centreY;
/* Get Magnetometer chip id*/
Serial.print("Device ID:");
Serial.println(qmc.getChipID(), HEX);
/* Config Magnetometer */
int r = qmc.configMagnetometer(
/*
* Run Mode
* MODE_SUSPEND
* MODE_NORMAL
* MODE_SINGLE
* MODE_CONTINUOUS
* * */
SensorQMC6310::MODE_NORMAL,
/*
* Full Range
* RANGE_30G
* RANGE_12G
* RANGE_8G
* RANGE_2G
* * */
SensorQMC6310::RANGE_2G,
/*
* Output data rate
* DATARATE_10HZ
* DATARATE_50HZ
* DATARATE_100HZ
* DATARATE_200HZ
* * */
SensorQMC6310::DATARATE_100HZ,
/*
* Over sample Ratio1
* OSR_8
* OSR_4
* OSR_2
* OSR_1
* * * */
SensorQMC6310::OSR_1,
/*
* Down sample Ratio1
* DSR_8
* DSR_4
* DSR_2
* DSR_1
* * */
SensorQMC6310::DSR_1);
if (r != DEV_WIRE_NONE) {
Serial.println("Device config failed!");
while (1)delay(1000);
}
// Print register configuration information
qmc.dumpCtrlRegister();
Serial.println("Read data now...");
}
void loop()
{
//Wiat data ready
if (qmc.isDataReady()) {
qmc.readData();
display.drawString(29, 0, "N");
display.drawString( 0, 28, "W");
display.drawString(60, 28, "E");
display.drawString(29, 53, "S");
display.drawLine(1, 1, 7, 7);
display.drawLine(62, 1, 56, 7);
display.drawLine(1, 62, 7, 56);
display.drawLine(56, 56, 62, 62);
//Compass application from https://github.com/G6EJD/ESP8266_micro_compass_HMC5883_OLED
float heading = atan2(qmc.getY(), qmc.getX()); // Result is in radians
// Now add the 'Declination Angle' for you location. Declination is the variation in magnetic field at your location.
// Find your declination here: http://www.magnetic-declination.com/
// At my location it is : -2° 20' W, or -2.33 Degrees, which needs to be in radians so = -2.33 / 180 * PI = -0.041 West is + E is -
// Make declination = 0 if you can't find your Declination value, the error is negible for nearly all locations
float declination = -0.041;
heading = heading + declination;
if (heading < 0) heading += 2 * PI; // Correct for when signs are reversed.
if (heading > 2 * PI) heading -= 2 * PI; // Correct for when heading exceeds 360-degree, especially when declination is included
angle = int(heading * 180 / M_PI); // Convert radians to degrees for more a more usual result
// For the screen -X = up and +X = down and -Y = left and +Y = right, so does not follow coordinate conventions
dx = (0.7 * radius * cos((angle - 90) * 3.14 / 180)) + centreX; // calculate X position for the screen coordinates - can be confusing!
dy = (0.7 * radius * sin((angle - 90) * 3.14 / 180)) + centreY; // calculate Y position for the screen coordinates - can be confusing!
arrow(last_dx, last_dy, centreX, centreY, 2, 2, BLACK); // Erase last arrow
arrow(dx, dy, centreX, centreY, 2, 2, WHITE); // Draw arrow in new position
display.setColor(BLACK);
display.fillRect(80, 50, 25, 48);
display.setColor(WHITE);
display.drawString(80, 50, String(angle) + "°");
display.display();
last_dx = dx;
last_dy = dy;
// for debug.
Serial.print("GYR: ");
Serial.print("X:");
Serial.print(qmc.getX());
Serial.print(" Y:");
Serial.print(qmc.getY());
Serial.print(" Z:");
Serial.print(qmc.getZ());
Serial.println(" uT");
}
delay(100);
}
#else
void setup()
{
Serial.begin(115200);
}
void loop()
{
Serial.println("The graphics library may not be supported on the current platform"); delay(1000);
}
#endif
@@ -0,0 +1,155 @@
/**
*
* @license MIT License
*
* Copyright (c) 2022 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file QMC6310_GetDataExample.ino
* @author Lewis He (lewishe@outlook.com)
* @date 2022-10-16
*
*/
#include <Wire.h>
#include <SPI.h>
#include <Arduino.h>
#include "SensorQMC6310.hpp"
#ifndef SENSOR_SDA
#define SENSOR_SDA 17
#endif
#ifndef SENSOR_SCL
#define SENSOR_SCL 18
#endif
#ifndef SENSOR_IRQ
#define SENSOR_IRQ -1
#endif
SensorQMC6310 qmc;
void setup()
{
Serial.begin(115200);
while (!Serial);
if (!qmc.begin(Wire, QMC6310_SLAVE_ADDRESS, SENSOR_SDA, SENSOR_SCL)) {
Serial.println("Failed to find QMC6310 - check your wiring!");
while (1) {
delay(1000);
}
}
/* Get Magnetometer chip id*/
Serial.print("Device ID:");
Serial.println(qmc.getChipID(), HEX);
/* Config Magnetometer */
qmc.configMagnetometer(
/*
* Run Mode
* MODE_SUSPEND
* MODE_NORMAL
* MODE_SINGLE
* MODE_CONTINUOUS
* * */
SensorQMC6310::MODE_CONTINUOUS,
/*
* Full Range
* RANGE_30G
* RANGE_12G
* RANGE_8G
* RANGE_2G
* * */
SensorQMC6310::RANGE_8G,
/*
* Output data rate
* DATARATE_10HZ
* DATARATE_50HZ
* DATARATE_100HZ
* DATARATE_200HZ
* * */
SensorQMC6310::DATARATE_200HZ,
/*
* Over sample Ratio1
* OSR_8
* OSR_4
* OSR_2
* OSR_1
* * * */
SensorQMC6310::OSR_1,
/*
* Down sample Ratio1
* DSR_8
* DSR_4
* DSR_2
* DSR_1
* * */
SensorQMC6310::DSR_1);
Serial.println("Read data now...");
}
void loop()
{
//Wiat data ready
if (qmc.isDataReady()) {
qmc.readData();
Serial.print("GYR: ");
Serial.print("X:");
Serial.print(qmc.getX());
Serial.print(" Y:");
Serial.print(qmc.getY());
Serial.print(" Z:");
Serial.print(qmc.getZ());
Serial.println(" uT");
Serial.print("RAW: ");
Serial.print("X:");
Serial.print(qmc.getRawX());
Serial.print(" Y:");
Serial.print(qmc.getRawY());
Serial.print(" Z:");
Serial.println(qmc.getRawZ());
/*
float x, y, z;
qmc.getMag(x, y, z);
Serial.print("X:");
Serial.print(x);
Serial.print(" Y:");
Serial.print(y);
Serial.print(" Z:");
Serial.println(x);
*/
}
delay(100);
}
@@ -0,0 +1,133 @@
/**
*
* @license MIT License
*
* Copyright (c) 2022 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file QMC6310_GetPolarExample.ino
* @author Lewis He (lewishe@outlook.com)
* @date 2022-10-16
*
*/
#include <Wire.h>
#include <SPI.h>
#include <Arduino.h>
#include "SensorQMC6310.hpp"
#ifndef SENSOR_SDA
#define SENSOR_SDA 17
#endif
#ifndef SENSOR_SCL
#define SENSOR_SCL 18
#endif
#ifndef SENSOR_IRQ
#define SENSOR_IRQ -1
#endif
SensorQMC6310 qmc;
void setup()
{
Serial.begin(115200);
while (!Serial);
if (!qmc.begin(Wire, QMC6310_SLAVE_ADDRESS, SENSOR_SDA, SENSOR_SCL)) {
Serial.println("Failed to find QMC6310 - check your wiring!");
while (1) {
delay(1000);
}
}
/* Get Magnetometer chip id*/
Serial.print("Device ID:");
Serial.println(qmc.getChipID(), HEX);
/* Config Magnetometer */
qmc.configMagnetometer(
/*
* Run Mode
* MODE_SUSPEND
* MODE_NORMAL
* MODE_SINGLE
* MODE_CONTINUOUS
* * */
SensorQMC6310::MODE_NORMAL,
/*
* Full Range
* RANGE_30G
* RANGE_12G
* RANGE_8G
* RANGE_2G
* * */
SensorQMC6310::RANGE_8G,
/*
* Output data rate
* DATARATE_10HZ
* DATARATE_50HZ
* DATARATE_100HZ
* DATARATE_200HZ
* * */
SensorQMC6310::DATARATE_200HZ,
/*
* Over sample Ratio1
* OSR_8
* OSR_4
* OSR_2
* OSR_1
* * * */
SensorQMC6310::OSR_8,
/*
* Down sample Ratio1
* DSR_8
* DSR_4
* DSR_2
* DSR_1
* * */
SensorQMC6310::DSR_1);
qmc.dumpCtrlRegister();
// Declination is the difference between magnetic-north and true-north ("heading") and depends on location
qmc.setDeclination(-2.77); // Found with: https://www.magnetic-declination.com/CHINA/SHENZHEN/475119.html
Serial.println("Read data now...");
}
void loop()
{
Polar data;
// Wait for data ready
if (qmc.readPolar(data)) {
Serial.print(" polar:"); Serial.print(data.polar); Serial.print("°");
Serial.print(" Gauss:"); Serial.print(data.Gauss);
Serial.print(" uT:"); Serial.println(data.uT);
}
delay(100);
}
@@ -0,0 +1,263 @@
/**
*
* @license MIT License
*
* Copyright (c) 2022 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file QMI8658_BlockExample.ino
* @author Lewis He (lewishe@outlook.com)
* @date 2022-10-16
*
*/
#include <Arduino.h>
#include <Wire.h>
#include <SPI.h>
#ifdef ARDUINO_ARCH_ESP32
#include "SensorQMI8658.hpp"
#include <MadgwickAHRS.h> //MadgwickAHRS from https://github.com/arduino-libraries/MadgwickAHRS
#include "SH1106Wire.h" //Oled display from https://github.com/ThingPulse/esp8266-oled-ssd1306
// #define USE_WIRE
#ifndef SENSOR_SDA
#define SENSOR_SDA 17
#endif
#ifndef SENSOR_SCL
#define SENSOR_SCL 18
#endif
#ifndef SENSOR_IRQ
#define SENSOR_IRQ -1
#endif
#define I2C1_SDA 22 //Display Wire SDA Pin
#define I2C1_SCL 21 //Display Wire SCL Pin
SH1106Wire display(0x3c, I2C1_SDA, I2C1_SCL);
SensorQMI8658 qmi;
IMUdata acc;
IMUdata gyr;
Madgwick filter;
unsigned long microsPerReading, microsPrevious;
float posX = 64;
float posY = 32;
float lastPosX = posX;
float lastPosY = posY;
void setup()
{
Serial.begin(115200);
while (!Serial);
display.init();
#ifdef USE_WIRE
//Using WIRE !!
if (!qmi.begin(Wire, QMI8658_L_SLAVE_ADDRESS, SENSOR_SDA, SENSOR_SCL)) {
Serial.println("Failed to find QMI8658 - check your wiring!");
while (1) {
delay(1000);
}
}
#else
#ifndef CONFIG_IDF_TARGET_ESP32
//Use tbeams3 defalut spi pin
#define SPI_MOSI (35)
#define SPI_SCK (36)
#define SPI_MISO (37)
#define SPI_CS (47)
#define IMU_CS (34)
#define IMU_INT1 (33) //INTERRUPT PIN1 & PIN2 ,Use or logic to form a pin
pinMode(SPI_CS, OUTPUT); //sdcard pin set high
digitalWrite(SPI_CS, HIGH);
if (!qmi.begin(IMU_CS, SPI_MOSI, SPI_MISO, SPI_SCK)) {
#else
//Use esp32dev module defalut spi pin
#define IMU_CS (5)
#define IMU_INT1 (15)
#define IMU_INT2 (22)
if (!qmi.begin(IMU_CS)) {
#endif
Serial.println("Failed to find QMI8658 - check your wiring!");
while (1) {
delay(1000);
}
}
#endif
/* Get chip id*/
Serial.print("Device ID:");
Serial.println(qmi.getChipID(), HEX);
display.flipScreenVertically();
qmi.configAccelerometer(
/*
* ACC_RANGE_2G
* ACC_RANGE_4G
* ACC_RANGE_8G
* ACC_RANGE_16G
* */
SensorQMI8658::ACC_RANGE_2G,
/*
* ACC_ODR_1000H
* ACC_ODR_500Hz
* ACC_ODR_250Hz
* ACC_ODR_125Hz
* ACC_ODR_62_5Hz
* ACC_ODR_31_25Hz
* ACC_ODR_LOWPOWER_128Hz
* ACC_ODR_LOWPOWER_21Hz
* ACC_ODR_LOWPOWER_11Hz
* ACC_ODR_LOWPOWER_3H
* */
SensorQMI8658::ACC_ODR_1000Hz,
/*
* LPF_MODE_0 //2.66% of ODR
* LPF_MODE_1 //3.63% of ODR
* LPF_MODE_2 //5.39% of ODR
* LPF_MODE_3 //13.37% of ODR
* */
SensorQMI8658::LPF_MODE_0,
// selfTest enable
true);
qmi.configGyroscope(
/*
* GYR_RANGE_16DPS
* GYR_RANGE_32DPS
* GYR_RANGE_64DPS
* GYR_RANGE_128DPS
* GYR_RANGE_256DPS
* GYR_RANGE_512DPS
* GYR_RANGE_1024DPS
* */
SensorQMI8658::GYR_RANGE_256DPS,
/*
* GYR_ODR_7174_4Hz
* GYR_ODR_3587_2Hz
* GYR_ODR_1793_6Hz
* GYR_ODR_896_8Hz
* GYR_ODR_448_4Hz
* GYR_ODR_224_2Hz
* GYR_ODR_112_1Hz
* GYR_ODR_56_05Hz
* GYR_ODR_28_025H
* */
SensorQMI8658::GYR_ODR_896_8Hz,
/*
* LPF_MODE_0 //2.66% of ODR
* LPF_MODE_1 //3.63% of ODR
* LPF_MODE_2 //5.39% of ODR
* LPF_MODE_3 //13.37% of ODR
* */
SensorQMI8658::LPF_MODE_3,
// selfTest enable
true);
// In 6DOF mode (accelerometer and gyroscope are both enabled),
// the output data rate is derived from the nature frequency of gyroscope
qmi.enableGyroscope();
qmi.enableAccelerometer();
// Print register configuration information
qmi.dumpCtrlRegister();
// start filter
filter.begin(25);
// initialize variables to pace updates to correct rate
microsPerReading = 1000000 / 25;
microsPrevious = micros();
Serial.println("Read data now...");
}
const uint8_t rectWidth = 10;
void loop()
{
float roll, pitch, heading;
// check if it's time to read data and update the filter
if (micros() - microsPrevious >= microsPerReading) {
// read raw data from IMU
if (qmi.getDataReady()) {
qmi.getAccelerometer(acc.x, acc.y, acc.z);
qmi.getGyroscope(gyr.x, gyr.y, gyr.z);
// update the filter, which computes orientation
filter.updateIMU(gyr.x, gyr.y, gyr.z, acc.x, acc.y, acc.z);
// print the heading, pitch and roll
roll = filter.getRoll();
pitch = filter.getPitch();
heading = filter.getYaw();
posX -= roll * 2;
posY += pitch;
Serial.printf("x:%.2f y:%.2f \n", posX, posY);
posX = constrain(posX, 0, display.width() - rectWidth);
posY = constrain(posY, 0, display.height() - rectWidth);
display.setColor(BLACK);
display.fillRect(lastPosX, lastPosY, 10, 10);
display.setColor(WHITE);
display.fillRect(posX, posY, 10, 10);
display.display();
lastPosX = posX;
lastPosY = posY;
}
// increment previous time, so we keep proper pace
microsPrevious = microsPrevious + microsPerReading;
}
}
#else
void setup()
{
Serial.begin(115200);
}
void loop()
{
Serial.println("The graphics library may not be supported on the current platform"); delay(1000);
}
#endif
@@ -0,0 +1,199 @@
/**
*
* @license MIT License
*
* Copyright (c) 2022 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file QMI8658_GetDataExample.ino
* @author Lewis He (lewishe@outlook.com)
* @date 2022-10-16
*
*/
#include <Arduino.h>
#include <Wire.h>
#include <SPI.h>
#include "SensorQMI8658.hpp"
#define USE_WIRE
#ifndef SENSOR_SDA
#define SENSOR_SDA 17
#endif
#ifndef SENSOR_SCL
#define SENSOR_SCL 18
#endif
#ifndef SENSOR_IRQ
#define SENSOR_IRQ -1
#endif
#define IMU_CS 5
SensorQMI8658 qmi;
IMUdata acc;
IMUdata gyr;
void setup()
{
Serial.begin(115200);
while (!Serial);
#ifdef USE_WIRE
//Using WIRE !!
if (!qmi.begin(Wire, QMI8658_L_SLAVE_ADDRESS, SENSOR_SDA, SENSOR_SCL)) {
Serial.println("Failed to find QMI8658 - check your wiring!");
while (1) {
delay(1000);
}
}
#else
if (!qmi.begin(IMU_CS)) {
Serial.println("Failed to find QMI8658 - check your wiring!");
while (1) {
delay(1000);
}
}
#endif
/* Get chip id*/
Serial.print("Device ID:");
Serial.println(qmi.getChipID(), HEX);
qmi.configAccelerometer(
/*
* ACC_RANGE_2G
* ACC_RANGE_4G
* ACC_RANGE_8G
* ACC_RANGE_16G
* */
SensorQMI8658::ACC_RANGE_4G,
/*
* ACC_ODR_1000H
* ACC_ODR_500Hz
* ACC_ODR_250Hz
* ACC_ODR_125Hz
* ACC_ODR_62_5Hz
* ACC_ODR_31_25Hz
* ACC_ODR_LOWPOWER_128Hz
* ACC_ODR_LOWPOWER_21Hz
* ACC_ODR_LOWPOWER_11Hz
* ACC_ODR_LOWPOWER_3H
* */
SensorQMI8658::ACC_ODR_1000Hz,
/*
* LPF_MODE_0 //2.66% of ODR
* LPF_MODE_1 //3.63% of ODR
* LPF_MODE_2 //5.39% of ODR
* LPF_MODE_3 //13.37% of ODR
* */
SensorQMI8658::LPF_MODE_0,
// selfTest enable
true);
qmi.configGyroscope(
/*
* GYR_RANGE_16DPS
* GYR_RANGE_32DPS
* GYR_RANGE_64DPS
* GYR_RANGE_128DPS
* GYR_RANGE_256DPS
* GYR_RANGE_512DPS
* GYR_RANGE_1024DPS
* */
SensorQMI8658::GYR_RANGE_64DPS,
/*
* GYR_ODR_7174_4Hz
* GYR_ODR_3587_2Hz
* GYR_ODR_1793_6Hz
* GYR_ODR_896_8Hz
* GYR_ODR_448_4Hz
* GYR_ODR_224_2Hz
* GYR_ODR_112_1Hz
* GYR_ODR_56_05Hz
* GYR_ODR_28_025H
* */
SensorQMI8658::GYR_ODR_896_8Hz,
/*
* LPF_MODE_0 //2.66% of ODR
* LPF_MODE_1 //3.63% of ODR
* LPF_MODE_2 //5.39% of ODR
* LPF_MODE_3 //13.37% of ODR
* */
SensorQMI8658::LPF_MODE_3,
// selfTest enable
true);
// In 6DOF mode (accelerometer and gyroscope are both enabled),
// the output data rate is derived from the nature frequency of gyroscope
qmi.enableGyroscope();
qmi.enableAccelerometer();
// Print register configuration information
qmi.dumpCtrlRegister();
Serial.println("Read data now...");
}
void loop()
{
if (qmi.getDataReady()) {
if (qmi.getAccelerometer(acc.x, acc.y, acc.z)) {
Serial.print("{ACCEL: ");
Serial.print(acc.x);
Serial.print(",");
Serial.print(acc.y);
Serial.print(",");
Serial.print(acc.z);
Serial.println("}");
}
if (qmi.getGyroscope(gyr.x, gyr.y, gyr.z)) {
Serial.print("{GYRO: ");
Serial.print(gyr.x);
Serial.print(",");
Serial.print(gyr.y );
Serial.print(",");
Serial.print(gyr.z);
Serial.println("}");
}
Serial.print("\t\t\t\t > ");
Serial.print(qmi.getTimestamp());
Serial.print(" ");
Serial.print(qmi.getTemperature_C());
Serial.println("*C");
}
delay(100);
}
@@ -0,0 +1,271 @@
/**
*
* @license MIT License
*
* Copyright (c) 2022 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file QMI8658_InterruptBlockExample.ino
* @author Lewis He (lewishe@outlook.com)
* @date 2022-10-16
*
*/
#include <Arduino.h>
#include <Wire.h>
#include <SPI.h>
#ifdef ARDUINO_ARCH_ESP32
#include "SensorQMI8658.hpp"
#include <MadgwickAHRS.h> //MadgwickAHRS from https://github.com/arduino-libraries/MadgwickAHRS
#include "SH1106Wire.h" //Oled display from https://github.com/ThingPulse/esp8266-oled-ssd1306
// #define USE_WIRE
#ifndef SENSOR_SDA
#define SENSOR_SDA 17
#endif
#ifndef SENSOR_SCL
#define SENSOR_SCL 18
#endif
#ifndef SENSOR_IRQ
#define SENSOR_IRQ -1
#endif
#define I2C1_SDA 22 //Display Wire SDA Pin
#define I2C1_SCL 21 //Display Wire SCL Pin
SH1106Wire display(0x3c, I2C1_SDA, I2C1_SCL);
SensorQMI8658 qmi;
IMUdata acc;
IMUdata gyr;
Madgwick filter;
unsigned long microsPerReading, microsPrevious;
float posX = 64;
float posY = 32;
float lastPosX = posX;
float lastPosY = posY;
void setup()
{
Serial.begin(115200);
while (!Serial);
display.init();
display.flipScreenVertically();
#ifdef USE_WIRE
//Using WIRE !!
if (!qmi.begin(Wire, QMI8658_L_SLAVE_ADDRESS, SENSOR_SDA, SENSOR_SCL)) {
Serial.println("Failed to find QMI8658 - check your wiring!");
while (1) {
delay(1000);
}
}
#else
#ifndef CONFIG_IDF_TARGET_ESP32
//Use tbeams3 defalut spi pin
#define SPI_MOSI (35)
#define SPI_SCK (36)
#define SPI_MISO (37)
#define SPI_CS (47)
#define IMU_CS (34)
#define IMU_INT1 (33) //INTERRUPT PIN1 & PIN2 ,Use or logic to form a pin
pinMode(SPI_CS, OUTPUT); //sdcard pin set high
digitalWrite(SPI_CS, HIGH);
if (!qmi.begin(IMU_CS, SPI_MOSI, SPI_MISO, SPI_SCK)) {
#else
//Use esp32dev module defalut spi pin
#define IMU_CS (5)
#define IMU_INT1 (15)
#define IMU_INT2 (22)
if (!qmi.begin(IMU_CS)) {
#endif
Serial.println("Failed to find QMI8658 - check your wiring!");
while (1) {
delay(1000);
}
}
#endif
/* Get chip id*/
Serial.print("Device ID:");
Serial.println(qmi.getChipID(), HEX);
qmi.configAccelerometer(
/*
* ACC_RANGE_2G
* ACC_RANGE_4G
* ACC_RANGE_8G
* ACC_RANGE_16G
* */
SensorQMI8658::ACC_RANGE_2G,
/*
* ACC_ODR_1000H
* ACC_ODR_500Hz
* ACC_ODR_250Hz
* ACC_ODR_125Hz
* ACC_ODR_62_5Hz
* ACC_ODR_31_25Hz
* ACC_ODR_LOWPOWER_128Hz
* ACC_ODR_LOWPOWER_21Hz
* ACC_ODR_LOWPOWER_11Hz
* ACC_ODR_LOWPOWER_3H
* */
SensorQMI8658::ACC_ODR_1000Hz,
/*
* LPF_MODE_0 //2.66% of ODR
* LPF_MODE_1 //3.63% of ODR
* LPF_MODE_2 //5.39% of ODR
* LPF_MODE_3 //13.37% of ODR
* */
SensorQMI8658::LPF_MODE_0,
// selfTest enable
true);
qmi.configGyroscope(
/*
* GYR_RANGE_16DPS
* GYR_RANGE_32DPS
* GYR_RANGE_64DPS
* GYR_RANGE_128DPS
* GYR_RANGE_256DPS
* GYR_RANGE_512DPS
* GYR_RANGE_1024DPS
* */
SensorQMI8658::GYR_RANGE_256DPS,
/*
* GYR_ODR_7174_4Hz
* GYR_ODR_3587_2Hz
* GYR_ODR_1793_6Hz
* GYR_ODR_896_8Hz
* GYR_ODR_448_4Hz
* GYR_ODR_224_2Hz
* GYR_ODR_112_1Hz
* GYR_ODR_56_05Hz
* GYR_ODR_28_025H
* */
SensorQMI8658::GYR_ODR_896_8Hz,
/*
* LPF_MODE_0 //2.66% of ODR
* LPF_MODE_1 //3.63% of ODR
* LPF_MODE_2 //5.39% of ODR
* LPF_MODE_3 //13.37% of ODR
* */
SensorQMI8658::LPF_MODE_3,
// selfTest enable
true);
// In 6DOF mode (accelerometer and gyroscope are both enabled),
// the output data rate is derived from the nature frequency of gyroscope
qmi.enableGyroscope();
qmi.enableAccelerometer();
pinMode(IMU_INT1, INPUT);
#ifdef IMU_INT2
pinMode(IMU_INT2, INPUT);
#endif
// qmi.enableINT(SensorQMI8658::IntPin1); //no use
// Enable data ready to interrupt pin2
qmi.enableINT(SensorQMI8658::IntPin2);
qmi.enableDataReadyINT();
// Print register configuration information
qmi.dumpCtrlRegister();
// start filter
filter.begin(25);
// initialize variables to pace updates to correct rate
microsPerReading = 1000000 / 25;
microsPrevious = micros();
Serial.println("Read data now...");
}
const uint8_t rectWidth = 10;
void loop()
{
float roll, pitch, heading;
// check if it's time to read data and update the filter
if (micros() - microsPrevious >= microsPerReading) {
// read raw data from IMU
if (digitalRead(IMU_INT1) == HIGH) {
qmi.getAccelerometer(acc.x, acc.y, acc.z);
qmi.getGyroscope(gyr.x, gyr.y, gyr.z);
// update the filter, which computes orientation
filter.updateIMU(gyr.x, gyr.y, gyr.z, acc.x, acc.y, acc.z);
// print the heading, pitch and roll
roll = filter.getRoll();
pitch = filter.getPitch();
heading = filter.getYaw();
posX -= roll * 2;
posY += pitch;
Serial.printf("x:%.2f y:%.2f \n", posX, posY);
posX = constrain(posX, 0, display.width() - rectWidth);
posY = constrain(posY, 0, display.height() - rectWidth);
display.setColor(BLACK);
display.fillRect(lastPosX, lastPosY, 10, 10);
display.setColor(WHITE);
display.fillRect(posX, posY, 10, 10);
display.display();
lastPosX = posX;
lastPosY = posY;
}
// increment previous time, so we keep proper pace
microsPrevious = microsPrevious + microsPerReading;
}
}
#else
void setup()
{
Serial.begin(115200);
}
void loop()
{
Serial.println("The graphics library may not support your current platform"); delay(1000);
}
#endif
@@ -0,0 +1,251 @@
/**
*
* @license MIT License
*
* Copyright (c) 2022 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file QMI8658_InterruptExample.ino
* @author Lewis He (lewishe@outlook.com)
* @date 2022-11-03
*
*/
#include <Arduino.h>
#include <Wire.h>
#include <SPI.h>
#include "SensorQMI8658.hpp"
// #define USE_WIRE
#ifndef SENSOR_SDA
#define SENSOR_SDA 17
#endif
#ifndef SENSOR_SCL
#define SENSOR_SCL 18
#endif
#ifndef SENSOR_IRQ
#define SENSOR_IRQ -1
#endif
SensorQMI8658 qmi;
IMUdata acc;
IMUdata gyr;
void setup()
{
Serial.begin(115200);
while (!Serial);
#ifdef USE_WIRE
//Using WIRE !!
if (!qmi.begin(Wire, QMI8658_L_SLAVE_ADDRESS, SENSOR_SDA, SENSOR_SCL)) {
Serial.println("Failed to find QMI8658 - check your wiring!");
while (1) {
delay(1000);
}
}
#else
#ifndef CONFIG_IDF_TARGET_ESP32
//Use tbeams3 defalut spi pin
#define SPI_MOSI (35)
#define SPI_SCK (36)
#define SPI_MISO (37)
#define SPI_CS (47)
#define IMU_CS (34)
#define IMU_INT1 (33)
pinMode(SPI_CS, OUTPUT); //sdcard pin set high
digitalWrite(SPI_CS, HIGH);
if (!qmi.begin(IMU_CS, SPI_MOSI, SPI_MISO, SPI_SCK)) {
#else
//Use esp32dev module defalut spi pin
#define IMU_CS (5)
#define IMU_INT1 (15)
#define IMU_INT2 (22)
if (!qmi.begin(IMU_CS)) {
#endif
Serial.println("Failed to find QMI8658 - check your wiring!");
while (1) {
delay(1000);
}
}
#endif
/* Get chip id*/
Serial.print("Device ID:");
Serial.println(qmi.getChipID(), HEX);
qmi.configAccelerometer(
/*
* ACC_RANGE_2G
* ACC_RANGE_4G
* ACC_RANGE_8G
* ACC_RANGE_16G
* */
SensorQMI8658::ACC_RANGE_4G,
/*
* ACC_ODR_1000H
* ACC_ODR_500Hz
* ACC_ODR_250Hz
* ACC_ODR_125Hz
* ACC_ODR_62_5Hz
* ACC_ODR_31_25Hz
* ACC_ODR_LOWPOWER_128Hz
* ACC_ODR_LOWPOWER_21Hz
* ACC_ODR_LOWPOWER_11Hz
* ACC_ODR_LOWPOWER_3H
* */
SensorQMI8658::ACC_ODR_1000Hz,
/*
* LPF_MODE_0 //2.66% of ODR
* LPF_MODE_1 //3.63% of ODR
* LPF_MODE_2 //5.39% of ODR
* LPF_MODE_3 //13.37% of ODR
* */
SensorQMI8658::LPF_MODE_0,
// selfTest enable
true);
qmi.configGyroscope(
/*
* GYR_RANGE_16DPS
* GYR_RANGE_32DPS
* GYR_RANGE_64DPS
* GYR_RANGE_128DPS
* GYR_RANGE_256DPS
* GYR_RANGE_512DPS
* GYR_RANGE_1024DPS
* */
SensorQMI8658::GYR_RANGE_64DPS,
/*
* GYR_ODR_7174_4Hz
* GYR_ODR_3587_2Hz
* GYR_ODR_1793_6Hz
* GYR_ODR_896_8Hz
* GYR_ODR_448_4Hz
* GYR_ODR_224_2Hz
* GYR_ODR_112_1Hz
* GYR_ODR_56_05Hz
* GYR_ODR_28_025H
* */
SensorQMI8658::GYR_ODR_896_8Hz,
/*
* LPF_MODE_0 //2.66% of ODR
* LPF_MODE_1 //3.63% of ODR
* LPF_MODE_2 //5.39% of ODR
* LPF_MODE_3 //13.37% of ODR
* */
SensorQMI8658::LPF_MODE_3,
// selfTest enable
true);
// In 6DOF mode (accelerometer and gyroscope are both enabled),
// the output data rate is derived from the nature frequency of gyroscope
qmi.enableGyroscope();
qmi.enableAccelerometer();
pinMode(IMU_INT1, INPUT);
#ifdef IMU_INT2
pinMode(IMU_INT2, INPUT);
#endif
// qmi.enableINT(SensorQMI8658::IntPin1); //no use
// Enable data ready to interrupt pin2
qmi.enableINT(SensorQMI8658::IntPin2);
qmi.enableDataReadyINT();
// Print register configuration information
qmi.dumpCtrlRegister();
Serial.println("Read data now...");
}
void readSensorData(const char *name)
{
uint8_t status = qmi.getIrqStatus();
// status == 0x01
// If syncSmpl (CTRL7.bit7) = 1:
// 0: Sensor Data is not available
// 1: Sensor Data is available for reading
// If syncSmpl = 0, this bit shows the same value of INT2 level
Serial.print(name);
Serial.print(" -> [");
Serial.print(millis());
Serial.print("]: -<HEX> ");
Serial.print(status);
Serial.print(" -<BIN> ");
Serial.println(status, BIN);
if (status & 0x01) {
if (qmi.getAccelerometer(acc.x, acc.y, acc.z)) {
Serial.print("{ACCEL: ");
Serial.print(acc.x);
Serial.print(",");
Serial.print(acc.y);
Serial.print(",");
Serial.print(acc.z);
Serial.println("}");
}
if (qmi.getGyroscope(gyr.x, gyr.y, gyr.z)) {
Serial.print("{GYRO: ");
Serial.print(gyr.x);
Serial.print(",");
Serial.print(gyr.y );
Serial.print(",");
Serial.print(gyr.z);
Serial.println("}");
}
Serial.print("\t\t\t\t > ");
Serial.print(qmi.getTimestamp());
Serial.print(" ");
Serial.print(qmi.getTemperature_C());
Serial.println("*C");
}
}
void loop()
{
if (digitalRead(IMU_INT1) == HIGH) {
readSensorData("INT1");
}
#ifdef IMU_INT2
if (digitalRead(IMU_INT2) == HIGH) {
readSensorData("INT2");
}
#endif
}
@@ -0,0 +1,250 @@
/**
*
* @license MIT License
*
* Copyright (c) 2022 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file QMI8658_LockingMechanismExample.ino
* @author Lewis He (lewishe@outlook.com)
* @date 2022-11-07
*
*/
#include <Arduino.h>
#include <Wire.h>
#include <SPI.h>
#include "SensorQMI8658.hpp"
// #define USE_WIRE
#ifndef SENSOR_SDA
#define SENSOR_SDA 17
#endif
#ifndef SENSOR_SCL
#define SENSOR_SCL 18
#endif
#ifndef SENSOR_IRQ
#define SENSOR_IRQ -1
#endif
SensorQMI8658 qmi;
IMUdata acc;
IMUdata gyr;
bool interruptFlag = false;
void setFlag(void)
{
interruptFlag = true;
}
// Callback when data is locked
void lockingMechanismHandler()
{
if (qmi.getAccelerometer(acc.x, acc.y, acc.z)) {
Serial.print("{ACCEL: ");
Serial.print(acc.x);
Serial.print(",");
Serial.print(acc.y);
Serial.print(",");
Serial.print(acc.z);
Serial.println("}");
}
if (qmi.getGyroscope(gyr.x, gyr.y, gyr.z)) {
Serial.print("{GYRO: ");
Serial.print(gyr.x);
Serial.print(",");
Serial.print(gyr.y );
Serial.print(",");
Serial.print(gyr.z);
Serial.println("}");
}
Serial.print("\t\t\t\t > ");
Serial.print(qmi.getTimestamp());
Serial.print(" ");
Serial.print(qmi.getTemperature_C());
Serial.println("*C");
}
void setup()
{
Serial.begin(115200);
while (!Serial);
#ifdef USE_WIRE
//Using WIRE !!
if (!qmi.begin(Wire, QMI8658_L_SLAVE_ADDRESS, SENSOR_SDA, SENSOR_SCL)) {
Serial.println("Failed to find QMI8658 - check your wiring!");
while (1) {
delay(1000);
}
}
#else
#ifndef CONFIG_IDF_TARGET_ESP32
//Use tbeams3 default spi pin
#define SPI_MOSI (35)
#define SPI_SCK (36)
#define SPI_MISO (37)
#define SPI_CS (47)
#define IMU_CS (34)
#define IMU_INT1 (33)
pinMode(SPI_CS, OUTPUT); //sdcard pin set high
digitalWrite(SPI_CS, HIGH);
if (!qmi.begin(IMU_CS, SPI_MOSI, SPI_MISO, SPI_SCK)) {
#else
//Use esp32dev module defalut spi pin
#define IMU_CS (5)
#define IMU_INT1 (15)
#define IMU_INT2 (22)
if (!qmi.begin(IMU_CS)) {
#endif
Serial.println("Failed to find QMI8658 - check your wiring!");
while (1) {
delay(1000);
}
}
#endif
/* Get chip id*/
Serial.print("Device ID:");
Serial.println(qmi.getChipID(), HEX);
qmi.configAccelerometer(
/*
* ACC_RANGE_2G
* ACC_RANGE_4G
* ACC_RANGE_8G
* ACC_RANGE_16G
* */
SensorQMI8658::ACC_RANGE_4G,
/*
* ACC_ODR_1000H
* ACC_ODR_500Hz
* ACC_ODR_250Hz
* ACC_ODR_125Hz
* ACC_ODR_62_5Hz
* ACC_ODR_31_25Hz
* ACC_ODR_LOWPOWER_128Hz
* ACC_ODR_LOWPOWER_21Hz
* ACC_ODR_LOWPOWER_11Hz
* ACC_ODR_LOWPOWER_3H
* */
SensorQMI8658::ACC_ODR_1000Hz,
/*
* LPF_MODE_0 //2.66% of ODR
* LPF_MODE_1 //3.63% of ODR
* LPF_MODE_2 //5.39% of ODR
* LPF_MODE_3 //13.37% of ODR
* */
SensorQMI8658::LPF_MODE_0,
// selfTest enable
true);
qmi.configGyroscope(
/*
* GYR_RANGE_16DPS
* GYR_RANGE_32DPS
* GYR_RANGE_64DPS
* GYR_RANGE_128DPS
* GYR_RANGE_256DPS
* GYR_RANGE_512DPS
* GYR_RANGE_1024DPS
* */
SensorQMI8658::GYR_RANGE_64DPS,
/*
* GYR_ODR_7174_4Hz
* GYR_ODR_3587_2Hz
* GYR_ODR_1793_6Hz
* GYR_ODR_896_8Hz
* GYR_ODR_448_4Hz
* GYR_ODR_224_2Hz
* GYR_ODR_112_1Hz
* GYR_ODR_56_05Hz
* GYR_ODR_28_025H
* */
SensorQMI8658::GYR_ODR_896_8Hz,
/*
* LPF_MODE_0 //2.66% of ODR
* LPF_MODE_1 //3.63% of ODR
* LPF_MODE_2 //5.39% of ODR
* LPF_MODE_3 //13.37% of ODR
* */
SensorQMI8658::LPF_MODE_3,
// selfTest enable
true);
// In 6DOF mode (accelerometer and gyroscope are both enabled),
// the output data rate is derived from the nature frequency of gyroscope
qmi.enableGyroscope();
qmi.enableAccelerometer();
//Enable Locking Mechanism
qmi.enableLockingMechanism();
// Set locking data event callback
qmi.setDataLockingEvevntCallBack(lockingMechanismHandler);
// Use interrupt .
// QMI8658 interrupt always outputs low level by default,
// and the interrupt is triggered when the rising edge
pinMode(IMU_INT1, INPUT_PULLUP);
#ifdef IMU_INT2
pinMode(IMU_INT2, INPUT_PULLUP);
attachInterrupt(IMU_INT2, setFlag, RISING);
#else
attachInterrupt(IMU_INT1, setFlag, RISING);
#endif
// qmi.enableINT(SensorQMI8658::IntPin1); //no use
// Enable data ready to interrupt pin2
qmi.enableINT(SensorQMI8658::IntPin2);
// Print register configuration information
qmi.dumpCtrlRegister();
Serial.println("Read data now...");
}
void loop()
{
if (interruptFlag) {
interruptFlag = false;
qmi.readSensorStatus();
}
delay(100);
}
@@ -0,0 +1,204 @@
/**
*
* @license MIT License
*
* Copyright (c) 2022 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file QMI8658_MadgwickAHRS.ino
* @author Lewis He (lewishe@outlook.com)
* @date 2022-10-16
*
*/
#include <Arduino.h>
#include <Wire.h>
#include <SPI.h>
#include "SensorQMI8658.hpp"
#include <MadgwickAHRS.h> //MadgwickAHRS from https://github.com/arduino-libraries/MadgwickAHRS
#define USE_WIRE
#ifndef SENSOR_SDA
#define SENSOR_SDA 17
#endif
#ifndef SENSOR_SCL
#define SENSOR_SCL 18
#endif
#ifndef SENSOR_IRQ
#define SENSOR_IRQ -1
#endif
#define IMU_CS 5
SensorQMI8658 qmi;
IMUdata acc;
IMUdata gyr;
Madgwick filter;
unsigned long microsPerReading, microsPrevious;
void setup()
{
Serial.begin(115200);
while (!Serial);
#ifdef USE_WIRE
//Using WIRE !!
if (!qmi.begin(Wire, QMI8658_L_SLAVE_ADDRESS, SENSOR_SDA, SENSOR_SCL)) {
Serial.println("Failed to find QMI8658 - check your wiring!");
while (1) {
delay(1000);
}
}
#else
if (!qmi.begin(IMU_CS)) {
Serial.println("Failed to find QMI8658 - check your wiring!");
while (1) {
delay(1000);
}
}
#endif
/* Get chip id*/
Serial.print("Device ID:");
Serial.println(qmi.getChipID(), HEX);
qmi.configAccelerometer(
/*
* ACC_RANGE_2G
* ACC_RANGE_4G
* ACC_RANGE_8G
* ACC_RANGE_16G
* */
SensorQMI8658::ACC_RANGE_2G,
/*
* ACC_ODR_1000H
* ACC_ODR_500Hz
* ACC_ODR_250Hz
* ACC_ODR_125Hz
* ACC_ODR_62_5Hz
* ACC_ODR_31_25Hz
* ACC_ODR_LOWPOWER_128Hz
* ACC_ODR_LOWPOWER_21Hz
* ACC_ODR_LOWPOWER_11Hz
* ACC_ODR_LOWPOWER_3H
* */
SensorQMI8658::ACC_ODR_1000Hz,
/*
* LPF_MODE_0 //2.66% of ODR
* LPF_MODE_1 //3.63% of ODR
* LPF_MODE_2 //5.39% of ODR
* LPF_MODE_3 //13.37% of ODR
* */
SensorQMI8658::LPF_MODE_0,
// selfTest enable
true);
qmi.configGyroscope(
/*
* GYR_RANGE_16DPS
* GYR_RANGE_32DPS
* GYR_RANGE_64DPS
* GYR_RANGE_128DPS
* GYR_RANGE_256DPS
* GYR_RANGE_512DPS
* GYR_RANGE_1024DPS
* */
SensorQMI8658::GYR_RANGE_256DPS,
/*
* GYR_ODR_7174_4Hz
* GYR_ODR_3587_2Hz
* GYR_ODR_1793_6Hz
* GYR_ODR_896_8Hz
* GYR_ODR_448_4Hz
* GYR_ODR_224_2Hz
* GYR_ODR_112_1Hz
* GYR_ODR_56_05Hz
* GYR_ODR_28_025H
* */
SensorQMI8658::GYR_ODR_896_8Hz,
/*
* LPF_MODE_0 //2.66% of ODR
* LPF_MODE_1 //3.63% of ODR
* LPF_MODE_2 //5.39% of ODR
* LPF_MODE_3 //13.37% of ODR
* */
SensorQMI8658::LPF_MODE_3,
// selfTest enable
true);
// In 6DOF mode (accelerometer and gyroscope are both enabled),
// the output data rate is derived from the nature frequency of gyroscope
qmi.enableGyroscope();
qmi.enableAccelerometer();
// Print register configuration information
qmi.dumpCtrlRegister();
// start filter
filter.begin(25);
// initialize variables to pace updates to correct rate
microsPerReading = 1000000 / 25;
microsPrevious = micros();
Serial.println("Read data now...");
}
void loop()
{
float roll, pitch, heading;
// check if it's time to read data and update the filter
if (micros() - microsPrevious >= microsPerReading) {
// read raw data from IMU
if (qmi.getDataReady()) {
qmi.getAccelerometer(acc.x, acc.y, acc.z);
qmi.getGyroscope(gyr.x, gyr.y, gyr.z);
// update the filter, which computes orientation
filter.updateIMU(gyr.x, gyr.y, gyr.z, acc.x, acc.y, acc.z);
// print the heading, pitch and roll
roll = filter.getRoll();
pitch = filter.getPitch();
heading = filter.getYaw();
Serial.print("Orientation: ");
Serial.print(heading);
Serial.print(" ");
Serial.print(pitch);
Serial.print(" ");
Serial.println(roll);
}
// increment previous time, so we keep proper pace
microsPrevious = microsPrevious + microsPerReading;
}
}
@@ -0,0 +1,196 @@
/**
*
* @license MIT License
*
* Copyright (c) 2022 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file QMI8658_GetDataExample.ino
* @author Lewis He (lewishe@outlook.com)
* @date 2022-10-16
*
*/
#include <Arduino.h>
#include <Wire.h>
#include <SPI.h>
#include "SensorQMI8658.hpp"
#define USE_WIRE
#ifndef SENSOR_SDA
#define SENSOR_SDA 17
#endif
#ifndef SENSOR_SCL
#define SENSOR_SCL 18
#endif
#ifndef SENSOR_IRQ
#define SENSOR_IRQ -1
#endif
#define IMU_CS 5
SensorQMI8658 qmi;
void setup()
{
Serial.begin(115200);
while (!Serial);
#ifdef USE_WIRE
//Using WIRE !!
if (!qmi.begin(Wire, QMI8658_L_SLAVE_ADDRESS, SENSOR_SDA, SENSOR_SCL)) {
Serial.println("Failed to find QMI8658 - check your wiring!");
while (1) {
delay(1000);
}
}
#else
if (!qmi.begin(IMU_CS)) {
Serial.println("Failed to find QMI8658 - check your wiring!");
while (1) {
delay(1000);
}
}
#endif
/* Get chip id*/
Serial.print("Device ID:");
Serial.println(qmi.getChipID(), HEX);
qmi.configAccelerometer(
/*
* ACC_RANGE_2G
* ACC_RANGE_4G
* ACC_RANGE_8G
* ACC_RANGE_16G
* */
SensorQMI8658::ACC_RANGE_4G,
/*
* ACC_ODR_1000H
* ACC_ODR_500Hz
* ACC_ODR_250Hz
* ACC_ODR_125Hz
* ACC_ODR_62_5Hz
* ACC_ODR_31_25Hz
* ACC_ODR_LOWPOWER_128Hz
* ACC_ODR_LOWPOWER_21Hz
* ACC_ODR_LOWPOWER_11Hz
* ACC_ODR_LOWPOWER_3H
* */
SensorQMI8658::ACC_ODR_125Hz,
/*
* LPF_MODE_0 //2.66% of ODR
* LPF_MODE_1 //3.63% of ODR
* LPF_MODE_2 //5.39% of ODR
* LPF_MODE_3 //13.37% of ODR
* */
SensorQMI8658::LPF_MODE_0,
// selfTest enable
true);
qmi.configGyroscope(
/*
* GYR_RANGE_16DPS
* GYR_RANGE_32DPS
* GYR_RANGE_64DPS
* GYR_RANGE_128DPS
* GYR_RANGE_256DPS
* GYR_RANGE_512DPS
* GYR_RANGE_1024DPS
* */
SensorQMI8658::GYR_RANGE_64DPS,
/*
* GYR_ODR_7174_4Hz
* GYR_ODR_3587_2Hz
* GYR_ODR_1793_6Hz
* GYR_ODR_896_8Hz
* GYR_ODR_448_4Hz
* GYR_ODR_224_2Hz
* GYR_ODR_112_1Hz
* GYR_ODR_56_05Hz
* GYR_ODR_28_025H
* */
SensorQMI8658::GYR_ODR_112_1Hz,
/*
* LPF_MODE_0 //2.66% of ODR
* LPF_MODE_1 //3.63% of ODR
* LPF_MODE_2 //5.39% of ODR
* LPF_MODE_3 //13.37% of ODR
* */
SensorQMI8658::LPF_MODE_3,
// selfTest enable
true);
// In 6DOF mode (accelerometer and gyroscope are both enabled),
// the output data rate is derived from the nature frequency of gyroscope
qmi.enableGyroscope();
qmi.enableAccelerometer();
qmi.configPedometer(0x007D, 0x00CC, 0x0066, 0x00C8, 0x14, 0x0A, 0, 0x04);
qmi.enablePedometer();
// Print register configuration information
qmi.dumpCtrlRegister();
Serial.println("Read data now...");
}
void loop()
{
// Get IMU status
uint8_t status = qmi.getStatusRegister();
Serial.printf("STATUS:0x%x BIN:", status);
Serial.println(status, BIN);
if (status & SensorQMI8658::EVENT_TAP_MOTION) {
Serial.println("Detected TAP event");
qmi.getTapStatus();
}
if (status & SensorQMI8658::EVENT_WOM_MOTION) {
Serial.println("Detected Wom event");
}
if (status & SensorQMI8658::EVENT_PEDOMETER_MOTION) {
Serial.println("Detected Pedometer event");
uint32_t val = qmi.getPedometerCounter();
Serial.println(val);
}
if (status & SensorQMI8658::EVENT_ANY_MOTION) {
Serial.println("Detected Any Motion event");
}
if (status & SensorQMI8658::EVENT_NO_MOTION) {
Serial.println("Detected No Motion event");
}
if (status & SensorQMI8658::EVENT_SIGNIFICANT_MOTION) {
Serial.println("Detected Significant Motion event");
}
delay(500);
}
@@ -0,0 +1,218 @@
/**
*
* @license MIT License
*
* Copyright (c) 2022 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file QMI8658_ReadFromFifoExample.ino
* @author Lewis He (lewishe@outlook.com)
* @date 2022-10-16
*
*/
#include <Arduino.h>
#include <Wire.h>
#include <SPI.h>
#include "SensorQMI8658.hpp"
#define USE_WIRE
#ifndef SENSOR_SDA
#define SENSOR_SDA 17
#endif
#ifndef SENSOR_SCL
#define SENSOR_SCL 18
#endif
#ifndef SENSOR_IRQ
#define SENSOR_IRQ -1
#endif
#define IMU_CS 5
SensorQMI8658 qmi;
IMUdata acc[128];
IMUdata gyr[128];
void setup()
{
Serial.begin(115200);
while (!Serial);
#ifdef USE_WIRE
//Using WIRE !!
if (!qmi.begin(Wire, QMI8658_L_SLAVE_ADDRESS, SENSOR_SDA, SENSOR_SCL)) {
Serial.println("Failed to find QMI8658 - check your wiring!");
while (1) {
delay(1000);
}
}
#else
//Using SPI !!
if (!qmi.begin(IMU_CS)) {
Serial.println("Failed to find QMI8658 - check your wiring!");
while (1) {
delay(1000);
}
}
#endif
/* Get chip id*/
Serial.print("Device ID:");
Serial.println(qmi.getChipID(), HEX);
qmi.configAccelerometer(
/*
* ACC_RANGE_2G
* ACC_RANGE_4G
* ACC_RANGE_8G
* ACC_RANGE_16G
* */
SensorQMI8658::ACC_RANGE_4G,
/*
* ACC_ODR_1000H
* ACC_ODR_500Hz
* ACC_ODR_250Hz
* ACC_ODR_125Hz
* ACC_ODR_62_5Hz
* ACC_ODR_31_25Hz
* ACC_ODR_LOWPOWER_128Hz
* ACC_ODR_LOWPOWER_21Hz
* ACC_ODR_LOWPOWER_11Hz
* ACC_ODR_LOWPOWER_3H
* */
SensorQMI8658::ACC_ODR_1000Hz,
/*
* LPF_MODE_0 //2.66% of ODR
* LPF_MODE_1 //3.63% of ODR
* LPF_MODE_2 //5.39% of ODR
* LPF_MODE_3 //13.37% of ODR
* */
SensorQMI8658::LPF_MODE_0,
// selfTest enable
true);
qmi.configGyroscope(
/*
* GYR_RANGE_16DPS
* GYR_RANGE_32DPS
* GYR_RANGE_64DPS
* GYR_RANGE_128DPS
* GYR_RANGE_256DPS
* GYR_RANGE_512DPS
* GYR_RANGE_1024DPS
* */
SensorQMI8658::GYR_RANGE_64DPS,
/*
* GYR_ODR_7174_4Hz
* GYR_ODR_3587_2Hz
* GYR_ODR_1793_6Hz
* GYR_ODR_896_8Hz
* GYR_ODR_448_4Hz
* GYR_ODR_224_2Hz
* GYR_ODR_112_1Hz
* GYR_ODR_56_05Hz
* GYR_ODR_28_025H
* */
SensorQMI8658::GYR_ODR_896_8Hz,
/*
* LPF_MODE_0 //2.66% of ODR
* LPF_MODE_1 //3.63% of ODR
* LPF_MODE_2 //5.39% of ODR
* LPF_MODE_3 //13.37% of ODR
* */
SensorQMI8658::LPF_MODE_3,
// selfTest enable
true);
qmi.configFIFO(
/**
* FIFO_MODE_BYPASS -- Disable fifo
* FIFO_MODE_FIFO -- Will not overwrite
* FIFO_MODE_STREAM -- Cover
*/
SensorQMI8658::FIFO_MODE_FIFO,
/*
* FIFO_SAMPLES_16
* FIFO_SAMPLES_32
* FIFO_SAMPLES_64
* FIFO_SAMPLES_128
* */
SensorQMI8658::FIFO_SAMPLES_16,
//FiFo mapped interrupt IO port
SensorQMI8658::IntPin1,
// watermark level
8);
// In 6DOF mode (accelerometer and gyroscope are both enabled),
// the output data rate is derived from the nature frequency of gyroscope
qmi.enableGyroscope();
qmi.enableAccelerometer();
// Print register configuration information
qmi.dumpCtrlRegister();
Serial.println("Read data now...");
}
void loop()
{
// If the reading is successful, true will be returned
if (!qmi.readFromFifo(acc, 128, gyr, 128)) {
return;
}
for (int i = 0; i < 16; ++i) {
Serial.print("ACCEL: ");
Serial.print("X:");
Serial.print(acc[i].x);
Serial.print(" Y:");
Serial.print(acc[i].y);
Serial.print(" Z:");
Serial.println(acc[i].z);
Serial.print("GYRO: ");
Serial.print(" X:");
Serial.print(gyr[i].x);
Serial.print(" Y:");
Serial.print(gyr[i].y );
Serial.print(" Z:");
Serial.println(gyr[i].z);
}
delay(100);
}
@@ -0,0 +1,160 @@
/**
*
* @license MIT License
*
* Copyright (c) 2022 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file QMI8658_WakeOnMotion.ino
* @author Lewis He (lewishe@outlook.com)
* @date 2022-11-05
*
*/
#include <Arduino.h>
#include <Wire.h>
#include <SPI.h>
#include "SensorQMI8658.hpp"
// #define USE_WIRE
#ifndef SENSOR_SDA
#define SENSOR_SDA 17
#endif
#ifndef SENSOR_SCL
#define SENSOR_SCL 18
#endif
#ifndef SENSOR_IRQ
#define SENSOR_IRQ -1
#endif
SensorQMI8658 qmi;
IMUdata acc;
IMUdata gyr;
bool interruptFlag = false;
void setFlag(void)
{
interruptFlag = true;
}
void setup()
{
Serial.begin(115200);
while (!Serial);
#ifdef USE_WIRE
//Using WIRE !!
if (!qmi.begin(Wire, QMI8658_L_SLAVE_ADDRESS, SENSOR_SDA, SENSOR_SCL)) {
Serial.println("Failed to find QMI8658 - check your wiring!");
while (1) {
delay(1000);
}
}
#else
#ifndef CONFIG_IDF_TARGET_ESP32
//Use tbeams3 defalut spi pinz
#define SPI_MOSI (35)
#define SPI_SCK (36)
#define SPI_MISO (37)
#define SPI_CS (47)
#define IMU_CS (34)
#define IMU_INT1 (33) //INTERRUPT PIN1 & PIN2 ,Use or logic to form a pin
pinMode(SPI_CS, OUTPUT); //sdcard pin set high
digitalWrite(SPI_CS, HIGH);
if (!qmi.begin(IMU_CS, SPI_MOSI, SPI_MISO, SPI_SCK)) {
#else
//Use esp32dev module defalut spi pin
#define IMU_CS (5)
#define IMU_INT1 (15)
#define IMU_INT2 (22)
if (!qmi.begin(IMU_CS)) {
#endif
Serial.println("Failed to find QMI8658 - check your wiring!");
while (1) {
delay(1000);
}
}
#endif
/* Get chip id*/
Serial.print("Device ID:");
Serial.println(qmi.getChipID(), HEX);
// enabling wake on motion low power mode with a threshold of 120 mg and
// an accelerometer data rate of 128 Hz.
qmi.configWakeOnMotion();
/*
* When the QMI8658 is configured as Wom, the interrupt level is arbitrary,
* not absolute high or low, and it is in the jump transition state
*/
pinMode(IMU_INT1, INPUT_PULLUP);
#ifdef IMU_INT2
pinMode(IMU_INT2, INPUT_PULLUP);
attachInterrupt(IMU_INT2, setFlag, CHANGE);
#else
attachInterrupt(IMU_INT1, setFlag, CHANGE);
#endif
// Print register configuration information
qmi.dumpCtrlRegister();
}
void loop()
{
if (interruptFlag) {
interruptFlag = false;
uint8_t status = qmi.getStatusRegister();
Serial.printf("status:0x%X BIN:", status);
Serial.println(status, BIN);
if (status & SensorQMI8658::EVENT_SIGNIFICANT_MOTION) {
Serial.println("EVENT_SIGNIFICANT_MOTION");
} else if (status & SensorQMI8658::EVENT_NO_MOTION) {
Serial.println("EVENT_NO_MOITON");
} else if (status & SensorQMI8658::EVENT_ANY_MOTION) {
Serial.println("EVENT_ANY_MOTION");
} else if (status & SensorQMI8658::EVENT_PEDOMETER_MOTION) {
Serial.println("EVENT_PEDOMETER_MOTION");
} else if (status & SensorQMI8658::EVENT_WOM_MOTION) {
Serial.println("EVENT_WOM_MOTION");
} else if (status & SensorQMI8658::EVENT_TAP_MOTION) {
Serial.println("EVENT_TAP_MOTION");
}
}
}
@@ -0,0 +1,152 @@
/**
*
* @license MIT License
*
* Copyright (c) 2022 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file QMI8658_WakeOnMotionCallBack.ino
* @author Lewis He (lewishe@outlook.com)
* @date 2022-11-07
*
*/
#include <Arduino.h>
#include <Wire.h>
#include <SPI.h>
#include "SensorQMI8658.hpp"
// #define USE_WIRE
#ifndef SENSOR_SDA
#define SENSOR_SDA 17
#endif
#ifndef SENSOR_SCL
#define SENSOR_SCL 18
#endif
#ifndef SENSOR_IRQ
#define SENSOR_IRQ -1
#endif
SensorQMI8658 qmi;
IMUdata acc;
IMUdata gyr;
bool interruptFlag = false;
void setFlag(void)
{
interruptFlag = true;
}
void wakeUp()
{
Serial.println("Awake!");
}
void setup()
{
Serial.begin(115200);
while (!Serial);
#ifdef USE_WIRE
//Using WIRE !!
if (!qmi.begin(Wire, QMI8658_L_SLAVE_ADDRESS, SENSOR_SDA, SENSOR_SCL)) {
Serial.println("Failed to find QMI8658 - check your wiring!");
while (1) {
delay(1000);
}
}
#else
#ifndef CONFIG_IDF_TARGET_ESP32
//Use tbeams3 defalut spi pinz
#define SPI_MOSI (35)
#define SPI_SCK (36)
#define SPI_MISO (37)
#define SPI_CS (47)
#define IMU_CS (34)
#define IMU_INT1 (33) //INTERRUPT PIN1 & PIN2 ,Use or logic to form a pin
pinMode(SPI_CS, OUTPUT); //sdcard pin set high
digitalWrite(SPI_CS, HIGH);
if (!qmi.begin(IMU_CS, SPI_MOSI, SPI_MISO, SPI_SCK)) {
#else
//Use esp32dev module defalut spi pin
#define IMU_CS (5)
#define IMU_INT1 (15)
#define IMU_INT2 (22)
if (!qmi.begin(IMU_CS)) {
#endif
Serial.println("Failed to find QMI8658 - check your wiring!");
while (1) {
delay(1000);
}
}
#endif
/* Get chip id*/
Serial.print("Device ID:");
Serial.println(qmi.getChipID(), HEX);
// enabling wake on motion low power mode with a threshold of 200 mg and
// an accelerometer data rate of 128 Hz.
qmi.configWakeOnMotion();
qmi.setWakeupMotionEventCallBack(wakeUp);
/*
* When the QMI8658 is configured as Wom, the interrupt level is arbitrary,
* not absolute high or low, and it is in the jump transition state
*/
pinMode(IMU_INT1, INPUT_PULLUP);
#ifdef IMU_INT2
pinMode(IMU_INT2, INPUT_PULLUP);
attachInterrupt(IMU_INT2, setFlag, CHANGE);
#else
attachInterrupt(IMU_INT1, setFlag, CHANGE);
#endif
// Print register configuration information
qmi.dumpCtrlRegister();
}
void loop()
{
if (interruptFlag) {
interruptFlag = false;
qmi.readSensorStatus();
}
}
@@ -0,0 +1,92 @@
/**
*
* @license MIT License
*
* Copyright (c) 2022 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file TouchDrv_CHSC5816_GetPoint.ino
* @author Lewis He (lewishe@outlook.com)
* @date 2023-04-17
*
*/
#include <Wire.h>
#include <SPI.h>
#include <Arduino.h>
#include "TouchDrvCHSC5816.hpp"
#ifndef SENSOR_SDA
#define SENSOR_SDA 39
#endif
#ifndef SENSOR_SCL
#define SENSOR_SCL 40
#endif
#ifndef SENSOR_IRQ
#define SENSOR_IRQ 13
#endif
#ifndef SENSOR_RST
#define SENSOR_RST 14
#endif
TouchDrvCHSC5816 touch;
void setup()
{
Serial.begin(115200);
while (!Serial);
touch.setPins(SENSOR_RST, SENSOR_IRQ);
if (!touch.begin(Wire, CHSC5816_SLAVE_ADDRESS, SENSOR_SDA, SENSOR_SCL)) {
Serial.println("Failed to find CHSC5816 - check your wiring!");
while (1) {
delay(1000);
}
}
Serial.println("Init CHSC5816 Touch device success!");
}
void loop()
{
int16_t x[2], y[2];
if (digitalRead(SENSOR_IRQ) == LOW) {
uint8_t touched = touch.getPoint(x, y);
for (int i = 0; i < touched; ++i) {
Serial.print("X[");
Serial.print(i);
Serial.print("]:");
Serial.print(x[i]);
Serial.print(" ");
Serial.print(" Y[");
Serial.print(i);
Serial.print("]:");
Serial.print(y[i]);
Serial.print(" ");
}
Serial.println();
}
}
@@ -0,0 +1,163 @@
/**
*
* @license MIT License
*
* Copyright (c) 2024 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file TouchDrv_CST9217_GetPoint.ino
* @author Lewis He (lewishe@outlook.com)
* @date 2024-07-27
*
*/
#include <Wire.h>
#include <SPI.h>
#include <Arduino.h>
#include "TouchDrvCST92xx.h"
#include "SensorWireHelper.h"
#ifndef SENSOR_SDA
#define SENSOR_SDA 8
#endif
#ifndef SENSOR_SCL
#define SENSOR_SCL 10
#endif
#ifndef SENSOR_IRQ
#define SENSOR_IRQ 5
#endif
#ifndef SENSOR_RST
#define SENSOR_RST -1
#endif
TouchDrvCST92xx touch;
int16_t x[5], y[5];
bool isPressed = false;
void setup()
{
Serial.begin(115200);
while (!Serial);
#if SENSOR_RST != -1
pinMode(SENSOR_RST, OUTPUT);
digitalWrite(SENSOR_RST, LOW);
delay(30);
digitalWrite(SENSOR_RST, HIGH);
delay(50);
delay(1000);
#endif
#if defined(ARDUINO_ARCH_RP2040)
Wire.setSCL(SENSOR_SCL);
Wire.setSDA(SENSOR_SDA);
Wire.begin();
#elif defined(NRF52840_XXAA) || defined(NRF52832_XXAA)
Wire.setPins(SENSOR_SDA, SENSOR_SCL);
Wire.begin();
#else
Wire.begin(SENSOR_SDA, SENSOR_SCL);
#endif
// Scan I2C devices
SensorWireHelper::dumpDevices(Wire);
// uint8_t touchAddress = 0x1A; // Other device addresses are determined by the touch firmware and are generally 0X5A by default.
uint8_t touchAddress = 0x5A; // The device address is determined according to the actual situation. Not all device addresses are 0X5A. There can also be other customized device addresses.
// Set to skip register check, used when the touch device address conflicts with other I2C device addresses [0x5A]
touch.jumpCheck();
touch.setPins(SENSOR_RST, SENSOR_IRQ);
bool result = touch.begin(Wire, touchAddress, SENSOR_SDA, SENSOR_SCL);
if (result == false) {
Serial.println("touch is not online..."); while (1)delay(1000);
}
Serial.print("Model :"); Serial.println(touch.getModelName());
touch.setCoverScreenCallback([](void *ptr) {
Serial.print(millis());
Serial.println(" : The screen is covered");
}, NULL);
Serial.println("Enter touch sleep mode.");
// Unable to obtain coordinates after turning on sleep
// CST9217 Work current ~= 1.3mA
// CST9217 sleep current = 3.4 uA
touch.sleep();
int i = 10;
while (i--) {
Serial.printf("Wake up after %d seconds\n", i);
delay(1000);
}
// Wakeup touch
touch.reset();
// Set touch max xy
// touch.setMaxCoordinates(240, 296);
// Set swap xy
// touch.setSwapXY(true);
// Set mirror xy
// touch.setMirrorXY(true, true);
//Register touch plane interrupt pin
attachInterrupt(SENSOR_IRQ, []() {
isPressed = true;
}, FALLING);
}
void loop()
{
if (isPressed) {
isPressed = false;
uint8_t touched = touch.getPoint(x, y, touch.getSupportTouchPoint());
if (touched) {
for (int i = 0; i < touched; ++i) {
Serial.print("X[");
Serial.print(i);
Serial.print("]:");
Serial.print(x[i]);
Serial.print(" ");
Serial.print(" Y[");
Serial.print(i);
Serial.print("]:");
Serial.print(y[i]);
Serial.print(" ");
}
Serial.println();
}
}
delay(30);
}
@@ -0,0 +1,179 @@
/**
*
* @license MIT License
*
* Copyright (c) 2022 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file TouchDrv_CSTxxx_GetPoint.ino
* @author Lewis He (lewishe@outlook.com)
* @date 2023-04-24
*
*/
#include <Wire.h>
#include <SPI.h>
#include <Arduino.h>
#include "TouchDrvCSTXXX.hpp"
#ifndef SENSOR_SDA
#define SENSOR_SDA 8
#endif
#ifndef SENSOR_SCL
#define SENSOR_SCL 10
#endif
#ifndef SENSOR_IRQ
#define SENSOR_IRQ 5
#endif
#ifndef SENSOR_RST
#define SENSOR_RST -1
#endif
TouchDrvCSTXXX touch;
int16_t x[5], y[5];
void scanDevices(void)
{
byte error, address;
int nDevices = 0;
Serial.println("Scanning for I2C devices ...");
for (address = 0x01; address < 0x7f; address++) {
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0) {
Serial.printf("I2C device found at address 0x%02X\n", address);
nDevices++;
} else if (error != 2) {
Serial.printf("Error %d at address 0x%02X\n", error, address);
}
}
if (nDevices == 0) {
Serial.println("No I2C devices found");
}
}
void setup()
{
Serial.begin(115200);
while (!Serial);
#if SENSOR_RST != -1
pinMode(SENSOR_RST, OUTPUT);
digitalWrite(SENSOR_RST, LOW);
delay(30);
digitalWrite(SENSOR_RST, HIGH);
delay(50);
// delay(1000);
#endif
// Search for known CSTxxx device addresses
uint8_t address = 0xFF;
#if defined(ARDUINO_ARCH_RP2040)
Wire.setSCL(SENSOR_SCL);
Wire.setSDA(SENSOR_SDA);
Wire.begin();
#elif defined(NRF52840_XXAA) || defined(NRF52832_XXAA)
Wire.setPins(SENSOR_SDA, SENSOR_SCL);
Wire.begin();
#else
Wire.begin(SENSOR_SDA, SENSOR_SCL);
#endif
// Scan I2C devices
scanDevices();
Wire.beginTransmission(CST816_SLAVE_ADDRESS);
if (Wire.endTransmission() == 0) {
address = CST816_SLAVE_ADDRESS;
}
Wire.beginTransmission(CST226SE_SLAVE_ADDRESS);
if (Wire.endTransmission() == 0) {
address = CST226SE_SLAVE_ADDRESS;
}
Wire.beginTransmission(CST328_SLAVE_ADDRESS);
if (Wire.endTransmission() == 0) {
address = CST328_SLAVE_ADDRESS;
}
while (address == 0xFF) {
Serial.println("Could't find touch chip!"); delay(1000);
}
touch.setPins(SENSOR_RST, SENSOR_IRQ);
touch.begin(Wire, address, SENSOR_SDA, SENSOR_SCL);
Serial.print("Model :"); Serial.println(touch.getModelName());
// T-Display-S3 CST816 touch panel, touch button coordinates are is 85 , 160
touch.setCenterButtonCoordinate(85, 360);
// T-Display-AMOLED 1.91 Inch CST816T touch panel, touch button coordinates is 600, 120.
// touch.setCenterButtonCoordinate(600, 120); // Only suitable for AMOLED 1.91 inch
// Depending on the touch panel, not all touch panels have touch buttons.
touch.setHomeButtonCallback([](void *user_data) {
Serial.println("Home key pressed!");
}, NULL);
// Unable to obtain coordinates after turning on sleep
// CST816T sleep current = 1.1 uA
// CST226SE sleep current = 60 uA
// touch.sleep();
// Set touch max xy
// touch.setMaxCoordinates(536, 240);
// Set swap xy
// touch.setSwapXY(true);
// Set mirror xy
// touch.setMirrorXY(true, true);
}
void loop()
{
uint8_t touched = touch.getPoint(x, y, touch.getSupportTouchPoint());
if (touched) {
for (int i = 0; i < touched; ++i) {
Serial.print("X[");
Serial.print(i);
Serial.print("]:");
Serial.print(x[i]);
Serial.print(" ");
Serial.print(" Y[");
Serial.print(i);
Serial.print("]:");
Serial.print(y[i]);
Serial.print(" ");
}
Serial.println();
}
delay(5);
}
@@ -0,0 +1,115 @@
/**
*
* @license MIT License
*
* Copyright (c) 2022 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file TouchDrv_FT3267_LilyGo_T_RGB.ino
* @author Lewis He (lewishe@outlook.com)
* @date 2023-04-17
*
*/
#include <Wire.h>
#include <SPI.h>
#include <Arduino.h>
#include "TouchDrvFT6X36.hpp"
#include "ExtensionIOXL9555.hpp"
#ifndef SENSOR_SDA
#define SENSOR_SDA 8
#endif
#ifndef SENSOR_SCL
#define SENSOR_SCL 48
#endif
#ifndef SENSOR_IRQ
#define SENSOR_IRQ 1
#endif
#ifndef SENSOR_RST
#define SENSOR_RST 1
#endif
TouchDrvFT6X36 touch;
ExtensionIOXL9555 extIO;
void setup()
{
Serial.begin(115200);
while (!Serial);
pinMode(SENSOR_IRQ, INPUT);
// T-RGB Use 0x20 device address
if (!extIO.begin(Wire, XL9555_SLAVE_ADDRESS0, SENSOR_SDA, SENSOR_SCL)) {
Serial.println("Failed to find XL9555 - check your wiring!");
while (1) {
delay(1000);
}
}
// Set PORT0 as output
extIO.configPort(ExtensionIOXL9555::PORT0, OUTPUT);
extIO.writePort(ExtensionIOXL9555::PORT0, 0xFF);
extIO.digitalWrite(SENSOR_RST, LOW);
delay(300);
extIO.digitalWrite(SENSOR_RST, HIGH);
delay(300);
if (!touch.begin(Wire, FT3267_SLAVE_ADDRESS, SENSOR_SDA, SENSOR_SCL)) {
Serial.println("Failed to find FT3267 - check your wiring!");
while (1) {
delay(1000);
}
}
touch.interruptTrigger();
Serial.println("Init FT3267 Sensor success!");
}
void loop()
{
int16_t x[2], y[2];
if (digitalRead(SENSOR_IRQ) == LOW) {
uint8_t touched = touch.getPoint(x, y, 2);
for (int i = 0; i < touched; ++i) {
Serial.print("X[");
Serial.print(i);
Serial.print("]:");
Serial.print(x[i]);
Serial.print(" ");
Serial.print(" Y[");
Serial.print(i);
Serial.print("]:");
Serial.print(y[i]);
Serial.print(" ");
}
Serial.println();
}
delay(50);
}
@@ -0,0 +1,92 @@
/**
*
* @license MIT License
*
* Copyright (c) 2022 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file TouchDrv_FT6232_GetPoint.ino
* @author Lewis He (lewishe@outlook.com)
* @date 2023-04-02
*
*/
#include <Wire.h>
#include <SPI.h>
#include <Arduino.h>
#include "TouchDrvFT6X36.hpp"
#ifndef SENSOR_SDA
#define SENSOR_SDA 21
#endif
#ifndef SENSOR_SCL
#define SENSOR_SCL 22
#endif
#ifndef SENSOR_IRQ
#define SENSOR_IRQ 39
#endif
TouchDrvFT6X36 touch;
void setup()
{
Serial.begin(115200);
while (!Serial);
pinMode(SENSOR_IRQ, INPUT);
if (!touch.begin(Wire, FT6X36_SLAVE_ADDRESS, SENSOR_SDA, SENSOR_SCL)) {
Serial.println("Failed to find FT6X36 - check your wiring!");
while (1) {
delay(1000);
}
}
touch.interruptTrigger();
Serial.println("Init FT6X36 Sensor success!");
}
void loop()
{
int16_t x[2], y[2];
if (digitalRead(SENSOR_IRQ) == LOW) {
uint8_t touched = touch.getPoint(x, y, 2);
for (int i = 0; i < touched; ++i) {
Serial.print("X[");
Serial.print(i);
Serial.print("]:");
Serial.print(x[i]);
Serial.print(" ");
Serial.print(" Y[");
Serial.print(i);
Serial.print("]:");
Serial.print(y[i]);
Serial.print(" ");
}
Serial.println();
}
delay(50);
}
@@ -0,0 +1,130 @@
/**
*
* @license MIT License
*
* Copyright (c) 2022 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file GT911_GetPoint.ino
* @author Lewis He (lewishe@outlook.com)
* @date 2023-04-12
*
*/
#include <Wire.h>
#include <SPI.h>
#include <Arduino.h>
#include "TouchDrvGT911.hpp"
#ifndef SENSOR_SDA
#define SENSOR_SDA 2
#endif
#ifndef SENSOR_SCL
#define SENSOR_SCL 3
#endif
#ifndef SENSOR_IRQ
#define SENSOR_IRQ 1
#endif
#ifndef SENSOR_RST
#define SENSOR_RST 10
#endif
TouchDrvGT911 touch;
int16_t x[5], y[5];
void setup()
{
Serial.begin(115200);
while (!Serial);
// If the reset pin and interrupt pin can be controlled by GPIO, the device address can be set arbitrarily
// If the interrupt and reset pins are not connected, you can pass in the -1 parameter and the library will automatically determine the address.
touch.setPins(SENSOR_RST, SENSOR_IRQ);
if (!touch.begin(Wire, GT911_SLAVE_ADDRESS_L, SENSOR_SDA, SENSOR_SCL)) {
while (1) {
Serial.println("Failed to find GT911 - check your wiring!");
delay(1000);
}
}
Serial.println("Init GT911 Sensor success!");
// Set the center button to trigger the callback , Only for specific devices, e.g LilyGo-EPD47 S3 GT911
touch.setHomeButtonCallback([](void *user_data) {
Serial.println("Home button pressed!");
}, NULL);
/*
* GT911 Interrupt mode
* * */
// Low level when idle, converts to high level when touched
// touch.setInterruptMode(HIGH_LEVEL_QUERY);
// Keep low level when idle, and trigger on the falling edge after touching, trigger once at a frequency of 100HZ, and keep high level for 10ms
// touch.setInterruptMode(RISING);
// Keep high level when idle, and switch to low level when touched
// touch.setInterruptMode(LOW_LEVEL_QUERY);
// Maintains high level when idle, and is triggered by the falling edge after being touched. The frequency is 100HZ and is triggered once. Maintains 10ms in the low level interval
// touch.setInterruptMode(FALLING);
/*
* GT911 Max touch point ,range: 1 ~ 5
* */
// touch.setMaxTouchPoint(1);
}
void loop()
{
if (touch.isPressed()) {
uint8_t touched = touch.getPoint(x, y, touch.getSupportTouchPoint());
if (touched > 0) {
Serial.print(millis());
Serial.print("ms ");
for (int i = 0; i < touched; ++i) {
Serial.print("X[");
Serial.print(i);
Serial.print("]:");
Serial.print(x[i]);
Serial.print(" ");
Serial.print(" Y[");
Serial.print(i);
Serial.print("]:");
Serial.print(y[i]);
Serial.print(" ");
}
Serial.println();
}
}
delay(100);
}
@@ -0,0 +1,165 @@
/**
*
* @license MIT License
*
* Copyright (c) 2022 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file TouchDrv_GT911_LilyGo_T_RGB.ino
* @author Lewis He (lewishe@outlook.com)
* @date 2023-04-12
*
*/
#include <Wire.h>
#include <SPI.h>
#include <Arduino.h>
#include "TouchDrvGT911.hpp"
#include "ExtensionIOXL9555.hpp"
#ifndef SENSOR_SDA
#define SENSOR_SDA 8
#endif
#ifndef SENSOR_SCL
#define SENSOR_SCL 48
#endif
#ifndef SENSOR_IRQ
#define SENSOR_IRQ 1
#endif
#ifndef SENSOR_RST
#define SENSOR_RST 1
#endif
TouchDrvGT911 touch;
ExtensionIOXL9555 extIO;
int16_t x[5], y[5];
void scanDevices(void)
{
byte error, address;
int nDevices = 0;
Serial.println("Scanning for I2C devices ...");
for (address = 0x01; address < 0x7f; address++) {
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0) {
Serial.printf("I2C device found at address 0x%02X\n", address);
nDevices++;
} else if (error != 2) {
Serial.printf("Error %d at address 0x%02X\n", error, address);
}
}
if (nDevices == 0) {
Serial.println("No I2C devices found");
}
}
void digitalWrite_CB(uint32_t pin, uint8_t value)
{
extIO.digitalWrite(pin, value);
}
void pinMode_CB(uint32_t pin, uint8_t mode)
{
extIO.pinMode(pin, mode);
}
void setup()
{
Serial.begin(115200);
while (!Serial);
Serial.println("Start!");
#if defined(ARDUINO_ARCH_RP2040)
Wire.setSCL(SENSOR_SCL);
Wire.setSDA(SENSOR_SDA);
Wire.begin();
#elif defined(NRF52840_XXAA) || defined(NRF52832_XXAA)
Wire.setPins(SENSOR_SDA, SENSOR_SCL);
Wire.begin();
#else
Wire.begin(SENSOR_SDA, SENSOR_SCL);
#endif
scanDevices();
// T-RGB Use 0x20 device address
if (!extIO.begin(Wire, XL9555_SLAVE_ADDRESS0, SENSOR_SDA, SENSOR_SCL)) {
Serial.println("Failed to find XL9555 - check your wiring!");
while (1) {
delay(1000);
}
}
// Set PORT0 as output
extIO.configPort(ExtensionIOXL9555::PORT0, OUTPUT);
extIO.writePort(ExtensionIOXL9555::PORT0, 0xFF);
touch.setPins(SENSOR_RST, SENSOR_IRQ);
touch.setGpioWriteCallback(digitalWrite_CB);
touch.setGpioModeCallback(pinMode_CB);
if (!touch.begin(Wire, GT911_SLAVE_ADDRESS_L, SENSOR_SDA, SENSOR_SCL )) {
scanDevices();
while (1) {
Serial.println("Failed to find GT911 - check your wiring!");
delay(1000);
}
}
//Set to trigger on falling edge
touch.setInterruptMode(FALLING);
Serial.println("Init GT911 Sensor success!");
}
void loop()
{
if (touch.isPressed()) {
uint8_t touched = touch.getPoint(x, y, touch.getSupportTouchPoint());
if (touched) {
for (int i = 0; i < touched; ++i) {
Serial.print("X[");
Serial.print(i);
Serial.print("]:");
Serial.print(x[i]);
Serial.print(" ");
Serial.print(" Y[");
Serial.print(i);
Serial.print("]:");
Serial.print(y[i]);
Serial.print(" ");
}
Serial.println();
}
}
delay(5);
}
@@ -0,0 +1,208 @@
/**
*
* @license MIT License
*
* Copyright (c) 2024 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file TouchDrv_LilyGo_T_RGB.ino
* @author Lewis He (lewishe@outlook.com)
* @date 2024-01-21
* @note T-RGB has three types of screens, each of which uses different touch driver chips.
* The example demonstrates using the touch interface class and one sketch is suitable for three types of touch chips.
*/
#include <Wire.h>
#include <SPI.h>
#include <Arduino.h>
#include "ExtensionIOXL9555.hpp"
#include "TouchDrvGT911.hpp"
#include "TouchDrvFT6X36.hpp"
#include "TouchDrvCSTXXX.hpp"
#ifndef SENSOR_SDA
#define SENSOR_SDA 8
#endif
#ifndef SENSOR_SCL
#define SENSOR_SCL 48
#endif
#ifndef SENSOR_IRQ
#define SENSOR_IRQ 1 //ESP32S3 GPIO1
#endif
#ifndef SENSOR_RST
#define SENSOR_RST 1 //XL9555 GPIO1
#endif
TouchDrvInterface *touchDrv = NULL;
ExtensionIOXL9555 extension;
ExtensionIOXL9555::ExtensionGPIO tp_reset = ExtensionIOXL9555::IO1;
ExtensionIOXL9555::ExtensionGPIO power_enable = ExtensionIOXL9555::IO2;
int16_t x[5], y[5];
//GPIO callback function
void TouchDrvDigitalWrite(uint32_t gpio, uint8_t level)
{
if (gpio & 0x80) {
extension.digitalWrite(gpio & 0x7F, level);
} else {
digitalWrite(gpio, level);
}
}
//GPIO callback function
int TouchDrvDigitalRead(uint32_t gpio)
{
if (gpio & 0x80) {
return extension.digitalRead(gpio & 0x7F);
} else {
return digitalRead(gpio);
}
}
//GPIO callback function
void TouchDrvPinMode(uint32_t gpio, uint8_t mode)
{
if (gpio & 0x80) {
extension.pinMode(gpio & 0x7F, mode);
} else {
pinMode(gpio, mode);
}
}
bool setupTouchDrv()
{
//Touch interrupt uses ESP32S3 GPIO1, touch reset uses XL9555 extended IO chip GPIO1, and needs to add a mask to distinguish
const uint8_t touch_reset_pin = 1 | 0x80;
const uint8_t touch_irq_pin = 1;
bool result = false;
touchDrv = new TouchDrvCSTXXX();
// T-RGB reset GPIO is connected through the expansion chip, and the GPIO callback function needs to be set.
touchDrv->setGpioCallback(TouchDrvPinMode, TouchDrvDigitalWrite, TouchDrvDigitalRead);
touchDrv->setPins(touch_reset_pin, touch_irq_pin);
result = touchDrv->begin(Wire, CST816_SLAVE_ADDRESS, SENSOR_SDA, SENSOR_SCL);
if (result) {
const char *model = touchDrv->getModelName();
Serial.printf("Successfully initialized %s, using %s Driver!", model, model);
return true;
}
delete touchDrv;
touchDrv = new TouchDrvGT911();
// T-RGB reset GPIO is connected through the expansion chip, and the GPIO callback function needs to be set.
touchDrv->setGpioCallback(TouchDrvPinMode, TouchDrvDigitalWrite, TouchDrvDigitalRead);
touchDrv->setPins(touch_reset_pin, touch_irq_pin);
result = touchDrv->begin(Wire, GT911_SLAVE_ADDRESS_H, SENSOR_SDA, SENSOR_SCL);
if (result) {
Serial.println("Successfully initialized GT911, using GT911 Driver!");
return true;
}
delete touchDrv;
touchDrv = new TouchDrvFT6X36();
// T-RGB reset GPIO is connected through the expansion chip, and the GPIO callback function needs to be set.
touchDrv->setGpioCallback(TouchDrvPinMode, TouchDrvDigitalWrite, TouchDrvDigitalRead);
touchDrv->setPins(touch_reset_pin, touch_irq_pin);
result = touchDrv->begin(Wire, FT3267_SLAVE_ADDRESS, SENSOR_SDA, SENSOR_SCL);
if (result) {
const char *model = touchDrv->getModelName();
Serial.printf("Successfully initialized %s, using %s Driver!", model, model);
return true;
}
delete touchDrv;
return false;
}
void setup()
{
Serial.begin(115200);
while (!Serial);
Serial.println("Start!");
#if defined(ARDUINO_ARCH_RP2040)
Wire.setSCL(SENSOR_SCL);
Wire.setSDA(SENSOR_SDA);
Wire.begin();
#elif defined(NRF52840_XXAA) || defined(NRF52832_XXAA)
Wire.setPins(SENSOR_SDA, SENSOR_SCL);
Wire.begin();
#else
Wire.begin(SENSOR_SDA, SENSOR_SCL);
#endif
// T-RGB XL9555 Use 0x20 device address
if (!extension.begin(Wire, XL9555_SLAVE_ADDRESS0, SENSOR_SDA, SENSOR_SCL)) {
while (1) {
Serial.println("Failed to find XL9555 - check your wiring!");
delay(1000);
}
}
// T-RGB power enable GPIO connected to XL9555 expansion chip GPIO
extension.pinMode(power_enable, OUTPUT);
extension.digitalWrite(power_enable, HIGH);
// Initialize touch using touch driver interface class
if (!setupTouchDrv()) {
while (1) {
Serial.println("Failed to find touch - check your wiring!");
delay(1000);
}
}
}
void loop()
{
if (touchDrv) {
if (touchDrv->isPressed()) {
uint8_t touched = touchDrv->getPoint(x, y, touchDrv->getSupportTouchPoint());
if (touched) {
for (int i = 0; i < touched; ++i) {
Serial.print("X[");
Serial.print(i);
Serial.print("]:");
Serial.print(x[i]);
Serial.print(" ");
Serial.print(" Y[");
Serial.print(i);
Serial.print("]:");
Serial.print(y[i]);
Serial.print(" ");
}
Serial.println();
}
}
}
delay(5);
}
@@ -0,0 +1,83 @@
/**
*
* @license MIT License
*
* Copyright (c) 2022 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file XL9555_ExtensionIOInterrupt.ino
* @author Lewis He (lewishe@outlook.com)
* @date 2023-01-03
*
*/
#include <Wire.h>
#include <SPI.h>
#include <Arduino.h>
#include <time.h>
#include "ExtensionIOXL9555.hpp"
#ifndef SENSOR_SDA
#define SENSOR_SDA 17
#endif
#ifndef SENSOR_SCL
#define SENSOR_SCL 18
#endif
#ifndef SENSOR_IRQ
#define SENSOR_IRQ -1
#endif
ExtensionIOXL9555 extIO;
void setup()
{
Serial.begin(115200);
while (!Serial);
// Device address 0x20~0x27
if (!extIO.begin(Wire, XL9555_SLAVE_ADDRESS4, SENSOR_SDA, SENSOR_SCL)) {
Serial.println("Failed to find XL9555 - check your wiring!");
while (1) {
delay(1000);
}
}
pinMode(SENSOR_IRQ, INPUT_PULLUP);
// Set PORT0 as input,mask = 0xFF = all pin input
extIO.configPort(ExtensionIOXL9555::PORT0, 0xFF);
// Set PORT1 as input,mask = 0xFF = all pin input
extIO.configPort(ExtensionIOXL9555::PORT1, 0xFF);
}
void loop()
{
// When the interrupt occurs, we read the mask value of PORT
if (digitalRead(SENSOR_IRQ) == LOW) {
Serial.print("PORT0:0b");
Serial.print(extIO.readPort(ExtensionIOXL9555::PORT0), BIN);
Serial.print("\tPORT1:0b");
Serial.println(extIO.readPort(ExtensionIOXL9555::PORT1), BIN);
}
}
@@ -0,0 +1,117 @@
/**
*
* @license MIT License
*
* Copyright (c) 2022 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file XL9555_ExtensionIORead.ino
* @author Lewis He (lewishe@outlook.com)
* @date 2023-01-03
*
*/
#include <Wire.h>
#include <SPI.h>
#include <Arduino.h>
#include <time.h>
#include "ExtensionIOXL9555.hpp"
#ifndef SENSOR_SDA
#define SENSOR_SDA 17
#endif
#ifndef SENSOR_SCL
#define SENSOR_SCL 18
#endif
#ifndef SENSOR_IRQ
#define SENSOR_IRQ -1
#endif
ExtensionIOXL9555 extIO;
void setup()
{
Serial.begin(115200);
while (!Serial);
// Device address 0x20~0x27
if (!extIO.begin(Wire, XL9555_SLAVE_ADDRESS4, SENSOR_SDA, SENSOR_SCL)) {
Serial.println("Failed to find XL9555 - check your wiring!");
while (1) {
delay(1000);
}
}
pinMode(SENSOR_IRQ, INPUT_PULLUP);
// Set PORT0 as input,mask = 0xFF = all pin input
extIO.configPort(ExtensionIOXL9555::PORT0, 0xFF);
// Set PORT1 as input,mask = 0xFF = all pin input
extIO.configPort(ExtensionIOXL9555::PORT1, 0xFF);
}
void loop()
{
Serial.print("PORT0:0b");
Serial.print(extIO.readPort(ExtensionIOXL9555::PORT0), BIN);
Serial.print("\tPORT1:0b");
Serial.println(extIO.readPort(ExtensionIOXL9555::PORT1), BIN);
//Allowable range of parameter input: IO0 ~ IO15
Serial.println("IO0:\tIO1:\tIO2:\tIO3:\tIO4:\tIO5:\tIO6:\tIO7:\tIO8:\tIO9:\tIO10:\tIO11:\tIO12:\tIO13:\tIO14:\tIO15:\t");
Serial.print(extIO.digitalRead(ExtensionIOXL9555::IO0));
Serial.print("\t");
Serial.print(extIO.digitalRead(ExtensionIOXL9555::IO1));
Serial.print("\t");
Serial.print(extIO.digitalRead(ExtensionIOXL9555::IO2));
Serial.print("\t");
Serial.print(extIO.digitalRead(ExtensionIOXL9555::IO3));
Serial.print("\t");
Serial.print(extIO.digitalRead(ExtensionIOXL9555::IO4));
Serial.print("\t");
Serial.print(extIO.digitalRead(ExtensionIOXL9555::IO5));
Serial.print("\t");
Serial.print(extIO.digitalRead(ExtensionIOXL9555::IO6));
Serial.print("\t");
Serial.print(extIO.digitalRead(ExtensionIOXL9555::IO7));
Serial.print("\t");
Serial.print(extIO.digitalRead(ExtensionIOXL9555::IO8));
Serial.print("\t");
Serial.print(extIO.digitalRead(ExtensionIOXL9555::IO9));
Serial.print("\t");
Serial.print(extIO.digitalRead(ExtensionIOXL9555::IO10));
Serial.print("\t");
Serial.print(extIO.digitalRead(ExtensionIOXL9555::IO11));
Serial.print("\t");
Serial.print(extIO.digitalRead(ExtensionIOXL9555::IO12));
Serial.print("\t");
Serial.print(extIO.digitalRead(ExtensionIOXL9555::IO13));
Serial.print("\t");
Serial.print(extIO.digitalRead(ExtensionIOXL9555::IO14));
Serial.print("\t");
Serial.print(extIO.digitalRead(ExtensionIOXL9555::IO15));
Serial.println();
delay(1000);
}
@@ -0,0 +1,94 @@
/**
*
* @license MIT License
*
* Copyright (c) 2022 lewis he
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @file XL9555_ExtensionIOWirte.ino
* @author Lewis He (lewishe@outlook.com)
* @date 2023-01-03
*
*/
#include <Wire.h>
#include <SPI.h>
#include <Arduino.h>
#include <time.h>
#include "ExtensionIOXL9555.hpp"
#ifndef SENSOR_SDA
#define SENSOR_SDA 17
#endif
#ifndef SENSOR_SCL
#define SENSOR_SCL 18
#endif
#ifndef SENSOR_IRQ
#define SENSOR_IRQ -1
#endif
ExtensionIOXL9555 extIO;
void setup()
{
Serial.begin(115200);
while (!Serial);
// Device address 0x20~0x27
if (!extIO.begin(Wire, XL9555_SLAVE_ADDRESS4, SENSOR_SDA, SENSOR_SCL)) {
Serial.println("Failed to find XL9555 - check your wiring!");
while (1) {
delay(1000);
}
}
// Set PORT0 as output ,mask = 0x00 = all pin output
extIO.configPort(ExtensionIOXL9555::PORT0, 0x00);
// Set PORT1 as output ,mask = 0x00 = all pin output
extIO.configPort(ExtensionIOXL9555::PORT1, 0x00);
}
void loop()
{
// Set all PORTs to 1, and the parameters here are mask values, corresponding to the 0~7 bits
Serial.println("Set port HIGH");
extIO.writePort(ExtensionIOXL9555::PORT0, 0xFF);
delay(1000);
Serial.println("Set port LOW");
// Set all PORTs to 0, and the parameters here are mask values, corresponding to the 0~7 bits
extIO.writePort(ExtensionIOXL9555::PORT1, 0x00);
delay(1000);
Serial.println("digitalWrite");
extIO.digitalWrite(ExtensionIOXL9555::IO0, HIGH);
delay(1000);
Serial.println("digitalToggle");
extIO.digitalToggle(ExtensionIOXL9555::IO0);
delay(1000);
}