Skip to content

Commit

Permalink
fixed compile errors, added SPI interface to Enchanti touch board
Browse files Browse the repository at this point in the history
  • Loading branch information
aburt2 committed Jan 13, 2024
1 parent 3b600e1 commit 4c75b8c
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 43 deletions.
6 changes: 3 additions & 3 deletions firmware/include/tstick-properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Define the ESP32 Board
/*
Set the amount of capacitive stripes to for the T-Stick, up to 120
*/
#define TSTICK_SIZE 30
#define TSTICK_SIZE 60

/*
Define I2C properties
Expand Down Expand Up @@ -80,7 +80,7 @@ Define Interrupt pins for sensors
/*
Enable second OSC address
*/
//#define OSC2
#define OSC2

/*
Task Rates
Expand All @@ -92,7 +92,7 @@ Task Rates
#define OSC_UPDATE_RATE 10 // ms (~77Hz)

// Sensors
#define TOUCH_UPDATE_RATE 10 // ms (takes 6ms to read 120 bytes over I2C)
#define TOUCH_UPDATE_RATE 10 // ms (takes 3ms to read 120 bytes over I2C)
#define IMU_UPDATE_RATE 2 // ms
#define ANG_UPDATE_RATE 2 // ms

Expand Down
4 changes: 2 additions & 2 deletions firmware/include/tstick-sensors.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ Sensor Boards
/*
Define the Touch Board
*/
#define touch_TRILL
//#define touch_TRILL
//#define touch_IDMIL
//#define touch_ENCHANTI
#define touch_ENCHANTI

