Skip to content

Commit 3475f75

Browse files
committed
Add LED matrix to the proximity demo example
1 parent dad73b3 commit 3475f75

File tree

3 files changed

+52
-7
lines changed

3 files changed

+52
-7
lines changed

examples/all/proximity_sensor_example.cc

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// This examples requires a APDS9960 sensor
55
// (https://www.adafruit.com/product/3595) connected to the qwiic0 connector.
66

7+
#include "../../libraries/sense_hat.hh"
78
#include <compartment.h>
89
#include <ctype.h>
910
#include <debug.hh>
@@ -20,6 +21,8 @@ const uint8_t ApdS9960Pdata = 0x9C;
2021
const uint8_t ApdS9960IdExp = 0xAB;
2122
const uint8_t ApdS9960I2cAddress = 0x39;
2223

24+
#define SENSE_HAT_AVAILABLE false
25+
2326
/// Expose debugging features unconditionally for this compartment.
2427
using Debug = ConditionalDebug<true, "proximity sensor example">;
2528

@@ -108,30 +111,72 @@ static uint8_t read_proximity_sensor(Mmio<OpenTitanI2c> i2c)
108111
return buf[0];
109112
}
110113

114+
void update_sense_hat(SenseHat *senseHat, uint8_t prox)
115+
{
116+
constexpr uint8_t RedOffset = 3u; // Minimum R on Sense HAT LEDs.
117+
constexpr uint8_t ColourRange = SenseHat::Colour::MaxRedValue - RedOffset;
118+
constexpr uint8_t ProxOffset = 16u; // Minimum prox to show on LEDs.
119+
constexpr uint8_t ProxRange = UINT8_MAX - ProxOffset;
120+
constexpr uint8_t NumLeds = 8u * 8u;
121+
constexpr SenseHat::Colour OffColour = {.red = 0U, .green = 0U, .blue = 0U};
122+
123+
SenseHat::Colour fb[NumLeds] = {OffColour};
124+
/* Scale the proximity to the number of LED Matrix pixels. Clamp and
125+
linearly scale the proximity / brightness scales for a better result. */
126+
prox = (prox < ProxOffset) ? 0u : prox - ProxOffset;
127+
uint8_t filled =
128+
static_cast<uint8_t>(static_cast<uint64_t>(prox) * NumLeds / ProxRange);
129+
filled = (filled > NumLeds) ? NumLeds : filled;
130+
for (uint8_t i = 0; i < filled; i++)
131+
{
132+
uint8_t red = static_cast<uint8_t>(
133+
static_cast<uint64_t>(i) * ColourRange / NumLeds + RedOffset);
134+
fb[i] = (SenseHat::Colour){.red = red, .green = 0u, .blue = 0u};
135+
}
136+
senseHat->set_pixels(fb);
137+
}
138+
111139
[[noreturn]] void __cheri_compartment("proximity_sensor_example") run()
112140
{
141+
// Initialise the Sense HAT if we use it in this demo
142+
SenseHat *senseHat = NULL;
143+
if (SENSE_HAT_AVAILABLE)
144+
{
145+
senseHat = new SenseHat();
146+
}
147+
113148
auto i2cSetup = [](Mmio<OpenTitanI2c> i2c) {
114149
i2c->reset_fifos();
115150
i2c->host_mode_set();
116151
i2c->speed_set(1);
117152
};
118153

119-
auto i2c1 = MMIO_CAPABILITY(OpenTitanI2c, i2c1);
120-
i2cSetup(i2c1);
154+
auto i2c0 = MMIO_CAPABILITY(OpenTitanI2c, i2c0);
155+
i2cSetup(i2c0);
121156
uint8_t addr;
122157

123158
auto rgbled = MMIO_CAPABILITY(SonataRgbLedController, rgbled);
124159

125-
setup_proximity_sensor(i2c1, ApdS9960I2cAddress);
160+
setup_proximity_sensor(i2c0, ApdS9960I2cAddress);
126161

127162
while (true)
128163
{
129-
uint8_t prox = read_proximity_sensor(i2c1);
164+
uint8_t prox = read_proximity_sensor(i2c0);
130165
Debug::log("Proximity is {}\r", prox);
131166
rgbled->rgb(SonataRgbLed::Led0, ((prox) >> 3), 0, 0);
132167
rgbled->rgb(SonataRgbLed::Led1, 0, (255 - prox) >> 3, 0);
133168
rgbled->update();
134169

170+
if (SENSE_HAT_AVAILABLE)
171+
{
172+
update_sense_hat(senseHat, prox);
173+
}
174+
135175
thread_millisecond_wait(100);
136176
}
177+
178+
if (SENSE_HAT_AVAILABLE)
179+
{
180+
delete senseHat;
181+
}
137182
}

examples/all/xmake.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ compartment("rgbled_lerp")
2020
add_files("rgbled_lerp.cc")
2121

2222
compartment("proximity_sensor_example")
23-
add_deps("debug")
23+
add_deps("debug", "sense_hat")
2424
add_files("proximity_sensor_example.cc")
2525

2626
compartment("sense_hat_demo")

examples/xmake.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ firmware("sonata_proximity_demo")
115115
compartment = "proximity_sensor_example",
116116
priority = 2,
117117
entry_point = "run",
118-
stack_size = 0x200,
118+
stack_size = 0x1000,
119119
trusted_stack_frames = 1
120120
}
121121
}, {expand = false})
@@ -131,7 +131,7 @@ firmware("proximity_test")
131131
compartment = "proximity_sensor_example",
132132
priority = 2,
133133
entry_point = "run",
134-
stack_size = 0x200,
134+
stack_size = 0x1000,
135135
trusted_stack_frames = 1
136136
}
137137
}, {expand = false})

0 commit comments

Comments
 (0)