Skip to content

Commit a6accdf

Browse files
authored
Merge pull request #44 from AxionQc/apds9999_support
2 parents 9f28f7d + befe75e commit a6accdf

6 files changed

Lines changed: 205 additions & 9 deletions

File tree

lib/ProtoTracer/Examples/Templates/ProtogenProjectTemplate.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ void ProtogenProject::UpdateFace(float ratio) {
9494
yOffset = fGenMatYMove.Update();
9595

9696
if (Menu::UseBoopSensor()) {
97-
isBooped = boop.isBooped();
97+
isBooped = boop60.isBooped();
98+
isBooped = boop99.isBooped();
9899
}
99100

100101
hud.SetEffect(Menu::GetEffect());// Pull Effect from menu and store reference in hud for observing data
@@ -557,7 +558,8 @@ ProtogenProject::ProtogenProject(CameraManager* cameras, Controller* controller,
557558
void ProtogenProject::Initialize() {
558559
controller->Initialize();
559560

560-
boop.Initialize(5);
561+
boop60.Initialize(5);
562+
boop99.Initialize(5);
561563

562564
hud.Initialize();
563565

lib/ProtoTracer/Examples/Templates/ProtogenProjectTemplate.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "../../Assets/Models/OBJ/Background.h"
1515
#include "../../ExternalDevices/InputDevices/Menu/Menu.h"
1616
#include "../../ExternalDevices/Sensors/APDS9960.h"
17+
#include "../../ExternalDevices/Sensors/APDS9999.h"
1718
#include "../../ExternalDevices/Displays/SSD1306.h"
1819
#include "../../ExternalDevices/Sensors/Microphone/MicrophoneFourier_MAX9814.h"
1920
#include "../../ExternalDevices/OutputDevices/FanController.h"
@@ -154,7 +155,8 @@ class ProtogenProject : public Project {
154155
/**
155156
* @brief Gesture sensor used for detecting "boops."
156157
*/
157-
APDS9960 boop;
158+
APDS9960 boop60;
159+
APDS9999 boop99;
158160

159161
/**
160162
* @brief Voice detection system based on FFT data.

lib/ProtoTracer/ExternalDevices/Displays/SSD1306.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,11 @@ class HeadsUpDisplay : public Effect {
165165
* @param str The string to print.
166166
*/
167167
void CheckInvertPrintText(int16_t x, int16_t y, uint8_t menu, const String& str);
168+
169+
/**
170+
* @brief Sets the array of face pixels.
171+
*
172+
* @param pixelGroup Pointer to the pixel group representing the face.
173+
*/
174+
void SetFacePixelArray(const IPixelGroup* pixelGroup);
168175
};
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#include "APDS9999.h"
2+
3+
Adafruit_APDS9999 APDS9999::apds;
4+
uint16_t APDS9999::proximity;
5+
uint16_t APDS9999::threshold;
6+
7+
MinFilter<10> APDS9999::minF = MinFilter<10>(false);
8+
TimeStep APDS9999::timeStep = TimeStep(5);
9+
float APDS9999::minimum = 0.0f;
10+
bool APDS9999::didBegin = false;
11+
bool APDS9999::isBright = false;
12+
bool APDS9999::isProx = false;
13+
14+
bool APDS9999::Initialize(uint8_t threshold)
15+
{
16+
APDS9999::threshold = threshold;
17+
18+
Wire.setClock(100000); // for longer-range transmissions
19+
Wire.begin();
20+
21+
#ifdef WS35
22+
Wire.setSDA(19);
23+
Wire.setSCL(18);
24+
#else
25+
Wire.setSDA(18);
26+
Wire.setSCL(19);
27+
#endif
28+
29+
Wire.beginTransmission(0x39);
30+
uint8_t error = Wire.endTransmission();
31+
32+
if (error == 0)
33+
{ // SSD1306 Found
34+
didBegin = apds.begin();
35+
36+
// apds.setLED(APDS9960_LEDDRIVE_12MA, APDS9960_LEDBOOST_100PCNT);
37+
// apds.setProxGain(APDS9960_PGAIN_1X);
38+
}
39+
else
40+
{
41+
didBegin = false;
42+
}
43+
44+
return didBegin;
45+
}
46+
47+
bool APDS9999::isBooped()
48+
{
49+
GetValue();
50+
51+
if (timeStep.IsReady())
52+
{
53+
minimum = minF.Filter(proximity);
54+
}
55+
56+
return proximity > minimum + threshold;
57+
}
58+
59+
void APDS9999::ResetI2CBus()
60+
{
61+
Wire.end(); // Disable the I2C hardware
62+
delay(10); // Wait a bit
63+
Wire.begin(); // Re-enable the I2C hardware
64+
}
65+
66+
uint8_t APDS9999::GetValue()
67+
{
68+
unsigned long cmdTime = millis();
69+
70+
if (didBegin)
71+
{
72+
if (!isProx)
73+
{
74+
apds.enableProximitySensor(true);
75+
isProx = true;
76+
}
77+
uint16_t proxVal = 0;
78+
apds.readProximity(&proxVal);
79+
proximity = proxVal;
80+
}
81+
82+
if (millis() - cmdTime > 100)
83+
{
84+
// Timeout occurred
85+
ResetI2CBus();
86+
}
87+
88+
return proximity;
89+
}
90+
91+
uint16_t APDS9999::GetBrightness()
92+
{
93+
uint16_t brightness;
94+
95+
if (didBegin)
96+
{
97+
if (!isBright)
98+
{
99+
apds.lightSensorEnabled();
100+
isBright = true;
101+
}
102+
103+
brightness = apds.getLightGain();
104+
}
105+
else
106+
{
107+
brightness = 0;
108+
}
109+
110+
return brightness;
111+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* @file APDS9999.h
3+
* @brief A class for interfacing with the Adafruit APDS9999 sensor.
4+
*
5+
* This file defines the APDS9999 class, which provides functionality for detecting proximity,
6+
* brightness, and determining if the sensor is "booped" (close proximity detection).
7+
*
8+
* @date 1/9/2026
9+
* @author Coela Can't, AxionQC
10+
*/
11+
12+
#pragma once
13+
14+
#include <Arduino.h> // Include for Arduino compatibility.
15+
#include <Wire.h> // Include for I2C communication.
16+
#include <Adafruit_APDS9999.h> // Include for Adafruit APDS9999 sensor library.
17+
#include "../../Utils/Filter/MinFilter.h" // Include for minimum filtering.
18+
#include "../../Utils/Time/TimeStep.h" // Include for timing utilities.
19+
20+
/**
21+
* @class APDS9999
22+
* @brief A class for managing the Adafruit APDS9999 sensor.
23+
*
24+
* The APDS9999 class provides methods for initializing the sensor, detecting proximity and brightness,
25+
* and determining if the sensor is close enough to trigger a "boop" detection.
26+
*/
27+
class APDS9999 {
28+
private:
29+
static Adafruit_APDS9999 apds; ///< Instance of the Adafruit APDS9999 sensor.
30+
static uint16_t proximity; ///< Current proximity value.
31+
static uint16_t threshold; ///< Threshold value for proximity detection.
32+
static MinFilter<10> minF; ///< Minimum filter for smoothing proximity values.
33+
static TimeStep timeStep; ///< Time utility for managing timing intervals.
34+
static float minimum; ///< Minimum detected value.
35+
static bool didBegin; ///< Flag indicating if the sensor has been initialized.
36+
static bool isBright; ///< Flag indicating if brightness detection is enabled.
37+
static bool isProx; ///< Flag indicating if proximity detection is enabled.
38+
39+
public:
40+
/**
41+
* @brief Initializes the APDS9999 sensor.
42+
*
43+
* @param threshold The threshold for proximity detection.
44+
* @return True if initialization is successful, false otherwise.
45+
*/
46+
static bool Initialize(uint8_t threshold);
47+
48+
/**
49+
* @brief Checks if the sensor is "booped" (close proximity detected).
50+
*
51+
* @return True if the sensor is booped, false otherwise.
52+
*/
53+
static bool isBooped();
54+
55+
/**
56+
* @brief Resets the I2C bus in case of communication issues.
57+
*/
58+
static void ResetI2CBus();
59+
60+
/**
61+
* @brief Retrieves the current proximity value.
62+
*
63+
* @return The current proximity value as an 8-bit integer.
64+
*/
65+
static uint8_t GetValue();
66+
67+
/**
68+
* @brief Retrieves the current brightness value.
69+
*
70+
* @return The current brightness value as a 16-bit integer.
71+
*/
72+
static uint16_t GetBrightness();
73+
};

platformio.ini

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ lib_deps =
2525
adafruit/Adafruit SSD1306@^2.5.7
2626
adafruit/Adafruit seesaw Library@^1.6.3
2727
adafruit/Adafruit MMC56x3@^1.0.0
28-
28+
adafruit/Adafruit APDS9999@^1.0.2
29+
2930
[env:teensy40hub75]
3031
extends = common
3132
platform = teensy@^4.18.0
@@ -48,7 +49,7 @@ build_flags =
4849

4950
[env:teensy40beta]
5051
extends = common
51-
platform = teensy
52+
platform = teensy@^4.18.0
5253
board = teensy40
5354
framework = arduino
5455
monitor_speed = 115200
@@ -58,7 +59,7 @@ build_flags =
5859

5960
[env:teensy40verifyrender]
6061
extends = common
61-
platform = teensy
62+
platform = teensy@^4.18.0
6263
board = teensy40
6364
framework = arduino
6465
monitor_speed = 115200
@@ -99,7 +100,7 @@ build_flags =
99100

100101
[env:teensy41beta]
101102
extends = common
102-
platform = teensy
103+
platform = teensy@^4.18.0
103104
board = teensy41
104105
framework = arduino
105106
monitor_speed = 115200
@@ -109,7 +110,7 @@ build_flags =
109110

110111
[env:teensy41verifyrender]
111112
extends = common
112-
platform = teensy
113+
platform = teensy@^4.18.0
113114
board = teensy41
114115
framework = arduino
115116
monitor_speed = 115200
@@ -120,7 +121,7 @@ build_flags =
120121

121122
[env:teensy41verifyhardware]
122123
extends = common
123-
platform = teensy
124+
platform = teensy@^4.18.0
124125
board = teensy41
125126
framework = arduino
126127
monitor_speed = 115200

0 commit comments

Comments
 (0)