Skip to content

Commit

Permalink
Initial ESP32 support #73
Browse files Browse the repository at this point in the history
  • Loading branch information
laurb9 committed Sep 27, 2020
1 parent 2d02707 commit bc85cca
Show file tree
Hide file tree
Showing 11 changed files with 283 additions and 16 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/arduino.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ jobs:
matrix:
board:
- adafruit:samd:adafruit_feather_m0
- esp32:esp32:esp32

runs-on: ubuntu-latest
steps:
Expand All @@ -27,7 +28,7 @@ jobs:
- name: Install arduino-cli
run: |
make setup
pip install pyserial
- name: Build
run: |
make setup
make all TARGET=${{ matrix.board }}
1 change: 1 addition & 0 deletions .github/workflows/platformio.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jobs:
fail-fast: false
matrix:
board:
- esp32dev
- adafruit_feather_m0
- teensylc

Expand Down
26 changes: 13 additions & 13 deletions examples/PanoController/PanoController.ino
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,16 @@
#include "camera.h"
#include "mpu.h"
#include "battery.h"
#include "bluetooth.h"
#include "gcode.h"

#if defined(BLUEFRUIT_SPI_CS)
#include "ble_bluefruit_spi.h"
static Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST);
static Bluetooth bluetooth(ble);
#elif defined(ESP32)
#include "ble_esp32.h"
static Bluetooth ble;
#endif