/*
Define the IMU
Expand Down
108 changes: 82 additions & 26 deletions firmware/lib/Touch/Enchanti-touch/enchanti-touch.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "enchanti-touch.h"

// Initialise touch board,
void EnchantiTouch::initTouch(float num, int threshold, int mode) {
void EnchantiTouch::initTouch(float num, int threshold, int mode, int com_mode) {
// Clip number of touch boards to 2
if (num > 2) {
num = 2;
Expand All @@ -13,16 +13,42 @@ void EnchantiTouch::initTouch(float num, int threshold, int mode) {
num_boards = num;
noise_threshold = threshold;
boardMode = mode;
if (boardMode == RAW) {
baseReg = RAW_REG;
} else if (boardMode == DIFF) {
baseReg = DIFF_REG;
} else if (boardMode == BASELINE) {
baseReg = BASELINE_REG;
} else {
baseReg = DIFF_REG;
comMode = com_mode;

// Setup SPI if spi mode selected
if (comMode == SPI_MODE) {
// to use DMA buffer, use these methods to allocate buffer
spi_master_tx_buf = master.allocDMABuffer(ENCHANTI_BUFFERSIZE);
spi_master_rx_buf = master.allocDMABuffer(ENCHANTI_BUFFERSIZE);

// set the DMA buffer
for (uint32_t i = 0; i < ENCHANTI_BUFFERSIZE; i++) {
spi_master_tx_buf[i] = i & 0xFF;
}
memset(spi_master_rx_buf, 0, ENCHANTI_BUFFERSIZE);

// intialising the spi bus
master.setDataMode(SPI_MODE0);
master.setFrequency(spiClk);
master.setMaxTransferSize(ENCHANTI_BUFFERSIZE);
master.setDutyCyclePos(96);
// Start bus
master.begin();
}

// // Send configuration data to touch board
// // Send number of boards
// Wire.beginTransmission(main_i2c_addr);
// Wire.write(NUMBOARD_REG);
// Wire.write(int(num_boards));
// uint8_t last_status = Wire.endTransmission();

// // Set up touch mode
// Wire.beginTransmission(main_i2c_addr);
// Wire.write(TOUCHMODE_REG);
// Wire.write(mode);
// last_status = Wire.endTransmission();

// set touchsize
touchSize = floor(num_boards * ENCHANTI_BASETOUCHSIZE);
// enable sensor
Expand All @@ -31,23 +57,28 @@ void EnchantiTouch::initTouch(float num, int threshold, int mode) {
}

void EnchantiTouch::readTouch(){
// Read data from all 60 touch buttons
// Compute buffer length
int length = 0;
if (touchSize < ENCHANTI_BASETOUCHSIZE) {
// Each sensor needs 2 bytes == 120 Byte read
length = touchSize * 2;
} else {
length = ENCHANTI_BASETOUCHSIZE * 2;
// Read data from all touch buttons
if (comMode == I2C_MODE) {
// Compute buffer length
int length = 0;
if (touchSize < ENCHANTI_BASETOUCHSIZE) {
// Each sensor needs 2 bytes == 120 Byte read
length = touchSize * 2;
} else {
length = ENCHANTI_BASETOUCHSIZE * 2;
}

// Read touch data from I2C buffer
readI2CBuffer(main_i2c_addr, baseReg, length);

// Read auxillary touch board data
if (num_boards > 1) {
length = (touchSize - ENCHANTI_BASETOUCHSIZE) * 2;
readI2CBuffer(aux_i2c_addr, baseReg, length, ENCHANTI_BASETOUCHSIZE); // offset data index to not overwrite main touch board data
}
}

// Read touch data from I2C buffer
readBuffer(main_i2c_addr, baseReg, length);

// Read auxillary touch board data
if (num_boards > 1) {
length = (touchSize - ENCHANTI_BASETOUCHSIZE) * 2;
readBuffer(aux_i2c_addr, baseReg, length, ENCHANTI_BASETOUCHSIZE); // offset data index to not overwrite main touch board data
if (comMode == SPI_MODE) {
readSPIBuffer(ENCHANTI_BUFFERSIZE, 2);
}
}

Expand Down Expand Up @@ -81,7 +112,7 @@ void EnchantiTouch::cookData() {
}
}

void EnchantiTouch::readBuffer(uint8_t i2c_addr, uint8_t reg, uint8_t length, int offset)
void EnchantiTouch::readI2CBuffer(uint8_t i2c_addr, uint8_t reg, uint8_t length, int offset)
{
// prepare for data read
uint8_t loc = 0;
Expand All @@ -103,4 +134,29 @@ void EnchantiTouch::readBuffer(uint8_t i2c_addr, uint8_t reg, uint8_t length, in
}
++loc;
}
}

void EnchantiTouch::readSPIBuffer(uint16_t length, int offset)
{
// prepare for data read
uint8_t loc = 0;
uint16_t value = 0;

// Start transaction
master.transfer(NULL, spi_master_rx_buf, length);

// Process data
while (loc < (touchSize*2)) {
uint8_t lsb = spi_master_rx_buf[loc+offset];
++loc;
uint8_t msb = spi_master_rx_buf[loc+offset];
++loc;
value = uint16_t(msb << 8) | uint16_t(lsb);
if (data[int(loc/2)] != value) {
newData = 1;
}
if (value < 10000) { // spi occassionally throws junk ignore it
data[int(loc/2)] = value;
}
}
}
35 changes: 29 additions & 6 deletions firmware/lib/Touch/Enchanti-touch/enchanti-touch.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,18 @@

#include <Arduino.h>
#include "Wire.h"
#include <ESP32DMASPIMaster.h>
#include <iostream>

#define ENCHANTI_BASETOUCHSIZE 60
#define ENCHANTI_BUFFERSIZE 256
#define ENCHANTI_MAXBUFFERSIZE 1024

// SPI Pins
#define CS 10
#define MISO 13
#define MOSI 11
#define CLK 12

class EnchantiTouch
{
Expand All @@ -16,22 +25,35 @@ class EnchantiTouch
BASELINE = 2,
DIFF = 3
};
// Board modes
enum COMMS {
SPI_MODE = 1,
I2C_MODE = 2
};

// Register addresses for touch data
enum regAddr
{
RAW_REG = 4, // Register for raw data for button 1
DIFF_REG = 4, // Register of difference data for button 1 (raw - baseline)
BASELINE_REG = 4, // Register of baseline data for button 1
BOARDMODE_REG = 1, // Register for mode of the board
NUMBOARD_REG = 2, // Register for number of touch boards
TOUCHMODE_REG = 3, // Register for the touch mode
DATA_REG = 4, // Register of difference data for button 1 (raw - baseline)
};

// Include SPI
ESP32DMASPI::Master master;
const int spiClk = 4000000;
uint8_t* spi_master_tx_buf;
uint8_t* spi_master_rx_buf;

// Board Properties
int comMode = SPI_MODE;
int boardMode = RAW;
int newData = 0;
float num_boards = 1; // number of touch boards, set half numbers to indicate if using only the first touch circuit
uint8_t main_i2c_addr = 0x1E;
uint8_t aux_i2c_addr = 0x1F;
uint8_t baseReg = RAW_REG;
uint8_t baseReg = DATA_REG;

// Touch properties
int noise_threshold = 0;
Expand All @@ -56,11 +78,12 @@ class EnchantiTouch
bool running = true;

// Methods
void initTouch(float num=1, int threshold=0, int mode=DIFF);
void initTouch(float num=1, int threshold=0, int mode=DIFF, int com_mode=SPI_MODE);
void readTouch();
void cookData();

private:
void readBuffer(uint8_t i2c_addr, uint8_t reg, uint8_t length, int offset = 0);
void readI2CBuffer(uint8_t i2c_addr, uint8_t reg, uint8_t length, int offset = 0);
void readSPIBuffer(uint16_t length, int offset = 0);
};
#endif
2 changes: 1 addition & 1 deletion firmware/lib/Touch/touch.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
TrillTouch touch;
#endif
#ifdef touch_ENCHANTI
#include "Enchanti-touch/enchanti-touch."
#include "Enchanti-touch/enchanti-touch.h"
EnchantiTouch touch;
#endif
#ifdef touch_IDMIL
Expand Down
1 change: 1 addition & 0 deletions firmware/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ lib_deps =
https://github.com/BelaPlatform/Trill-Arduino.git
https://github.com/sparkfun/SparkFun_ICM-20948_ArduinoLibrary.git
https://github.com/CAP1Sup/Arduino_LSM9DS1.git
https://github.com/hideakitai/ESP32DMASPI.git
https://github.com/arkhipenko/TaskScheduler.git
build_flags =
-DCONFIG_ARDUINO_LOOP_STACK_SIZE=32768
Expand Down
28 changes: 23 additions & 5 deletions firmware/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ Include T-Stick properties
#include "tstick-properties.h"
#include "tstick-sensors.h"

// Includ SPI
#include "SPI.h"
#if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
#define VSPI FSPI
#endif
// Sensor libraries
#include "touch.h"

Expand Down Expand Up @@ -319,7 +324,7 @@ void updateGestures();

// Setup sensor tasks (task rates defined in tstick-properties.h)
Task updateIMU (TASK_IMMEDIATE, TASK_ONCE, &readIMU, &runnerSensors, false);
// Task updateTOUCH (TOUCH_UPDATE_RATE, TASK_FOREVER, &readTouch, &runnerSensors, false);
Task updateTOUCH (TOUCH_UPDATE_RATE, TASK_FOREVER, &readTouch, &runnerSensors, false);
Task updateANALOG (ANG_UPDATE_RATE, TASK_FOREVER, &readAnalog, &runnerSensors, false);
Task updateGesture (GESTURE_UPDATE_RATE, TASK_FOREVER, &updateGestures, &runnerSensors, false);
Task updateBattery (BATTERY_UPDATE_RATE, TASK_FOREVER, &readBattery, &runnerSensors, false);
Expand Down Expand Up @@ -520,6 +525,13 @@ void updateOSC1() {
mergeddiscretetouch[114], mergeddiscretetouch[115], mergeddiscretetouch[116], mergeddiscretetouch[117], mergeddiscretetouch[118], mergeddiscretetouch[119]);
}

#ifdef touch_ENCHANTI
oscNamespace.replace(oscNamespace.begin()+baseNamespace.size(),oscNamespace.end(), "instrument/touch/scantime");
lo_send(osc1, oscNamespace.c_str(), "i", touch.scantime);
oscNamespace.replace(oscNamespace.begin()+baseNamespace.size(),oscNamespace.end(), "instrument/touch/polltime");
lo_send(osc1, oscNamespace.c_str(), "i", touch.polltime);
#endif

oscNamespace.replace(oscNamespace.begin()+baseNamespace.size(),oscNamespace.end(), "instrument/touch/all");
lo_send(osc1, oscNamespace.c_str(), "f", gestures.touchAll);
oscNamespace.replace(oscNamespace.begin()+baseNamespace.size(),oscNamespace.end(), "instrument/touch/top");
Expand Down Expand Up @@ -773,6 +785,13 @@ void updateOSC2() {
mergeddiscretetouch[114], mergeddiscretetouch[115], mergeddiscretetouch[116], mergeddiscretetouch[117], mergeddiscretetouch[118], mergeddiscretetouch[119]);
}

#ifdef touch_ENCHANTI
oscNamespace.replace(oscNamespace.begin()+baseNamespace.size(),oscNamespace.end(), "instrument/touch/scantime");
lo_send(osc2, oscNamespace.c_str(), "i", touch.scantime);
oscNamespace.replace(oscNamespace.begin()+baseNamespace.size(),oscNamespace.end(), "instrument/touch/polltime");
lo_send(osc2, oscNamespace.c_str(), "i", touch.polltime);
#endif

oscNamespace.replace(oscNamespace.begin()+baseNamespace.size(),oscNamespace.end(), "instrument/touch/all");
lo_send(osc2, oscNamespace.c_str(), "f", gestures.touchAll);
oscNamespace.replace(oscNamespace.begin()+baseNamespace.size(),oscNamespace.end(), "instrument/touch/top");
Expand All @@ -782,7 +801,8 @@ void updateOSC2() {
oscNamespace.replace(oscNamespace.begin()+baseNamespace.size(),oscNamespace.end(), "instrument/touch/bottom");
lo_send(osc2, oscNamespace.c_str(), "f", gestures.touchBottom);

event.touchReady - false;
event.touchReady = false;
touch.newData = 0; // clear new data flag
}
if (event.mimu) {
oscNamespace.replace(oscNamespace.begin()+baseNamespace.size(),oscNamespace.end(), "raw/accl");
Expand Down Expand Up @@ -929,11 +949,9 @@ void readTouch() {
// Update event structure
if (touch.newData) {
event.touchReady = true;
touch.newData = 0;
} else {
event.touchReady = false;
}
}


void readAnalog() {
// Update button
Expand Down

0 comments on commit 4c75b8c

Please sign in to comment.