ProtoTracer  1.0
Real-time 3D rendering and animation engine
Loading...
Searching...
No Matches
ProtogenHardwareTest.h
Go to the documentation of this file.
1#pragma once
2
3#include <Arduino.h>
4#include <Adafruit_APDS9960.h>
5#include <Adafruit_GFX.h>
6#include <Adafruit_SSD1306.h>
7#include <APDS9930.h>
8#include "Adafruit_NeoTrellis.h"
9
11private:
12 static void PrintAddress(uint8_t address, const String& text){
13 Serial.print(address, HEX);
14 Serial.print('\t');
15 Serial.println(text);
16 }
17
18 static void PrintAddressValue(uint8_t address, const String& text, int value){
19 Serial.print(address, HEX);
20 Serial.print('\t');
21 Serial.print(text);
22 Serial.print('\t');
23 Serial.println(value);
24 }
25
26 static bool TestI2CConnection(uint8_t address, String name, bool flip){
27 Wire.setClock(400000);//for longer range transmissions
28
29 if(flip){
30 Wire.setSDA(19);
31 Wire.setSCL(18);
32 }
33 else{
34 Wire.setSDA(18);
35 Wire.setSCL(19);
36 }
37
38 Wire.begin();
39
40 Wire.beginTransmission(address);
41
42 uint8_t error = Wire.endTransmission();
43
44 switch (error) {
45 case 0:
46 PrintAddress(address, " " + name + " Found!");
47 return true;
48 break;
49 case 1:
50 PrintAddress(address, " " + name + ":\tData too long to fit in transmit buffer.");
51 break;
52 case 2:
53 PrintAddress(address, " " + name + ":\tReceived NACK on transmit of address.");
54 break;
55 case 3:
56 PrintAddress(address, " " + name + ":\tReceived NACK on transmit of data.");
57 break;
58 case 4:
59 PrintAddress(address, " " + name + ":\tOther error.");
60 break;
61 default:
62 PrintAddress(address, " " + name + " not found. Unknown error.");
63 break;
64 }
65
66 Wire.end();
67
68 return false;
69 }
70
71 static void TestAPDS9960(uint8_t address, bool flip){
72 Wire.setClock(400000);//for longer range transmissions
73
74 if(flip){
75 Wire.setSDA(19);
76 Wire.setSCL(18);
77 }
78 else{
79 Wire.setSDA(18);
80 Wire.setSCL(19);
81 }
82
83 Wire.begin();
84
85 Wire.beginTransmission(address);
86
87 Adafruit_APDS9960 apds;
88
89 bool didBegin = apds.begin(10U, APDS9960_AGAIN_4X, address);
90 apds.enableProximity(true);
91
92 delay(500);
93
94 PrintAddressValue(address, " APDS Did Begin: ", didBegin);
95
96 if(didBegin){
97 PrintAddressValue(address, " APDS Proximity: ", apds.readProximity());
98 }
99
100 Wire.end();
101 }
102
103 static void TestAPDS9930(uint8_t address, bool flip){
104 Wire.setClock(400000);//for longer range transmissions
105
106 if(flip){
107 Wire.setSDA(19);
108 Wire.setSCL(18);
109 }
110 else{
111 Wire.setSDA(18);
112 Wire.setSCL(19);
113 }
114
115 Wire.begin();
116
117 Wire.beginTransmission(address);
118
119 APDS9930 apds = APDS9930();
120
121 bool didBegin = apds.init();
122
123 //if(!apds.setProximityGain(PGAIN_8X)) Serial.println(F("Something went wrong trying to set PGAIN"));
124
125 // Start running the APDS-9930 proximity sensor (no interrupts)
126 if(apds.enableProximitySensor(false)) Serial.println(F("Proximity sensor is now running"));
127
128 delay(500);
129
130 PrintAddressValue(address, " APDS Did Begin: ", didBegin);
131
132 uint16_t data = 100;
133 for (int i = 0; i < 10; i++){
134 bool didRead = apds.readProximity(data);
135
136 if(didBegin && didRead){
137 PrintAddressValue(address, " APDS Proximity: ", data);
138 }
139
140 delay(150);
141 }
142
143
144 Wire.end();
145 }
146
147 static void TestSSD1306(uint8_t address, bool flip){
148 Wire.setClock(100000);//for longer range transmissions
149
150 if(flip){
151 Wire.setSDA(19);
152 Wire.setSCL(18);
153 }
154 else{
155 Wire.setSDA(18);
156 Wire.setSCL(19);
157 }
158
159 Wire.begin();
160
161 Wire.beginTransmission(address);
162
163 Adafruit_SSD1306 display;
164
165 display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
166
167 // Clear the buffer and display
168 display.clearDisplay();
169 display.invertDisplay(true);
170 display.display();
171
172 // Draw some shapes and text
173 display.setTextSize(1);
174 display.setTextColor(SSD1306_WHITE);
175 display.setCursor(0, 0);
176 display.println(F("Hello, SSD1306!"));
177
178 display.drawRect(10, 10, 20, 20, SSD1306_WHITE);
179 display.fillCircle(60, 30, 15, SSD1306_WHITE);
180
181 display.display(); // Display the buffer content
182
183 Wire.end();
184 }
185
186 static Adafruit_NeoTrellis trellis;
187
188 //define a callback for key presses
189 static TrellisCallback blink(keyEvent evt){
190 // Check is the pad pressed?
191 if (evt.bit.EDGE == SEESAW_KEYPAD_EDGE_RISING) {
192 trellis.pixels.setPixelColor(evt.bit.NUM, Wheel(map(evt.bit.NUM, 0, trellis.pixels.numPixels(), 0, 255))); //on rising
193 } else if (evt.bit.EDGE == SEESAW_KEYPAD_EDGE_FALLING) {
194 // or is the pad released?
195 trellis.pixels.setPixelColor(evt.bit.NUM, 0); //off falling
196 }
197
198 // Turn on/off the neopixels!
199 trellis.pixels.show();
200
201 return 0;
202 }
203
204 static uint32_t Wheel(byte WheelPos) {
205 if(WheelPos < 85) {
206 return trellis.pixels.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
207 } else if(WheelPos < 170) {
208 WheelPos -= 85;
209 return trellis.pixels.Color(255 - WheelPos * 3, 0, WheelPos * 3);
210 } else {
211 WheelPos -= 170;
212 return trellis.pixels.Color(0, WheelPos * 3, 255 - WheelPos * 3);
213 }
214 return 0;
215 }
216
217 static void TestTrellisHardware(bool flip){
218 Wire.setClock(50000);//for longer range transmissions
219
220 if(flip){
221 Wire.setSDA(19);
222 Wire.setSCL(18);
223 }
224 else{
225 Wire.setSDA(18);
226 Wire.setSCL(19);
227 }
228
229 Serial.begin(9600);
230 // while(!Serial) delay(1);
231
232 if (!trellis.begin()) {
233 Serial.println("Could not start trellis, check wiring?");
234 while(1) delay(1);
235 } else {
236 Serial.println("NeoPixel Trellis started");
237 }
238
239 //activate all keys and set callbacks
240 for(int i=0; i<NEO_TRELLIS_NUM_KEYS; i++){
241 trellis.activateKey(i, SEESAW_KEYPAD_EDGE_RISING);
242 trellis.activateKey(i, SEESAW_KEYPAD_EDGE_FALLING);
243 trellis.registerCallback(i, blink);
244 }
245
246 //do a little animation to show we're on
247 for (uint16_t i=0; i<trellis.pixels.numPixels(); i++) {
248 trellis.pixels.setPixelColor(i, Wheel(map(i, 0, trellis.pixels.numPixels(), 0, 255)));
249 trellis.pixels.show();
250 delay(50);
251 }
252 for (uint16_t i=0; i<trellis.pixels.numPixels(); i++) {
253 trellis.pixels.setPixelColor(i, 0x000000);
254 trellis.pixels.show();
255 delay(50);
256 }
257 }
258
259public:
260 static void TestMicrophone(){
261
262 }
263
264 static void TestButton(){
265
266 }
267
268 static void TestUART(){
269
270 }
271
272 static void TestHUD(){
273 bool test1 = TestI2CConnection(0x3C, "SSD1306", false);
274
275 if (test1) TestSSD1306(0x3C, false);
276 }
277
278 static void TestBoopSensor(){
279 bool test1 = TestI2CConnection(0x39, "APDS9960", false);
280
281 if (test1) TestAPDS9960(0x39, false);
282 //if (test1) TestAPDS9930(0x39, false);
283
284 }
285
286 static void TestNeoTrellis(){
288
289 for (uint16_t i = 0; i < 65536; i++){
290 trellis.read();
291 delay(20);
292 }
293 }
294
295 static void ScanDevices() {//timeout in milliseconds and threshold is minimum for detection (0 is far away, 255 is touching)
296 Wire.setClock(100000);//for longer range transmissions
297 Wire.begin();
298
299 Wire.setSDA(18);
300 Wire.setSCL(19);
301
302 uint8_t numDevices = 0;
303
304 for (uint8_t i = 0; i < 127; i++){
305 Wire.beginTransmission(i);
306
307 uint8_t error = Wire.endTransmission();
308
309 if(error == 0){// SSD1306 Found
310 Serial.print("Device on address: ");
311 Serial.println(i, HEX);
312 numDevices++;
313 }
314 }
315
316 Serial.print("Number of Devices Found: ");
317 Serial.println(numDevices);
318
319 Wire.end();
320 }
321
322
323 static void ResetI2CBus() {
324 Wire.end(); // Disable the I2C hardware
325 delay(10); // Wait a bit
326 Wire.begin(); // Re-enable the I2C hardware
327 }
328
329};
330
331
332
333Adafruit_NeoTrellis HardwareTest::trellis;
static uint32_t Wheel(byte WheelPos)
static bool TestI2CConnection(uint8_t address, String name, bool flip)
static void TestButton()
static void ScanDevices()
static void TestSSD1306(uint8_t address, bool flip)
static TrellisCallback blink(keyEvent evt)
static void TestHUD()
static void PrintAddress(uint8_t address, const String &text)
static void TestNeoTrellis()
static void TestUART()
static void TestTrellisHardware(bool flip)
static void ResetI2CBus()
static void TestMicrophone()
static void TestAPDS9930(uint8_t address, bool flip)
static void TestAPDS9960(uint8_t address, bool flip)
static Adafruit_NeoTrellis trellis
static void TestBoopSensor()
static void PrintAddressValue(uint8_t address, const String &text, int value)