Refactored for teensy 4.0, xEvi hardware
- Switched to platformio, ino -> cpp - MPRLS for pressure sensor - Added basic ICM support - Removed widi, battery, other features not supported in xEvi - Removed legacy options/processing - Added LED strip support - Added encoder support - Reworked menu code to use encoders/be more flexible
This commit is contained in:
parent
c58c3f9e46
commit
01d193c9b3
92 changed files with 69119 additions and 73272 deletions
131
NuEVI/src/hardware.cpp
Normal file
131
NuEVI/src/hardware.cpp
Normal file
|
@ -0,0 +1,131 @@
|
|||
#include <Wire.h>
|
||||
#include "hardware.h"
|
||||
|
||||
#define ENCODER_OPTIMIZE_INTERRUPTS
|
||||
#include <Encoder.h>
|
||||
#include "menu.h"
|
||||
#include "config.h"
|
||||
#include "FilterOnePole.h" // for the breath signal low-pass filtering, from https://github.com/JonHub/Filters
|
||||
FilterOnePole breathFilter;
|
||||
FilterOnePole breathAltFilter;
|
||||
|
||||
Adafruit_MPR121 touchSensorKeys = Adafruit_MPR121();
|
||||
Adafruit_MPR121 touchSensorUtil = Adafruit_MPR121();
|
||||
Adafruit_MPRLS pressureSensorMain = Adafruit_MPRLS();
|
||||
Adafruit_MPRLS pressureSensorAlt = Adafruit_MPRLS();
|
||||
|
||||
byte drawingMemory[numLeds*3]; // 3 bytes per LED
|
||||
DMAMEM byte displayMemory[numLeds*12]; // 12 bytes per LED
|
||||
WS2812Serial ledStrip(numLeds, displayMemory, drawingMemory, ledStripPin, WS2812_GRB);
|
||||
Adafruit_ICM20948 icmSensor;
|
||||
Adafruit_Sensor *accelSensor;
|
||||
Encoder knobs[] = {
|
||||
Encoder(e1aPin, e1bPin),
|
||||
Encoder(e2aPin, e2bPin),
|
||||
Encoder(e3aPin, e3bPin),
|
||||
Encoder(e4aPin, e4bPin),
|
||||
};
|
||||
|
||||
void errorWait();
|
||||
|
||||
|
||||
void initHardware() {
|
||||
MainI2CBus.setClock(1000000);
|
||||
AuxI2CBus.setClock(1000000);
|
||||
pinMode(statusLedPin,OUTPUT); // Teensy onboard LED
|
||||
|
||||
// Buttons
|
||||
pinMode(b1Pin,INPUT_PULLUP);
|
||||
pinMode(b2Pin,INPUT_PULLUP);
|
||||
pinMode(b3Pin,INPUT_PULLUP);
|
||||
pinMode(b4Pin,INPUT_PULLUP);
|
||||
|
||||
breathFilter.setFilter(LOWPASS, FILTER_FREQ, 0.0); // create a one pole (RC) lowpass filter
|
||||
breathAltFilter.setFilter(LOWPASS, FILTER_FREQ, 0.0); // create a one pole (RC) lowpass filter
|
||||
|
||||
ledStrip.begin();
|
||||
|
||||
if (!touchSensorKeys.begin(KeysI2CAddr, &MainI2CBus)) {
|
||||
displayError("Keys touch error");
|
||||
errorWait();
|
||||
}
|
||||
|
||||
if (!touchSensorUtil.begin(UtilI2CAddr, &MainI2CBus)) {
|
||||
displayError("Roller/Util touch error");
|
||||
errorWait();
|
||||
}
|
||||
|
||||
if (!pressureSensorMain.begin(MPRLS_DEFAULT_ADDR, &MainI2CBus)) {
|
||||
displayError("Main pressure sensor error");
|
||||
errorWait();
|
||||
}
|
||||
|
||||
if (!pressureSensorAlt.begin(MPRLS_DEFAULT_ADDR, &AuxI2CBus)) {
|
||||
displayError("Alt pressure sensor error");
|
||||
errorWait();
|
||||
}
|
||||
|
||||
if (!icmSensor.begin_I2C(ICM20948_I2CADDR_DEFAULT, &MainI2CBus)) {
|
||||
displayError("ICM sensor error");
|
||||
errorWait();
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Return true if the given button bitmask is pressed
|
||||
*/
|
||||
bool checkButtonState(uint8_t mask) {
|
||||
return buttonState() & mask;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the state of the switches (note that they are active low, so we invert the values)
|
||||
*/
|
||||
uint8_t buttonState() {
|
||||
return 0x0f ^(digitalRead(b1Pin)
|
||||
| (digitalRead(b2Pin) << 1)
|
||||
| (digitalRead(b3Pin) << 2)
|
||||
| (digitalRead(b4Pin) << 3));
|
||||
}
|
||||
|
||||
|
||||
void errorWait() {
|
||||
while (1) {
|
||||
if (!digitalRead(b1Pin)) {
|
||||
_reboot_Teensyduino_(); // reboot to program mode if b1 pressed
|
||||
}
|
||||
|
||||
if (!digitalRead(b4Pin)) {
|
||||
break; // Continue if b4 pressed
|
||||
}
|
||||
|
||||
delay(10);
|
||||
}
|
||||
}
|
||||
|
||||
int readKnob(uint8_t n) {
|
||||
return knobs[n].readAndReset();
|
||||
}
|
||||
|
||||
int readTouchKey(uint8_t n) {
|
||||
return CAP_SENS_ABSOLUTE_MAX - touchSensorKeys.filteredData(n);
|
||||
}
|
||||
int readTouchUtil(uint8_t n) {
|
||||
return CAP_SENS_ABSOLUTE_MAX - touchSensorUtil.filteredData(n);
|
||||
}
|
||||
|
||||
uint16_t keysTouched() {
|
||||
return touchSensorKeys.touched();
|
||||
}
|
||||
|
||||
uint16_t utilTouched() {
|
||||
return touchSensorKeys.touched();
|
||||
}
|
||||
|
||||
int readPressure() {
|
||||
return breathFilter.input(pressureSensorMain.readPressure()) * PRESSURE_SENS_MULTIPLIER;
|
||||
}
|
||||
|
||||
int readAltPressure() {
|
||||
return breathAltFilter.input(pressureSensorAlt.readPressure()) * PRESSURE_SENS_MULTIPLIER;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue