Skip to content

Commit 30864e3

Browse files
committed
Add led_gizmo, Fix MF_I2C
Introduced Adafruit NeoPixel library for RGB LED support and updated LED handling in example projects to use color indications for armed/disarmed states. Refactored I2C chip scanner for better device identification and summary output. Enhanced HP203B barometer driver initialization and status handling. Updated board and config files to use new 'led_gizmo' parameter, and added configuration for ESP-FC2 board.
1 parent c6ee022 commit 30864e3

31 files changed

+5578
-145
lines changed

examples/01.Quadcopter/01.Quadcopter.ino

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,16 +149,16 @@ void imu_loop() {
149149
out_Mixer(); //Mixes PID outputs and sends command pulses to the motors, if mot.arm == true
150150
}
151151

152-
//========================================================================================================================//
153-
// IMU UPDATE LOOP FUNCTIONS - in same order as they are called from imu_loop() //
154-
//========================================================================================================================//
152+
//========================================================================================================================
153+
// IMU UPDATE LOOP FUNCTIONS - in same order as they are called from imu_loop()
154+
//========================================================================================================================
155155

156156
void led_Blink() {
157157
//Blink LED once per second, if LED blinks slower then the loop takes too much time, use CLI 'pimu' to investigate.
158-
//DISARMED: long off, short on, ARMED: long on, short off
158+
//DISARMED: green long off, short on, ARMED: red long on, short off
159159
uint32_t modulus = imu.update_cnt % imu.getSampleRate();
160-
if( modulus == 0) led.set(!out.armed); //start of pulse
161-
if( modulus == imu.getSampleRate() / 10) led.set(out.armed); //end of pulse
160+
if( modulus == 0) led.color( (out.armed ? 0 : 0x00ff00) ); //start of pulse - armed: off, disarmed: green
161+
if( modulus == imu.getSampleRate() / 10) led.color( (out.armed ? 0xff0000 : 0) ); //end of pulse - armed: red, disarmed: off
162162
}
163163

164164
//returns angle in range -180 to 180

