4
4
// This examples requires a APDS9960 sensor
5
5
// (https://www.adafruit.com/product/3595) connected to the qwiic0 connector.
6
6
7
+ #include " ../../libraries/sense_hat.hh"
7
8
#include < compartment.h>
8
9
#include < ctype.h>
9
10
#include < debug.hh>
@@ -20,6 +21,8 @@ const uint8_t ApdS9960Pdata = 0x9C;
20
21
const uint8_t ApdS9960IdExp = 0xAB ;
21
22
const uint8_t ApdS9960I2cAddress = 0x39 ;
22
23
24
+ #define SENSE_HAT_AVAILABLE false
25
+
23
26
// / Expose debugging features unconditionally for this compartment.
24
27
using Debug = ConditionalDebug<true , " proximity sensor example" >;
25
28
@@ -108,30 +111,72 @@ static uint8_t read_proximity_sensor(Mmio<OpenTitanI2c> i2c)
108
111
return buf[0 ];
109
112
}
110
113
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
+
111
139
[[noreturn]] void __cheri_compartment (" proximity_sensor_example" ) run()
112
140
{
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
+
113
148
auto i2cSetup = [](Mmio<OpenTitanI2c> i2c) {
114
149
i2c->reset_fifos ();
115
150
i2c->host_mode_set ();
116
151
i2c->speed_set (1 );
117
152
};
118
153
119
- auto i2c1 = MMIO_CAPABILITY (OpenTitanI2c, i2c1 );
120
- i2cSetup (i2c1 );
154
+ auto i2c0 = MMIO_CAPABILITY (OpenTitanI2c, i2c0 );
155
+ i2cSetup (i2c0 );
121
156
uint8_t addr;
122
157
123
158
auto rgbled = MMIO_CAPABILITY (SonataRgbLedController, rgbled);
124
159
125
- setup_proximity_sensor (i2c1 , ApdS9960I2cAddress);
160
+ setup_proximity_sensor (i2c0 , ApdS9960I2cAddress);
126
161
127
162
while (true )
128
163
{
129
- uint8_t prox = read_proximity_sensor (i2c1 );
164
+ uint8_t prox = read_proximity_sensor (i2c0 );
130
165
Debug::log (" Proximity is {}\r " , prox);
131
166
rgbled->rgb (SonataRgbLed::Led0, ((prox) >> 3 ), 0 , 0 );
132
167
rgbled->rgb (SonataRgbLed::Led1, 0 , (255 - prox) >> 3 , 0 );
133
168
rgbled->update ();
134
169
170
+ if (SENSE_HAT_AVAILABLE)
171
+ {
172
+ update_sense_hat (senseHat, prox);
173
+ }
174
+
135
175
thread_millisecond_wait (100 );
136
176
}
177
+
178
+ if (SENSE_HAT_AVAILABLE)
179
+ {
180
+ delete senseHat;
181
+ }
137
182
}
0 commit comments