Skip to content

Commit

Permalink
Added support for ESP32 (#119)
Browse files Browse the repository at this point in the history
Thanks, @ZwoCa !
  • Loading branch information
ZwoCa authored Jan 8, 2021
1 parent e6ab14a commit 3bb4084
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 7 deletions.
10 changes: 9 additions & 1 deletion Microcontroller/Moppy2-Arduino/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@
; lib_deps=
; FastLED ; For experimental lighting effects
; https://github.com/sstaub/Ticker ; For experimental lighting effects (because lights are too slow for Timer)

; upload_speed = 115200
; monitor_speed = 115200

; [env:esp32]
; platform = espressif32
; board = esp32dev
; lib_deps=
; FastLED ; For experimental lighting effects
; https://github.com/sstaub/Ticker ; For experimental lighting effects (because lights are too slow for Timer)
; upload_speed = 115200
; monitor_speed = 115200

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ Additionally, the ICACHE_RAM_ATTR helps avoid crashes with WiFi libraries, but m
#pragma GCC optimize("Ofast") // Required to unroll this loop, but useful to try to keep this speedy
#ifdef ARDUINO_ARCH_ESP8266
void ICACHE_RAM_ATTR FloppyDrives::tick() {
#elif ARDUINO_ARCH_ESP32
void IRAM_ATTR FloppyDrives::tick() {
#else
void FloppyDrives::tick() {
#endif
Expand All @@ -191,6 +193,8 @@ void FloppyDrives::tick() {

#ifdef ARDUINO_ARCH_ESP8266
void ICACHE_RAM_ATTR FloppyDrives::togglePin(byte driveNum, byte pin, byte direction_pin) {
#elif ARDUINO_ARCH_ESP32
void IRAM_ATTR FloppyDrives::togglePin(byte driveNum, byte pin, byte direction_pin) {
#else
void FloppyDrives::togglePin(byte driveNum, byte pin, byte direction_pin) {
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
#ifdef ARDUINO_ARCH_AVR
#define TIMER_RESOLUTION 40
#elif ARDUINO_ARCH_ESP8266
#elif ARDUINO_ARCH_ESP8266 || ARDUINO_ARCH_ESP32
#define TIMER_RESOLUTION 20 // Higher resolution for the faster processor
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#ifdef ARDUINO_ARCH_AVR
#include <TimerOne.h>
#elif ARDUINO_ARCH_ESP8266
#elif ARDUINO_ARCH_ESP8266 || ARDUINO_ARCH_ESP32
#include <Arduino.h>
#endif

Expand All @@ -15,5 +15,10 @@ void MoppyTimer::initialize(unsigned long microseconds, void (*isr)()) {
timer1_attachInterrupt(isr);
timer1_enable(TIM_DIV16, TIM_EDGE, TIM_LOOP);
timer1_write(5 * microseconds);
#elif ARDUINO_ARCH_ESP32
hw_timer_t *timer = timerBegin(0, 80, true);
timerAttachInterrupt(timer, isr, true);
timerAlarmWrite(timer, microseconds, true);
timerAlarmEnable(timer);
#endif
}
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ Additionally, the ICACHE_RAM_ATTR helps avoid crashes with WiFi libraries, but m
#pragma GCC optimize("Ofast") // Required to unroll this loop, but useful to try to keep this speedy
#ifdef ARDUINO_ARCH_ESP8266
void ICACHE_RAM_ATTR ShiftedFloppyDrives::tick() {
#elif ARDUINO_ARCH_ESP32
void IRAM_ATTR ShiftedFloppyDrives::tick() {
#else
void ShiftedFloppyDrives::tick() {
#endif
Expand All @@ -169,6 +171,8 @@ void ShiftedFloppyDrives::tick() {

#ifdef ARDUINO_ARCH_ESP8266
void ICACHE_RAM_ATTR ShiftedFloppyDrives::togglePin(byte driveIndex) {
#elif ARDUINO_ARCH_ESP32
void IRAM_ATTR ShiftedFloppyDrives::togglePin(byte driveIndex) {
#else
void ShiftedFloppyDrives::togglePin(byte driveIndex) {
#endif
Expand All @@ -194,6 +198,8 @@ void ShiftedFloppyDrives::togglePin(byte driveIndex) {

#ifdef ARDUINO_ARCH_ESP8266
void ICACHE_RAM_ATTR ShiftedFloppyDrives::shiftBits() {
#elif ARDUINO_ARCH_ESP32
void IRAM_ATTR ShiftedFloppyDrives::shiftBits() {
#else
void ShiftedFloppyDrives::shiftBits() {
#endif
Expand Down
9 changes: 7 additions & 2 deletions Microcontroller/Moppy2-Arduino/src/MoppyNetworks/MoppyUDP.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "MoppyUDP.h"

#ifdef ARDUINO_ARCH_ESP8266 // For now, this will only work with ESP8266
#if !defined ARDUINO_ARCH_ESP8266 && !defined ARDUINO_ARCH_ESP32
#else

WiFiUDP UDP;

Expand Down Expand Up @@ -31,7 +32,11 @@ bool MoppyUDP::startUDP() {
Serial.println("");
Serial.println("Connecting to UDP");

#ifdef ARDUINO_ARCH_ESP8266
if (UDP.beginMulticast(WiFi.localIP(), IPAddress(239, 2, 2, 7), MOPPY_UDP_PORT) == 1) {
#elif ARDUINO_ARCH_ESP32
if (UDP.beginMulticast(IPAddress(239, 2, 2, 7), MOPPY_UDP_PORT) == 1) {
#endif
Serial.println("Connection successful");
connected = true;
} else {
Expand Down Expand Up @@ -139,4 +144,4 @@ void MoppyUDP::sendPong() {
UDP.write(pongBytes, sizeof(pongBytes));
UDP.endPacket();
}
#endif /* ARDUINO_ARCH_ESP8266 */
#endif /* ARDUINO_ARCH_ESP8266 or ARDUINO_ARCH_ESP32 */
10 changes: 8 additions & 2 deletions Microcontroller/Moppy2-Arduino/src/MoppyNetworks/MoppyUDP.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
* MoppyUDP.h
*
*/
#ifdef ARDUINO_ARCH_ESP8266 // For now, this will only work with ESP8266
#if !defined ARDUINO_ARCH_ESP8266 && !defined ARDUINO_ARCH_ESP32
// For now, this will only work with ESP8266 or ESP32
#else
#ifndef SRC_MOPPYNETWORKS_MOPPYUDP_H_
#define SRC_MOPPYNETWORKS_MOPPYUDP_H_

Expand All @@ -11,7 +13,11 @@
#include "Arduino.h"
#include "MoppyNetwork.h"
#include <ArduinoOTA.h>
#ifdef ARDUINO_ARCH_ESP8266
#include <ESP8266WiFi.h>
#elif ARDUINO_ARCH_ESP32
#include <WiFi.h>
#endif
#include <ESPAsyncWebServer.h> //Local WebServer used to serve the configuration portal
#include <ESPAsyncWiFiManager.h> // https://github.com/alanswx/ESPAsyncWiFiManager WiFi Configuration Magic
#include <WiFiUdp.h>
Expand All @@ -38,4 +44,4 @@ class MoppyUDP {
};

#endif /* SRC_MOPPYNETWORKS_MOPPYUDP_H_ */
#endif /* ARDUINO_ARCH_ESP8266 */
#endif /* ARDUINO_ARCH_ESP8266 or ARDUINO_ARCH_ESP32 */

0 comments on commit 3bb4084

Please sign in to comment.