added code for esp32
parent
de0984ce6d
commit
c86c328937
|
@ -0,0 +1,180 @@
|
|||
#include "SLIPEncodedBluetoothSerial.h"
|
||||
#include "BluetoothSerial.h"
|
||||
|
||||
/*
|
||||
CONSTRUCTOR
|
||||
*/
|
||||
//instantiate with the tranmission layer
|
||||
//use BluetoothSerial
|
||||
SLIPEncodedBluetoothSerial::SLIPEncodedBluetoothSerial(BluetoothSerial &s){
|
||||
serial = &s;
|
||||
rstate = CHAR;
|
||||
}
|
||||
|
||||
static const uint8_t eot = 0300;
|
||||
static const uint8_t slipesc = 0333;
|
||||
static const uint8_t slipescend = 0334;
|
||||
static const uint8_t slipescesc = 0335;
|
||||
/*
|
||||
SERIAL METHODS
|
||||
*/
|
||||
bool SLIPEncodedBluetoothSerial::endofPacket()
|
||||
{
|
||||
if(rstate == SECONDEOT)
|
||||
{
|
||||
rstate = CHAR;
|
||||
return true;
|
||||
}
|
||||
if (rstate==FIRSTEOT)
|
||||
{
|
||||
if(serial->available())
|
||||
{
|
||||
uint8_t c =serial->peek();
|
||||
if(c==eot)
|
||||
{
|
||||
serial->read(); // throw it on the floor
|
||||
}
|
||||
}
|
||||
rstate = CHAR;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
int SLIPEncodedBluetoothSerial::available(){
|
||||
back:
|
||||
int cnt = serial->available();
|
||||
|
||||
if(cnt==0)
|
||||
return 0;
|
||||
if(rstate==CHAR)
|
||||
{
|
||||
uint8_t c =serial->peek();
|
||||
if(c==slipesc)
|
||||
{
|
||||
rstate = SLIPESC;
|
||||
serial->read(); // throw it on the floor
|
||||
goto back;
|
||||
}
|
||||
else if( c==eot)
|
||||
{
|
||||
rstate = FIRSTEOT;
|
||||
serial->read(); // throw it on the floor
|
||||
goto back;
|
||||
}
|
||||
return 1; // we may have more but this is the only sure bet
|
||||
}
|
||||
else if(rstate==SLIPESC)
|
||||
return 1;
|
||||
else if(rstate==FIRSTEOT)
|
||||
{
|
||||
if(serial->peek()==eot)
|
||||
{
|
||||
rstate = SECONDEOT;
|
||||
serial->read(); // throw it on the floor
|
||||
return 0;
|
||||
}
|
||||
rstate = CHAR;
|
||||
}else if (rstate==SECONDEOT) {
|
||||
rstate = CHAR;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
//reads a byte from the buffer
|
||||
int SLIPEncodedBluetoothSerial::read(){
|
||||
back:
|
||||
uint8_t c = serial->read();
|
||||
if(rstate==CHAR)
|
||||
{
|
||||
if(c==slipesc)
|
||||
{
|
||||
rstate=SLIPESC;
|
||||
goto back;
|
||||
}
|
||||
else if(c==eot){
|
||||
|
||||
return -1; // xxx this is an error
|
||||
}
|
||||
return c;
|
||||
}
|
||||
else
|
||||
if(rstate==SLIPESC)
|
||||
{
|
||||
rstate=CHAR;
|
||||
if(c==slipescend)
|
||||
return eot;
|
||||
else if(c==slipescesc)
|
||||
return slipesc;
|
||||
else {
|
||||
// insert some error code here
|
||||
return -1;
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
|
||||
// as close as we can get to correct behavior
|
||||
int SLIPEncodedBluetoothSerial::peek(){
|
||||
uint8_t c = serial->peek();
|
||||
if(rstate==SLIPESC)
|
||||
{
|
||||
if(c==slipescend)
|
||||
return eot;
|
||||
else if(c==slipescesc)
|
||||
return slipesc;
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
//the arduino and wiring libraries have different return types for the write function
|
||||
#if defined(WIRING) || defined(BOARD_DEFS_H)
|
||||
|
||||
//encode SLIP
|
||||
void SLIPEncodedBluetoothSerial::write(uint8_t b){
|
||||
if(b == eot){
|
||||
serial->write(slipesc);
|
||||
return serial->write(slipescend);
|
||||
} else if(b==slipesc) {
|
||||
serial->write(slipesc);
|
||||
return serial->write(slipescesc);
|
||||
} else {
|
||||
return serial->write(b);
|
||||
}
|
||||
}
|
||||
void SLIPEncodedBluetoothSerial::write(const uint8_t *buffer, size_t size) { while(size--) write(*buffer++); }
|
||||
#else
|
||||
//encode SLIP
|
||||
size_t SLIPEncodedBluetoothSerial::write(uint8_t b){
|
||||
if(b == eot){
|
||||
serial->write(slipesc);
|
||||
return serial->write(slipescend);
|
||||
} else if(b==slipesc) {
|
||||
serial->write(slipesc);
|
||||
return serial->write(slipescesc);
|
||||
} else {
|
||||
return serial->write(b);
|
||||
}
|
||||
}
|
||||
size_t SLIPEncodedBluetoothSerial::write(const uint8_t *buffer, size_t size) { size_t result=0; while(size--) result = write(*buffer++); return result; }
|
||||
|
||||
#endif
|
||||
|
||||
void SLIPEncodedBluetoothSerial::begin(String name){
|
||||
serial->begin(name);
|
||||
}
|
||||
//SLIP specific method which begins a transmitted packet
|
||||
void SLIPEncodedBluetoothSerial::beginPacket() { serial->write(eot); }
|
||||
|
||||
//signify the end of the packet with an EOT
|
||||
void SLIPEncodedBluetoothSerial::endPacket(){
|
||||
serial->write(eot);
|
||||
|
||||
}
|
||||
|
||||
void SLIPEncodedBluetoothSerial::flush(){
|
||||
serial->flush();
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
/*
|
||||
Extends the Serial class to encode SLIP over serial
|
||||
*/
|
||||
|
||||
#ifndef SLIPEncodedBluetoothSerial_h
|
||||
#define SLIPEncodedBluetoothSerial_h
|
||||
|
||||
|
||||
#include "Arduino.h"
|
||||
#include <Stream.h>
|
||||
#include "BluetoothSerial.h"
|
||||
|
||||
|
||||
class SLIPEncodedBluetoothSerial: public Stream{
|
||||
|
||||
private:
|
||||
enum erstate {CHAR, FIRSTEOT, SECONDEOT, SLIPESC } rstate;
|
||||
|
||||
//the serial port used
|
||||
BluetoothSerial * serial;
|
||||
|
||||
|
||||
public:
|
||||
|
||||
//the serial port used
|
||||
SLIPEncodedBluetoothSerial(BluetoothSerial & );
|
||||
|
||||
|
||||
int available();
|
||||
int read();
|
||||
int peek();
|
||||
void flush();
|
||||
|
||||
//same as Serial.begin
|
||||
void begin(String);
|
||||
|
||||
//SLIP specific method which begins a transmitted packet
|
||||
void beginPacket();
|
||||
|
||||
//SLIP specific method which ends a transmittedpacket
|
||||
void endPacket();
|
||||
// SLIP specific method which indicates that an EOT was received
|
||||
bool endofPacket();
|
||||
|
||||
|
||||
//the arduino and wiring libraries have different return types for the write function
|
||||
#if defined(WIRING) || defined(BOARD_DEFS_H)
|
||||
void write(uint8_t b);
|
||||
void write(const uint8_t *buffer, size_t size);
|
||||
|
||||
#else
|
||||
//overrides the Stream's write function to encode SLIP
|
||||
size_t write(uint8_t b);
|
||||
size_t write(const uint8_t *buffer, size_t size);
|
||||
|
||||
//using Print::write;
|
||||
#endif
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif
|
|
@ -0,0 +1,212 @@
|
|||
|
||||
// esp32 capacitive touch interface /////////////////////////////////////////////////
|
||||
// rob canning 2023 /////////////////////////////////////////////////
|
||||
/////////////////////////////////////////////////////////////////////
|
||||
|
||||
const int ledPin = 23;
|
||||
const int shiftLedPin = 25;
|
||||
const int loopLedPin = 26;
|
||||
|
||||
const int threshold = 30; // set the threshold
|
||||
int ledState = LOW; // the current state of the output pin
|
||||
int shiftLedState = HIGH; // the current state of the output pin
|
||||
int loopLedState = LOW; // the current state of the output pin
|
||||
|
||||
//#include <SPI.h> // Serial Peripheral Interface
|
||||
#include <Wire.h> // allows you to communicate with I2C / TWI devices
|
||||
|
||||
// Wi-Fi SETTINGS /////////////
|
||||
#include <WiFi.h>
|
||||
const char* ssid = "zavod rizoma_EXT";
|
||||
const char* password = "cermozise";
|
||||
|
||||
// BROADCAST TO ALL IP / port
|
||||
const IPAddress castIp = IPAddress(192,168,0,255); // 255 FOR BROADCAST
|
||||
const int port = 57120; // SUPERCOLLIDERS DEFAULT INCOMING OSC PORT
|
||||
bool connected = false;
|
||||
|
||||
#include <WiFiUdp.h>
|
||||
WiFiUDP udp;
|
||||
|
||||
// OSC and serial communications ////////////////////////////
|
||||
|
||||
#define SERIAL_OSC
|
||||
#define WIFI_OSC
|
||||
#define BT_OSC
|
||||
|
||||
#include <OSCBundle.h>
|
||||
#include <OSCMessage.h>
|
||||
#include <OSCBoards.h>
|
||||
|
||||
/* OSC MSG channels */
|
||||
OSCBundle bundle;
|
||||
|
||||
// SERIAL
|
||||
#ifdef BOARD_HAS_USB_SERIAL
|
||||
#include <SLIPEncodedUSBSerial.h>
|
||||
SLIPEncodedUSBSerial SLIPSerial( thisBoardsSerialUSB );
|
||||
#else
|
||||
#include <SLIPEncodedSerial.h>
|
||||
SLIPEncodedSerial SLIPSerial(Serial);
|
||||
#endif
|
||||
|
||||
// Bluetooth ////////////////////////////////
|
||||
|
||||
#ifdef BT_OSC
|
||||
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
|
||||
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
|
||||
#endif
|
||||
|
||||
#include <SLIPEncodedSerial.h>
|
||||
#include "BluetoothSerial.h"
|
||||
#include "SLIPEncodedBluetoothSerial.h"
|
||||
|
||||
BluetoothSerial SerialBT;
|
||||
SLIPEncodedBluetoothSerial SLIPBTSerial(SerialBT);
|
||||
#endif
|
||||
|
||||
//////////////////////////////////////////
|
||||
// CAPACITIVE TOUCH SETUP
|
||||
|
||||
byte keys[] = {32, 33, 27, 14, 12, 13, 4, 15};
|
||||
byte KEYLEN = 8;
|
||||
byte pressed[] = {0, 0, 0, 0, 0, 0, 0, 0};
|
||||
byte lastTouchState[] = {0, 0, 0, 0, 0, 0, 0, 0};
|
||||
byte touchState[] = {0, 0, 0, 0, 0, 0, 0, 0};
|
||||
byte lastDebounceTime[] = {0, 0, 0, 0, 0, 0, 0, 0};
|
||||
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
|
||||
int THRESH = 30; // capacitive noise threshold
|
||||
|
||||
///////////////////////////////////////////
|
||||
|
||||
void setup() {
|
||||
pinMode(ledPin, OUTPUT); //Define ledPin as output
|
||||
pinMode(shiftLedPin, OUTPUT); //Define ledPin as output
|
||||
pinMode(loopLedPin, OUTPUT); //Define ledPin as output
|
||||
digitalWrite(ledPin, ledState);
|
||||
digitalWrite(shiftLedPin, shiftLedState);
|
||||
digitalWrite(loopLedPin, loopLedState);
|
||||
Serial.begin(115200);
|
||||
WiFi.mode(WIFI_AP);
|
||||
connectWiFi();
|
||||
|
||||
const String macAddress = WiFi.macAddress(); ;
|
||||
|
||||
// I2C init
|
||||
Serial.println("Starting up I2C");
|
||||
Wire.begin();
|
||||
//Wire.setClock(400000); // 400kHz I2C clock. Comment this line if having compilation difficulties
|
||||
//
|
||||
Serial.println("starting I2C (Wire.begin)...");
|
||||
|
||||
#ifdef SERIAL_OSC
|
||||
SLIPSerial.begin(115200); // set this as high as you can reliably run on your platform
|
||||
#endif
|
||||
|
||||
#ifdef BT_OSC
|
||||
SerialBT.begin("grimore");
|
||||
#endif
|
||||
|
||||
// touch pads
|
||||
Serial.println("setting INPUT_PULLUP for capacitive touch pins... ");
|
||||
|
||||
for(int i = 0; i < KEYLEN; i++) {
|
||||
pinMode(keys[i], INPUT_PULLUP);
|
||||
}
|
||||
} // setup ends //////////////////////////////////////////////////
|
||||
|
||||
// MAIN LOOP /////////////////////////////////////////////////////////
|
||||
|
||||
void loop() {
|
||||
sendCapacitive();
|
||||
}
|
||||
|
||||
// FUNCTIONS /////////////////////////////////////////////
|
||||
|
||||
void connectWiFi(){
|
||||
// Connect to Wi-Fi network with SSID and password
|
||||
Serial.print("Connecting to ");
|
||||
Serial.println(ssid);
|
||||
WiFi.begin(ssid, password);
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
}
|
||||
|
||||
// Print local IP address and start web server
|
||||
// Serial.println("");
|
||||
Serial.println("WiFi connected.");
|
||||
Serial.println("IP address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
}
|
||||
|
||||
// detect touch events and send over OSC //////////////
|
||||
|
||||
void sendCapacitive(void){
|
||||
|
||||
for(int i = 0; i < KEYLEN; i++) {
|
||||
// read the state of the touch pad
|
||||
pressed[i] = touchRead(keys[i]);
|
||||
Serial.println(pressed[i]);
|
||||
|
||||
if (pressed[i] < threshold) {
|
||||
pressed[i] = HIGH;
|
||||
} else{
|
||||
pressed[i] = LOW;
|
||||
}
|
||||
|
||||
// If the pin is touched:
|
||||
if (pressed[i] != lastTouchState[i]) {
|
||||
// reset the debouncing timer
|
||||
lastDebounceTime[i] = millis();
|
||||
}
|
||||
|
||||
if ((millis() - lastDebounceTime[i]) > debounceDelay) {
|
||||
// whatever the reading is at, it's been there for longer than the debounce
|
||||
// delay, so take it as the actual current state:
|
||||
|
||||
// if the touch state has changed:
|
||||
if (pressed[i] != touchState[i]) {
|
||||
touchState[i] = pressed[i];
|
||||
// Serial.println("-------------------------------------- ");
|
||||
digitalWrite(ledPin, HIGH); // toggle the led on any touch
|
||||
|
||||
// only toggle the LED if the new touch state is high
|
||||
if (touchState[i] == HIGH) {
|
||||
ledState = !ledState;
|
||||
|
||||
OSCMessage msg("/touch");
|
||||
msg.add(WiFi.macAddress().c_str()); // mac address of sender
|
||||
msg.add((int32_t)i); // send which sensor was touched
|
||||
|
||||
#ifdef WIFI_OSC
|
||||
udp.beginPacket(castIp, port);
|
||||
//bundle.send(udp);
|
||||
msg.send(udp);
|
||||
udp.endPacket();
|
||||
#endif
|
||||
|
||||
String cap = "val for key num: " + String(i) + " === " + String(pressed[i]);
|
||||
Serial.println(""); Serial.println(cap);
|
||||
}
|
||||
|
||||
if (touchState[7] == HIGH) {
|
||||
digitalWrite(loopLedPin, loopLedState);
|
||||
loopLedState = !loopLedState;
|
||||
}
|
||||
if (touchState[8] == HIGH) {
|
||||
digitalWrite(shiftLedPin, shiftLedState);
|
||||
shiftLedState = !shiftLedState;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// set the LED:
|
||||
digitalWrite(ledPin, ledState);
|
||||
// save the reading. Next time through the loop, it'll be the lastTouchState:
|
||||
lastTouchState[i] = pressed[i];
|
||||
|
||||
}
|
||||
|
||||
} //////////////////
|
Loading…
Reference in New Issue