examples/01.Quadcopter/madflight_config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ const char madflight_config[] = R""(
9595
//pin_rdr_echo -1
9696

9797
//--- LED ---
98-
//led_on LOW_IS_ON // options: LOW_IS_ON, HIGH_IS_ON
98+
//led_gizmo NONE // options: NONE, HIGH_IS_ON, LOW_IS_ON, RGB
9999
//pin_led -1
100100

101101
//--- AHR --- AHRS (keep MAHONY, unless you want to experiment)

examples/02.QuadcopterAdvanced/02.QuadcopterAdvanced.ino

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -192,16 +192,16 @@ void imu_loop() {
192192
//bbx.log_imu(); //uncomment for full speed black box logging of IMU data, but memory will fill up quickly...
193193
}
194194

195-
//========================================================================================================================//
196-
// IMU UPDATE LOOP FUNCTIONS - in same order as they are called from imu_loop() //
197-
//========================================================================================================================//
195+
//========================================================================================================================
196+
// IMU UPDATE LOOP FUNCTIONS - in same order as they are called from imu_loop()
197+
//========================================================================================================================
198198

199199
void led_Blink() {
200200
//Blink LED once per second, if LED blinks slower then the loop takes too much time, use CLI 'pimu' to investigate.
201-
//DISARMED: long off, short on, ARMED: long on, short off
201+
//DISARMED: green long off, short on, ARMED: red long on, short off
202202
uint32_t modulus = imu.update_cnt % imu.getSampleRate();
203-
if( modulus == 0) led.set(!out.armed); //start of pulse
204-
if( modulus == imu.getSampleRate() / 10) led.set(out.armed); //end of pulse
203+
if( modulus == 0) led.color( (out.armed ? 0 : 0x00ff00) ); //start of pulse - armed: off, disarmed: green
204+
if( modulus == imu.getSampleRate() / 10) led.color( (out.armed ? 0xff0000 : 0) ); //end of pulse - armed: red, disarmed: off
205205
}
206206

207207
//returns angle in range -180 to 180

examples/02.QuadcopterAdvanced/madflight_config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ const char madflight_config[] = R""(
9595
//pin_rdr_echo -1
9696

9797
//--- LED ---
98-
//led_on LOW_IS_ON // options: LOW_IS_ON, HIGH_IS_ON
98+
//led_gizmo NONE // options: NONE, HIGH_IS_ON, LOW_IS_ON, RGB
9999
//pin_led -1
100100

101101
//--- AHR --- AHRS (keep MAHONY, unless you want to experiment)

examples/03.Plane/03.Plane.ino

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,16 +206,16 @@ void imu_loop() {
206206
//bbx.log_imu(); //full speed black box logging of IMU data, memory fills up quickly...
207207
}
208208

209-
//========================================================================================================================//
210-
// IMU UPDATE LOOP FUNCTIONS - in same order as they are called from imu_loop() //
211-
//========================================================================================================================//
209+
//========================================================================================================================
210+
// IMU UPDATE LOOP FUNCTIONS - in same order as they are called from imu_loop()
211+
//========================================================================================================================
212212

213213
void led_Blink() {
214214
//Blink LED once per second, if LED blinks slower then the loop takes too much time, use CLI 'pimu' to investigate.
215-
//DISARMED: long off, short on, ARMED: long on, short off
215+
//DISARMED: green long off, short on, ARMED: red long on, short off
216216
uint32_t modulus = imu.update_cnt % imu.getSampleRate();
217-
if( modulus == 0) led.set(!out.armed); //start of pulse
218-
if( modulus == imu.getSampleRate() / 10) led.set(out.armed); //end of pulse
217+
if( modulus == 0) led.color( (out.armed ? 0 : 0x00ff00) ); //start of pulse - armed: off, disarmed: green
218+
if( modulus == imu.getSampleRate() / 10) led.color( (out.armed ? 0xff0000 : 0) ); //end of pulse - armed: red, disarmed: off
219219
}
220220

221221
void control_FBWA(bool zero_integrators) {

examples/03.Plane/madflight_config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ const char madflight_config[] = R""(
9595
//pin_rdr_echo -1
9696

9797
//--- LED ---
98-
//led_on LOW_IS_ON // options: LOW_IS_ON, HIGH_IS_ON
98+
//led_gizmo NONE // options: NONE, HIGH_IS_ON, LOW_IS_ON, RGB
9999
//pin_led -1
100100

101101
//--- AHR --- AHRS (keep MAHONY, unless you want to experiment)

extras/I2C_Chip_Scanner/I2C_Chip_Scanner.ino

Lines changed: 102 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -60,21 +60,37 @@ void setup() {
6060
}
6161

6262
void loop() {
63-
Serial.println("===================\nI2C Chip Scanner\n===================");
63+
uint8_t num_adr_found = 0;
64+
uint8_t adr_found[128];
65+
String adr_msg[128];
6466

65-
Serial.printf("I2C: Scanning ...\n");
66-
int num_adr_found = 0;
67+
Serial.printf("===========================\n");
68+
Serial.printf("I2C Chip Scanner 2025-08-08\n");
69+
Serial.printf("===========================\n");
70+
71+
//scan addresses to find devices
72+
Serial.printf("Step 1: Scanning Adressses ... ");
6773
for (uint8_t adr = 1; adr < 128; adr++) {
68-
i2c->beginTransmission(adr); // Begin I2C transmission Address (i)
69-
if (i2c->endTransmission() == 0) { // Receive 0 = success (ACK response)
70-
Serial.printf("I2C: Found address: 0x%02X (decimal %d)\n",adr,adr);
74+
if (i2c_TestAddress(adr)) {
75+
Serial.printf("0x%02X(%d) ",adr,adr);
76+
adr_found[num_adr_found] = adr;
7177
num_adr_found++;
72-
i2c_identify_chip(adr);
7378
}
74-
delay(10);
7579
}
76-
Serial.printf("I2C: Found %d device(s)\n", num_adr_found);
77-
delay(100);
80+
Serial.printf(" - Found %d devices\n", num_adr_found);
81+
82+
//try to identify the devices
83+
Serial.printf("Step 2: Identifying Devices ...\n");
84+
for (uint8_t i = 0; i < num_adr_found; i++) {
85+
i2c_identify_chip(adr_found[i], adr_msg[i]);
86+
}
87+
88+
Serial.printf("\nStep 3: Summary\n\n");
89+
for (uint8_t i = 0; i < num_adr_found; i++) {
90+
Serial.printf("address 0x%02X (decimal %3d) %s\n", adr_found[i], adr_found[i], adr_msg[i].c_str());
91+
}
92+
93+
delay(1000);
7894
}
7995

8096
struct test_struct{
@@ -101,25 +117,25 @@ test_struct tests[] = {
101117
{0x68, 0x69, 0x75, 1, 0x19, "MPU6886 6DOF motion"},
102118
{0x68, 0x69, 0x75, 1, 0x47, "ICM42688P 6DOF motion"},
103119
{0x68, 0x69, 0x75, 1, 0x68, "MPU6000, MPU6050, or MPU9150 6/9DOF motion"},
104-
{0x68, 0x69, 0x75, 1, 0x69, "FAKE MPU6050, got WHO_AM_I=0x69, real chip returns 0x68"},
120+
{0x68, 0x69, 0x75, 1, 0x69, "FAKE MPU6050, got WHO_AM_I=0x69, real chip returns 0x68"},
105121
{0x68, 0x69, 0x75, 1, 0x70, "MPU6500 6DOF motion"},
106122
{0x68, 0x69, 0x75, 1, 0x71, "MPU9250 9DOF motion"},
107123
{0x68, 0x69, 0x75, 1, 0x72, "FAKE MPU6050, got WHO_AM_I=0x72, real chip returns 0x68"},
108-
{0x68, 0x69, 0x75, 1, 0x73, "MPU9255 9DOF motion"},
124+
{0x68, 0x69, 0x75, 1, 0x73, "MPU9255 9DOF motion"},
109125
{0x68, 0x69, 0x75, 1, 0x74, "MPU9515 motion"},
110126
{0x68, 0x69, 0x75, 1, 0x75, "FAKE MPU9250, got WHO_AM_I=0x75, real chip returns 0x71"},
111127
{0x68, 0x69, 0x75, 1, 0x78, "FAKE MPU9250, got WHO_AM_I=0x78, real chip returns 0x71"},
112128
{0x68, 0x69, 0x75, 1, 0x98, "ICM20689 6DOF motion"},
113-
{0x76, 0x77, 0x8F, 1, 0x80, "HP203B pressure"}, //needs to be first 0x76 sensor, otherwise this sensor will lock up
114129
{0x76, 0x77, 0x00, 1, 0x50, "BMP388 pressure"},
115-
{0x76, 0x77, 0x00, 1, 0x60, "BMP390 pressure"},
130+
{0x76, 0x77, 0x00, 1, 0x60, "BMP390 pressure"},
116131
{0x76, 0x77, 0x0D, 1, 0x10, "DPS310, HP303B, or SPL06 pressure"},
132+
{0x76, 0x77, 0x8F, 1, 0x80, "HP203B pressure"},
117133
{0x76, 0x77, 0xD0, 1, 0x55, "BMP180 pressure"},
118134
{0x76, 0x77, 0xD0, 1, 0x56, "BMP280 pressure"},
119135
{0x76, 0x77, 0xD0, 1, 0x57, "BMP280 pressure"},
120136
{0x76, 0x77, 0xD0, 1, 0x58, "BMP280 pressure"},
121137
{0x76, 0x77, 0xD0, 1, 0x60, "BME280 pressure, temperature, humidity"},
122-
{0x76, 0x77, 0xD0, 1, 0x61, "BME680 pressure, temperature, humidity, gas sensor"},
138+
{0x76, 0x77, 0xD0, 1, 0x61, "BME680 pressure, temperature, humidity, gas"},
123139
{0x7C, 0x7C, 0x00, 1, 0x90, "QMC6309 magnetometer"},
124140

125141
{0,0,0,0,0,""} //end
@@ -139,11 +155,7 @@ adrtest_struct adrtests[] = {
139155
{0,0,""} //end
140156
};
141157

142-
void i2c_identify_chip(uint8_t adr) {
143-
bool found = false;
144-
145-
uint32_t tried[256] = {0};
146-
158+
void i2c_identify_chip(uint8_t adr, String &msg) {
147159
//try adr,reg,result match
148160
int i = 0;
149161
while(tests[i].adr1) {
@@ -155,58 +167,96 @@ void i2c_identify_chip(uint8_t adr) {
155167
uint32_t received = 0;
156168
if(adr1<=adr && adr<=adr2) {
157169
uint8_t data[4];
158-
i2c_ReadRegs(adr, reg, data, len);
170+
Serial.printf("try: %s --> read adr:0x%02X reg:0x%02X len:%d --> ", tests[i].descr.c_str(), adr, reg, len);
171+
i2c_ReadRegs(adr, reg, data, len, true);
159172
for(int i=0;i<len;i++) received = (received<<8) + data[i];
160-
tried[reg] = received;
173+
Serial.printf("received:0x%02X ", (int)received);
161174
if(received == expected) {
162-
Serial.printf(" MATCH: reg=0x%02X result=0x%02X -> %s\n", reg, (int)received, tests[i].descr.c_str());
163-
found = true;
164-
}
175+
msg = tests[i].descr;
176+
Serial.printf("--> %s\n",msg.c_str());
177+
return;
178+
}
179+
Serial.printf("\n");
165180
}
166181
i++;
167182
}
168183

169-
if(!found) {
170-
//show tries
171-
for(int i=0;i<256;i++) if(tried[i]!=0) {
172-
Serial.printf(" tried: reg=0x%02X result=0x%02X\n", i, (int)tried[i]);
173-
}
174-
175-
//try address only match
176-
int i = 0;
177-
while(adrtests[i].adr1) {
178-
int adr1 = adrtests[i].adr1;
179-
int adr2 = adrtests[i].adr2;
180-
if(adr1<=adr && adr<=adr2) {
181-
Serial.printf(" POTENTIAL MATCH (address 0x%02X match only) -> %s\n", adr, adrtests[i].descr);
182-
}
183-
i++;
184+
//try address only match
185+
i = 0;
186+
while(adrtests[i].adr1) {
187+
int adr1 = adrtests[i].adr1;
188+
int adr2 = adrtests[i].adr2;
189+
if(adr1<=adr && adr<=adr2) {
190+
msg = adrtests[i].descr + String(" - ADDRESS MATCH ONLY");
191+
return;
184192
}
193+
i++;
185194
}
195+
196+
msg = "UNKNOWN";
197+
}
198+
199+
bool i2c_TestAddress(uint8_t adr) {
200+
i2c->beginTransmission(adr); // Begin I2C transmission Address (i)
201+
return (i2c->endTransmission() == 0); // Receive 0 = success (ACK response)
186202
}
187203

188-
void WriteReg( uint8_t adr, uint8_t reg, uint8_t data ) {
204+
void i2c_WriteByte( uint8_t adr, uint8_t reg ) {
189205
i2c->beginTransmission(adr);
190-
i2c->write(reg);
191-
i2c->write(data);
206+
i2c->write(reg);
192207
i2c->endTransmission();
193208
}
194209

195-
unsigned int i2c_ReadReg( uint8_t adr, uint8_t reg ) {
196-
uint8_t data = 0;
197-
i2c_ReadRegs(adr, reg, &data, 1);
198-
return data;
210+
//returns -1 on fail
211+
int i2c_ReadByte( uint8_t adr ) {
212+
int rv = -1;
213+
uint8_t data;
214+
uint8_t bytesReceived = i2c->requestFrom(adr, 1);
215+
if(bytesReceived == 1) {
216+
i2c->readBytes(&data, 1);
217+
rv = data;
218+
}
219+
//i2c->endTransmission(); -- don't do this
220+
return rv;
199221
}
200222

201-
void i2c_ReadRegs( uint8_t adr, uint8_t reg, uint8_t *data, uint8_t n ) {
223+
void i2c_WriteReg( uint8_t adr, uint8_t reg, uint8_t data ) {
224+
i2c->beginTransmission(adr);
225+
i2c->write(reg);
226+
i2c->write(data);
227+
i2c->endTransmission();
228+
}
229+
230+
231+
232+
int i2c_ReadRegs( uint8_t adr, uint8_t reg, uint8_t *data, uint8_t n, bool stop ) {
202233
i2c->beginTransmission(adr);
203234
i2c->write(reg);
204-
i2c->endTransmission(false); //false = repeated start
205-
uint8_t bytesReceived = i2c->requestFrom(adr, n);
206-
if(bytesReceived == n) {
235+
i2c->endTransmission(stop); //false = repeated start, true = stop + start
236+
int bytesReceived = i2c->requestFrom(adr, n);
237+
if(bytesReceived > 0) {
207238
i2c->readBytes(data, bytesReceived);
208239
}
209-
i2c->endTransmission();
240+
//i2c->endTransmission(); -- don't do this
241+
return bytesReceived;
242+
}
243+
244+
int i2c_ReadRegs( uint8_t adr, uint8_t reg, uint8_t *data, uint8_t n) {
245+
return i2c_ReadRegs( adr, reg, data, n, false);
246+
}
247+
248+
//returns -1 on fail
249+
int i2c_ReadReg( uint8_t adr, uint8_t reg, bool stop ) {
250+
uint8_t data = 0;
251+
int bytesReceived = i2c_ReadRegs(adr, reg, &data, 1);
252+
if(bytesReceived == 1) {
253+
return data;
254+
}
255+
return -1;
256+
}
257+
258+
int i2c_ReadReg( uint8_t adr, uint8_t reg) {
259+
return i2c_ReadReg( adr, reg, false );
210260
}
211261

212262
void i2c_scan() {

0 commit comments

Comments
 (0)