Skip to content

Commit

Permalink
Add IOexpander (#90)
Browse files Browse the repository at this point in the history
* Add IO expander - still have I2C conflict bug

* Add IOexpander and Move Speaker to Main Timer

putting speaker updated on main timer (instead of it's own ISR) avoids I2C conflicts with other peripherals

I2C expander added and supported in variants

* Only init IOexpander if it exists

* Remove redundant lib files
  • Loading branch information
oxothnk423 authored Mar 8, 2025
1 parent fb98d43 commit 7bd18fe
Show file tree
Hide file tree
Showing 10 changed files with 234 additions and 418 deletions.
3 changes: 3 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ lib_deps =
; For reduced memory BLE Server Support
h2zero/NimBLE-Arduino@^2.2.3

; I2C IO-Expander
robtillaart/TCA9555@^0.4.1

[env:release]
extends = env
build_unflags = -D ARDUINO_USB_MODE
Expand Down
39 changes: 37 additions & 2 deletions src/variants/leaf_3_2_5/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,40 @@
#define SX1262_RESET 0 // Busy pin
#define SX1262_BUSY 16 // Busy pin

#define SPEAKER_VOLA -1 // Not Connected
#define SPEAKER_VOLB -1 // Not Connected

#define HAS_IO_EXPANDER 1 // this variant has an IO expander

#define SPEAKER_VOLA_IOEX 1 // this pin is on the IO Expander
#define SPEAKER_VOLA 2 // Pin 2 on the IO Expander

#define SPEAKER_VOLB_IOEX 1 // this pin is on the IO Expander
#define SPEAKER_VOLB 1 // Pin 1 on the IO Expander

#define IOEX_REG_CONFIG_PORT0 0b10000001 // P00 and P07 are inputs; others outputs
#define IOEX_REG_CONFIG_PORT1 0b10010000 // P10 and P13 are inputs; others outputs

/*
|IOEX Pin# |Function
|-----|--------|
| P00 | GPS_1PPS (input)
| P01 | SPKR_VOL_B
| P02 | SPKR_VOL_A
| P03 | EYESPI_SDcard_CS
| P04 | No Connection
| P05 | EYESPI_GPIO_2
| P06 | EYESPI_GPIO_1
| P07 | EYESPI_BUSY (input)
| P10 | EYESPI_INT (input)
| P11 | EYESPI_MEM_CS
| P12 | EYESPI_TS_CS
| P13 | IMU_INT (input)
| P14 | EX_14 *Expanded GPIO
| P15 | EX_15 *Expanded GPIO
| P16 | EX_16 *Expanded GPIO
| P17 | EX_17 *Expanded GPIO
*EYESPI is the multipurpose AdaFruit 18-pin connector ("Display 2")
*/

3 changes: 2 additions & 1 deletion src/vario/baro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ void baro_update(bool startNewCycle, bool doTemp) {
baro.climbRate = 0;
baro.climbRateAverage = 0;
baro.climbRateFiltered = 0;
speaker_updateVarioNote(baro.climbRateFiltered);
return;
}

Expand Down Expand Up @@ -548,7 +549,7 @@ void baro_updateClimb() {
(baro.climbRateAverage * (total_samples - 1) + baro.climbRateFiltered) / total_samples;

// finally, update the speaker sound based on the new climbrate
speaker_updateVarioNoteSample(baro.climbRateFiltered);
speaker_updateVarioNote(baro.climbRateFiltered);
}

// Device reading & data processing
Expand Down
56 changes: 32 additions & 24 deletions src/vario/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,25 @@
// #define DISABLE_BATTERY_SHUTDOWN

// **** D-Pad 5-button default configuration (for 3.2.3)
#ifndef BUTTON_PIN_CENTER
#define BUTTON_PIN_CENTER 2 // INPUT
#endif
#ifndef BUTTON_PIN_CENTER
#define BUTTON_PIN_CENTER 2 // INPUT
#endif

#ifndef BUTTON_PIN_LEFT
#define BUTTON_PIN_LEFT 3 // INPUT
#endif
#ifndef BUTTON_PIN_LEFT
#define BUTTON_PIN_LEFT 3 // INPUT
#endif

#ifndef BUTTON_PIN_DOWN
#define BUTTON_PIN_DOWN 4 // INPUT
#endif
#ifndef BUTTON_PIN_DOWN
#define BUTTON_PIN_DOWN 4 // INPUT
#endif

#ifndef BUTTON_PIN_UP
#define BUTTON_PIN_UP 5 // INPUT
#endif
#ifndef BUTTON_PIN_UP
#define BUTTON_PIN_UP 5 // INPUT
#endif

#ifndef BUTTON_PIN_RIGHT
#define BUTTON_PIN_RIGHT 6 // INPUT
#endif
#ifndef BUTTON_PIN_RIGHT
#define BUTTON_PIN_RIGHT 6 // INPUT
#endif

// ***** LoRa Configuration *****
// To flag support for FANET / LoRa.
Expand All @@ -61,14 +61,22 @@
// #define SX1262_BUSY

// **** Speaker *****
#ifndef SPEAKER_PIN
#define SPEAKER_PIN 14 // 7
#endif
#ifndef SPEAKER_PIN
#define SPEAKER_PIN 14 // 7
#endif

#ifndef SPEAKER_VOLA
#define SPEAKER_VOLA 15 // 8
#endif
#ifndef SPEAKER_VOLA
#define SPEAKER_VOLA 15 // 8
#endif

#ifndef SPEAKER_VOLB
#define SPEAKER_VOLB 16 // 9
#endif
#ifndef SPEAKER_VOLB
#define SPEAKER_VOLB 16 // 9
#endif

#ifndef SPEAKER_VOLA_IOEX
#define SPEAKER_VOLA_IOEX 0 // default not on IO expander
#endif

#ifndef SPEAKER_VOLB_IOEX
#define SPEAKER_VOLB_IOEX 0 // default not on IO expander
#endif
46 changes: 46 additions & 0 deletions src/vario/io_pins.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "io_pins.h"
#include <TCA9555.h>
#include <Wire.h>

// First try to load up any config from variants
#ifdef VARIANT
#include "variant.h"
#endif

// Pin configuration for the IO Expander (0=output 1=input)
#ifndef IOEX_REG_CONFIG_PORT0
#define IOEX_REG_CONFIG_PORT0 B11111111 // default all inputs
#endif

#ifndef IOEX_REG_CONFIG_PORT1
#define IOEX_REG_CONFIG_PORT1 B11111111 // default all inputs
#endif

// Create IO Expander
TCA9535 IOEX(IOEX_ADDR);

void ioexInit() {

// start IO Expander
bool result = IOEX.begin();
Serial.print("IOEX.begin succes: ");
Serial.println(result);

// configure IO expander pins
result = IOEX.pinMode8(0, IOEX_REG_CONFIG_PORT0);
Serial.print("IOEX.pinModeP0 succes: ");
Serial.println(result);
IOEX.pinMode8(1, IOEX_REG_CONFIG_PORT1);
Serial.print("IOEX.pinModeP1 succes: ");
Serial.println(result);
}

void ioexDigitalWrite(bool onIOEX, uint8_t pin, uint8_t value) {
// if we're writing a pin on the IO Expander
if (onIOEX) {
bool result = IOEX.write1(pin, value);
} else {
digitalWrite(pin, value);
}
}

14 changes: 14 additions & 0 deletions src/vario/io_pins.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once
#include <Arduino.h>

/* IO Expander
[datasheet](https://wmsc.lcsc.com/wmsc/upload/file/pdf/v2/lcsc/2304251416_XINLUDA-XL9535QF24_C5444301.pdf)
Chip is configured with I2C address: 0x20
*/

#define IOEX_ADDR 0x20

void ioexInit();

void ioexDigitalWrite(bool onIOEX, uint8_t pin, uint8_t value);
46 changes: 32 additions & 14 deletions src/vario/power.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "tempRH.h"
#include "ui/display.h"
#include "ui/displayFields.h"
#include "io_pins.h"

POWER power; // struct for battery-state and on-state variables

Expand Down Expand Up @@ -63,22 +64,34 @@ void power_init() {
void power_init_peripherals() {
Serial.print("init_peripherals: ");
Serial.println(power.onState);

// Init Peripheral Busses
wire_init();
Serial.println(" - Finished I2C Wire");
spi_init();
Serial.println(" - Finished SPI");

// initialize IO expander (needed for speaker in v3.2.5)
#ifdef HAS_IO_EXPANDER
ioexInit(); // initialize IO Expander
Serial.println(" - Finished IO Expander");
#endif

// initialize speaker to play sound (so user knows they can let go of the power button)
speaker_init();
Serial.println(" - Finished Speaker");

if (power.onState == POWER_ON) {
power_latch_on();
speaker_playSound(fx_enter);
}

// then initialize the rest of the devices
SDcard_init();
Serial.println(" - Finished SDcard");
gps_init();
Serial.println(" - Finished GPS");
wire_init();
Serial.println(" - Finished I2C Wire");
spi_init();
Serial.println(" - Finished SPI");

display_init();
Serial.println(" - Finished display");
baro_init();
Expand All @@ -105,7 +118,7 @@ void power_sleep_peripherals() {
Serial.println(" - Sleeping baro");
baro_sleep();
Serial.println(" - Sleeping speaker");
speaker_sleep();
speaker_mute();
Serial.println("Shut down speaker");
Serial.println(" - DONE");
}
Expand All @@ -119,7 +132,7 @@ void power_wake_peripherals() {
Serial.println(" - waking baro");
baro_wake();
Serial.println(" - waking speaker");
speaker_wake();
speaker_unMute();
Serial.println(" - DONE");
}

Expand All @@ -132,12 +145,18 @@ void power_switchToOnState() {

void power_shutdown() {
Serial.println("power_shutdown");

display_clear();
speaker_playSound(fx_exit);
baro_sleep(); // stop getting climbrate updates so we don't hear vario beeps while shutting down
display_off_splash();
auto shutdownTimeStamp = millis();
baro_sleep(); // stop getting climbrate updates so we don't hear vario beeps while shutting down

// play shutdown sound
speaker_playSound(fx_exit);

//loop until sound is done playing
while(onSpeakerTimer()) {
delay(10);
}

// saving logs and system data
if (flightTimer_isRunning()) {
Expand All @@ -147,11 +166,10 @@ void power_shutdown() {
// save any changed settings this session
settings_save();

// wait 3 seconds before shutting down to give user
// wait another 2.5 seconds before shutting down to give user
// a chance to see the shutdown screen
while (millis() - shutdownTimeStamp < 3000) {
delay(100);
}
delay(2500);


// finally, turn off devices
power_sleep_peripherals();
Expand Down
Loading

0 comments on commit 7bd18fe

Please sign in to comment.