static Battery battery(BATTERY, BATT_R1, BATT_R2, VCC);
static Camera camera(CAMERA_FOCUS, CAMERA_SHUTTER);
Expand All @@ -38,24 +43,20 @@ void setup() {
digitalWrite(MPU_VCC, HIGH);

Serial.println("Configuring stepper drivers");

horiz_motor.begin(MOTOR_RPM, MICROSTEPS);
vert_motor.begin(MOTOR_RPM/2, MICROSTEPS);
motors.disable(); // turn off motors at startup
Serial.print("Horiz RPM="); Serial.println(horiz_motor.getRPM());
Serial.print("Vert RPM="); Serial.println(vert_motor.getRPM());

#if !defined(ESP32)
Serial.println("Configuring Bluefruit LE");
ble.begin(true);
ble.echo(false); // disable command echo
ble.factoryReset();
ble.info();
ble.verbose(false); // turn off debug info

Serial.println("Initializing BLE App Communication");
bluetooth.begin();

ble.sendCommandCheckOK("AT+BLEBATTEN=1"); // enable battery service
// ble.sendCommandCheckOK("AT+BLEPOWERLEVEL=0"); // can be used to change transmit power
#else
Serial.println("Starting Bluetooth");
ble.begin("PanoController");
#endif

Serial.print("Checking battery voltage... ");
battery.begin();
Expand All @@ -71,7 +72,6 @@ void setup() {
gcode.setMaxAccel(MOTOR_ACCEL, MOTOR_DECEL);

Serial.println("System ready.");

while (!ble.isConnected()){
delay(1000);
}
Expand All @@ -83,7 +83,7 @@ void loop() {
static int len;

if (ble.available()){
len = ble.readline(buffer, GCODE_BUF_SIZE);
len = ble.readBytesUntil('\n', buffer, GCODE_BUF_SIZE);
*(buffer+len) = '\0';

Serial.print("BLE> ");
Expand Down
3 changes: 3 additions & 0 deletions examples/PanoController/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
#if defined(ARDUINO_SAMD_FEATHER_M0) || defined(ADAFRUIT_FEATHER_M0)
#include "config_feather_m0.h"

#elif defined(ESP32)
#include "config_esp32.h"

#elif defined(__arm__) && defined(CORE_TEENSY)
#include "config_teensy.h"

Expand Down
39 changes: 39 additions & 0 deletions examples/PanoController/config_esp32.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Pano Controller Configuration File for ESP32 board
* https://github.com/espressif/arduino-esp32/blob/master/docs/esp32_pinmap.png
* https://desire.giesecke.tk/index.php/2018/07/06/reserved-gpios/
*/
#include <Arduino.h>

/* SPI (for display)
SPI2 and SPI3 are general purpose SPI controllers, sometimes referred to as HSPI and VSPI
SDA = IO 23
SCLK = IO 18
D/C = IO 21
RST = IO 22
CS = IO 5
*/

// Camera shutter controls
#define CAMERA_FOCUS GPIO_NUM_16
#define CAMERA_SHUTTER GPIO_NUM_17

// Battery measurement pin R1/R2
#define BATTERY GPIO_NUM_34 //34 # ADC1 CH0

// MPU (accel/gyro)
// GPIO_NUM_21 I2C SDA
// GPIO_NUM_22 I2C SCL
#define MPU_INT GPIO_NUM_2
#define MPU_VCC GPIO_NUM_4

// Future devices
//#define COMPASS_DRDY xx

// Stepper drivers control
#define DIR GPIO_NUM_27
#define VERT_STEP GPIO_NUM_25
#define HORIZ_STEP GPIO_NUM_26

// this should be hooked up to nENABLE on both drivers
#define nENABLE GPIO_NUM_14
16 changes: 15 additions & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ description = PanoController-Firmware
src_dir = examples/PanoController
lib_dir = .
default_envs =
adafruit_feather_m0
esp32dev

[env]
framework = arduino
Expand Down Expand Up @@ -42,3 +42,17 @@ board = teensylc
lib_deps =
${env.lib_deps}
featherfly/SoftwareSerial@^1.0

[env:esp32dev]
platform = espressif32
board = esp32dev
#board = esp-wrover-kit
board_build.f_cpu = 80000000L
lib_deps =
${env.lib_deps}
plerup/EspSoftwareSerial @ ^6.8.5
build_type = debug
build_flags =
${env.build_flags}
# -D CORE_DEBUG_LEVEL=5
# -D CORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG
11 changes: 10 additions & 1 deletion src/bluetooth.cpp → src/ble_bluefruit_spi.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#if !defined(ESP32)
/*
* App Communication over Bluetooth LE Serial
*
Expand All @@ -7,7 +8,7 @@
* A copy of this license has been included with this distribution in the file LICENSE.
*/

#include "bluetooth.h"
#include "ble_bluefruit_spi.h"

#define UART_BUFFER_SIZE BLE_BUFSIZE

Expand All @@ -16,6 +17,13 @@ Bluetooth::Bluetooth(Adafruit_BluefruitLE_SPI& ble)
{}

void Bluetooth::begin(){
ble.begin(true);
ble.echo(false); // disable command echo
ble.factoryReset();
ble.info();
ble.verbose(false); // turn off debug info
ble.sendCommandCheckOK("AT+BLEBATTEN=1"); // enable battery service
// ble.sendCommandCheckOK("AT+BLEPOWERLEVEL=0"); // can be used to change transmit power
ble.sendCommandCheckOK("AT+BAUDRATE=921600");
// LED Activity command is only supported from 0.6.6: MODE, BLEUART
ble.sendCommandCheckOK("AT+HWMODELED=BLEUART");
Expand Down Expand Up @@ -48,3 +56,4 @@ void Bluetooth::poll(uint32_t timeout){
} while (p == eob); // we filled the buffer so there may be more to read
}
}
#endif // !defined(ESP32)
2 changes: 2 additions & 0 deletions src/bluetooth.h → src/ble_bluefruit_spi.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#if !defined(ESP32)
/*
* App Communication over Bluetooth LE Serial
*
Expand All @@ -23,3 +24,4 @@ class Bluetooth {
};

#endif /* BLUETOOTH_H_ */
#endif // !defined(ESP32)
109 changes: 109 additions & 0 deletions src/ble_esp32.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#if defined(ESP32)

#include "ble_esp32.h"

// Nordic UART uuids
#define UART_SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E"
#define UART_RX_CHAR_UUID "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
#define UART_TX_CHAR_UUID "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"
#define PANO_SERVICE_UUID (uint16_t)2017

// How many bytes we can send at once
#define BLOCK_SIZE 128

void Bluetooth::begin(const char *name) {
BLEDevice::init(name);

pServer = BLEDevice::createServer();
pServer->setCallbacks(&serverCallbacks);

pServer->createService(PANO_SERVICE_UUID)->start();

BLEService *pService = pServer->createService(UART_SERVICE_UUID);
rx = pService->createCharacteristic(UART_RX_CHAR_UUID, BLECharacteristic::PROPERTY_WRITE);
rx->setCallbacks(&rxCallbacks);

tx = pService->createCharacteristic(UART_TX_CHAR_UUID, BLECharacteristic::PROPERTY_NOTIFY);
tx->addDescriptor(new BLE2902()); // 0x2902: org.bluetooth.descriptor.gatt.client_characteristic_configuration

pService->start();

pServer->startAdvertising();
}

bool Bluetooth::isConnected(void) {
return ((ServerCallbacks *)&serverCallbacks)->isConnected(); // FIXME: temporary
}

void RXCallbacks::onWrite(BLECharacteristic *pCharacteristic) {
std::string val = pCharacteristic->getValue();
int len = val.length();
if (len > 0 && abs(writeAt - readAt) < len ) {
if (writeAt + len < buf + FIFO_SIZE) { // no wrap
std::copy(val.begin(), val.end(), writeAt);
writeAt += len;
} else {
int wrapLen = buf + FIFO_SIZE - writeAt;
std::copy(val.begin(), val.begin() + wrapLen, writeAt);
std::copy(val.begin() + wrapLen, val.end(), buf);
writeAt = buf + len - wrapLen;
}
}
};

int RXCallbacks::read(void) {
return available() ? (int)(*readAt++) : EOF;
}

int RXCallbacks::peek(void) {
return available() ? (int)(*readAt) : EOF;
}

void RXCallbacks::flush(void) {
writeAt = readAt = buf;
}

/*
* Implement Stream interface
*/
size_t Bluetooth::write(uint8_t c) {
if (isConnected()) {
tx->setValue(&c, 1);
tx->notify();
ets_delay_us(10000); // bluetooth stack will go into congestion, if too many packets are sent
return 1;
}
return 0;
};

size_t Bluetooth::write(const uint8_t *buf, size_t size) {
size_t remain = size;
while (isConnected() && remain > 0) {
if (remain > BLOCK_SIZE){
tx->setValue((uint8_t*)buf, BLOCK_SIZE);
remain -= BLOCK_SIZE;
buf += BLOCK_SIZE;
} else {
tx->setValue((uint8_t*)buf, remain);
remain = 0;
}
tx->notify();
ets_delay_us(10000);
}
return size - remain;
}

int Bluetooth::available(void) {
return rxCallbacks.available();
};
int Bluetooth::read(void) {
return rxCallbacks.read();
};
int Bluetooth::peek(void) {
return rxCallbacks.peek();
};
void Bluetooth::flush(void) {
rxCallbacks.flush();
};

#endif // defined(ESP32)
Loading

0 comments on commit bc85cca

Please sign in to comment.