SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
cm32181_light_interrupt.ino
Go to the documentation of this file.
1
30#include <Wire.h>
31#include <SPI.h>
32#include <Arduino.h>
33#include <LightSensorDrv.hpp>
34
35#ifndef SENSOR_SDA
36#define SENSOR_SDA 39
37#endif
38
39#ifndef SENSOR_SCL
40#define SENSOR_SCL 40
41#endif
42
43#ifndef SENSOR_IRQ
44#define SENSOR_IRQ 1
45#endif
46
48
49#ifdef ARDUINO_T_AMOLED_147
50#include <XPowersAXP2101.tpp> //PMU Library https://github.com/lewisxhe/XPowersLib.git
51XPowersAXP2101 power;
52#endif
53
55{
56 // T_AMOLED_147 The PMU voltage needs to be turned on to use the sensor
57#if defined(ARDUINO_T_AMOLED_147)
58 bool ret = power.begin(Wire, AXP2101_SLAVE_ADDRESS, SENSOR_SDA, SENSOR_SCL);
59 if (!ret) {
60 Serial.println("PMU NOT FOUND!\n");
61 }
62 power.setALDO1Voltage(1800); power.enableALDO1();
63 power.setALDO3Voltage(3300); power.enableALDO3();
64 power.setBLDO1Voltage(1800); power.enableBLDO1();
65#endif
66}
67
68#include "SensorWireHelper.h"
69
70void setup()
71{
72 Serial.begin(115200);
73 while (!Serial);
74
75 beginPower();
76
77 SensorWireHelper::dumpDevices(Wire);
78
79 pinMode(SENSOR_IRQ, INPUT_PULLUP);
80
81 if (!light.begin(Wire, CM32181_ADDR_PRIMARY, SENSOR_SDA, SENSOR_SCL)) {
82 Serial.println("Failed to find CM32181 - check your wiring!");
83 while (1) {
84 delay(1000);
85 }
86 }
87
88 Serial.println("Init CM32181 Sensor success!");
89
90 int id = light.getChipID();
91 Serial.print("CM32181 ID = "); Serial.println(id);
92
93 /*
94 Sensitivity mode selection
95 SAMPLING_X1
96 SAMPLING_X2
97 SAMPLING_X1_8
98 SAMPLING_X1_4
99 */
102 );
103
104 // Set high and low thresholds. If the threshold is higher or lower than the set threshold, an interrupt will be triggered.
105 uint16_t lowThresholdRaw = 100;
106 uint16_t highThresholdRaw = 250;
107 light.setIntThreshold(lowThresholdRaw, highThresholdRaw);
108
109 //Power On sensor
110 light.powerOn();
111
112 // Turn on interrupt
114}
115
116
117void loop()
118{
119 int pinVal = digitalRead(SENSOR_IRQ) ;
120 if (pinVal == LOW) {
121
122 // After triggering the interrupt, the interrupt status must be read
124
125 // Turn off interrupts
127
128 // Get Status
129 switch (event) {
131 Serial.println("Low interrupt event");
132 break;
134 Serial.println("High interrupt event");
135 break;
136 default:
137 Serial.println("This is an impossible place to reach");
138 break;
139 }
140
141 // Get raw data
142 uint16_t raw = light.getRaw();
143
144 // Get conversion data , The manual does not provide information on how to obtain the
145 // calibration value, now use the calibration value 0.28 provided by the manual
146 float lux = light.getLux();
147 Serial.print(" IRQ:"); Serial.print(pinVal);
148 Serial.print(" RAW:"); Serial.print(raw);
149 Serial.print(" Lux:"); Serial.println(lux);
150 // Turn on interrupts
152 }
153
154 delay(800);
155}
156
157
158
@license MIT License
@license MIT License
bool begin(SensorCommCustom::CustomCallback callback, uint8_t addr)
Initialize the sensor using custom callback interface.
High Sensitivity I2C Ambient Light Sensor (CM32181) driver.
void setSampling(Sampling sampling=SAMPLING_X1, IntegrationTime int_time=INTEGRATION_TIME_200MS)
Configure ALS sensitivity and integration time.
InterruptEvent getIrqStatus()
Read IRQ status (and clear interrupt by reading status register).
void enableINT()
Enable interrupt function (REG_ALS_CONF bit1).
void disableINT()
Disable interrupt function (REG_ALS_CONF bit1).
void powerOn()
Power on sensor (REG_ALS_CONF bit0 = 0).
InterruptEvent
Interrupt event type reported by getIrqStatus().
@ ALS_EVENT_HIGH_TRIGGER
High threshold window interrupt.
@ ALS_EVENT_LOW_TRIGGER
Low threshold window interrupt.
float getLux()
Convert raw ALS to lux (approximate).
uint16_t getRaw()
Read raw ALS data (16-bit).
void setIntThreshold(uint16_t low_threshold, uint16_t high_threshold)
Set ALS interrupt thresholds.
SensorCM32181 light
#define SENSOR_SCL
#define SENSOR_SDA
void beginPower()
#define SENSOR_IRQ