Skip to content

Commit 22d1722

Browse files
committed
Make code work for esp8266
1 parent 4738b61 commit 22d1722

File tree

3 files changed

+240
-45
lines changed

3 files changed

+240
-45
lines changed

platformio.ini

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
; https://docs.slimevr.dev/firmware/configuring-project.html#1-configuring-platformioini
1212
; ================================================
1313

14+
[platformio]
15+
build_cache_dir = cache
16+
1417
[env]
1518
lib_deps=
1619
https://github.com/SlimeVR/CmdParser.git
@@ -57,13 +60,12 @@ build_unflags = -Os -std=gnu++11 -std=gnu++17
5760

5861
; Settings for different boards
5962

60-
;[env:esp12e]
61-
;platform = espressif8266 @ 4.2.1
62-
;board = esp12e
63+
[env:esp12e]
64+
platform = espressif8266 @ 4.2.1
65+
board = esp12e
6366
; Comment out this line below if you have any trouble uploading the firmware
6467
; and if it has a CP2102 on it (a square chip next to the usb port): change to 3000000 (3 million) for even faster upload speed
65-
;upload_speed = 921600
66-
;board_build.f_cpu = 160000000L
68+
upload_speed = 921600
6769

6870
; Uncomment below if you want to build for ESP-01
6971
;[env:esp01_1m]
@@ -98,14 +100,22 @@ build_unflags = -Os -std=gnu++11 -std=gnu++17
98100
; -DARDUINO_USB_MODE=1
99101
; -DARDUINO_USB_CDC_ON_BOOT=1
100102

101-
[env:esp32c3]
102-
platform = espressif32 @ 6.7.0
103-
platform_packages =
104-
framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32.git#3.0.1
105-
framework-arduinoespressif32-libs @ https://github.com/espressif/arduino-esp32/releases/download/3.0.1/esp32-arduino-libs-3.0.1.zip
106-
build_flags =
107-
${env.build_flags}
108-
-DESP32C3
109-
-DARDUINO_USB_MODE=1
110-
-DARDUINO_USB_CDC_ON_BOOT=1
111-
board = lolin_c3_mini
103+
;[env:esp32c3]
104+
;platform = espressif32 @ 6.7.0
105+
;platform_packages =
106+
; framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32.git#3.0.1
107+
; framework-arduinoespressif32-libs @ https://github.com/espressif/arduino-esp32/releases/download/3.0.1/esp32-arduino-libs-3.0.1.zip
108+
;build_flags =
109+
; ${env.build_flags}
110+
; -DESP32C3
111+
;board = lolin_c3_mini
112+
113+
; If you want to use a ESP32C6, you can use this (experimental)
114+
;[env:esp32c6]
115+
;platform = https://github.com/tasmota/platform-espressif32/releases/download/2024.06.11/platform-espressif32.zip
116+
;board = esp32-c6-devkitc-1
117+
;build_flags =
118+
; ${env.build_flags}
119+
; -DESP32C6
120+
; -DARDUINO_USB_MODE=1
121+
; -DARDUINO_USB_CDC_ON_BOOT=1

src/button.cpp

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
#include "button.h"
22

3+
#include <climits>
4+
5+
#if ESP32
36
#include <driver/rtc_io.h>
47
#include <esp_sleep.h>
5-
6-
#include <climits>
8+
#endif
79

810
#include "GlobalVars.h"
911

@@ -18,17 +20,18 @@ void IRAM_ATTR buttonInterruptHandler() {
1820
}
1921

2022
void OnOffButton::setup() {
23+
#if ESP8266
24+
digitalWrite(D0, LOW);
25+
pinMode(D0, OUTPUT);
26+
pinMode(ON_OFF_BUTTON_PIN, INPUT);
27+
#endif
28+
29+
#if ESP32
2130
pinMode(
2231
ON_OFF_BUTTON_PIN,
2332
BUTTON_ACTIVE_LEVEL == 0 ? INPUT_PULLUP : INPUT_PULLDOWN
2433
);
2534

26-
attachInterrupt(
27-
ON_OFF_BUTTON_PIN,
28-
buttonInterruptHandler,
29-
BUTTON_ACTIVE_LEVEL == 0 ? FALLING : RISING
30-
);
31-
3235
esp_deep_sleep_enable_gpio_wakeup(
3336
1 << ON_OFF_BUTTON_PIN,
3437
BUTTON_ACTIVE_LEVEL == 0 ? ESP_GPIO_WAKEUP_GPIO_LOW : ESP_GPIO_WAKEUP_GPIO_HIGH
@@ -41,6 +44,13 @@ void OnOffButton::setup() {
4144
#endif
4245

4346
gpio_deep_sleep_hold_en();
47+
#endif
48+
49+
attachInterrupt(
50+
ON_OFF_BUTTON_PIN,
51+
buttonInterruptHandler,
52+
BUTTON_ACTIVE_LEVEL == 0 ? FALLING : RISING
53+
);
4454
}
4555

4656
void OnOffButton::tick() {
@@ -52,7 +62,7 @@ void OnOffButton::tick() {
5262
}
5363
#endif
5464

55-
#ifdef BUTTON_BATTERY_VOLTAGE_THRESHOLD
65+
#if defined(BUTTON_BATTERY_VOLTAGE_THRESHOLD) && BATTERY_MONITOR == BAT_EXTERNAL
5666
if (battery.getVoltage() >= BUTTON_BATTERY_VOLTAGE_THRESHOLD) {
5767
batteryBad = false;
5868
} else if (!batteryBad) {
@@ -120,7 +130,7 @@ void OnOffButton::emitOnBeforeSleep() {
120130
void OnOffButton::goToSleep() {
121131
emitOnBeforeSleep();
122132

123-
#ifdef BUTTON_IMU_ENABLE_PIN
133+
#if defined(BUTTON_IMU_ENABLE_PIN) && ESP32
124134
digitalWrite(BUTTON_IMU_ENABLE_PIN, LOW);
125135
gpio_hold_en(static_cast<gpio_num_t>(BUTTON_IMU_ENABLE_PIN));
126136
#endif
@@ -130,7 +140,11 @@ void OnOffButton::goToSleep() {
130140
while (buttonPressed && getButton())
131141
;
132142

143+
#if ESP8266
144+
ESP.deepSleep(0);
145+
#elif ESP32
133146
esp_deep_sleep_start();
147+
#endif
134148
}
135149

136150
OnOffButton OnOffButton::instance;

src/defines.h

Lines changed: 190 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,45 +20,216 @@
2020
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2121
THE SOFTWARE.
2222
*/
23+
// ================================================
24+
// See docs for configuration options and examples:
25+
// https://docs.slimevr.dev/firmware/configuring-project.html#2-configuring-definesh
26+
// ================================================
2327

24-
#define IMU IMU_LSM6DSV
25-
#define BOARD BOARD_CUSTOM
26-
#define IMU_ROTATION DEG_90
28+
// Set parameters of IMU and board used
29+
#define IMU IMU_BNO085
30+
#define SECOND_IMU IMU
31+
#define BOARD BOARD_SLIMEVR
32+
#define IMU_ROTATION DEG_270
33+
#define SECOND_IMU_ROTATION DEG_270
2734

2835
#define PRIMARY_IMU_OPTIONAL false
36+
#define SECONDARY_IMU_OPTIONAL true
2937

30-
#define MAX_IMU_COUNT 1
38+
// Set I2C address here or directly in IMU_DESC_ENTRY for each IMU used
39+
// If not set, default address is used based on the IMU and Sensor ID
40+
// #define PRIMARY_IMU_ADDRESS_ONE 0x4a
41+
// #define SECONDARY_IMU_ADDRESS_TWO 0x4b
3142

32-
#define ON_OFF_BUTTON 1
43+
#define MAX_IMU_COUNT 2
44+
45+
// Axis mapping example
46+
/*
47+
#include "sensors/axisremap.h"
48+
#define BMI160_QMC_REMAP AXIS_REMAP_BUILD(AXIS_REMAP_USE_Y, AXIS_REMAP_USE_XN,
49+
AXIS_REMAP_USE_Z, \ AXIS_REMAP_USE_YN, AXIS_REMAP_USE_X, AXIS_REMAP_USE_Z)
50+
51+
IMU_DESC_ENTRY(IMU_BMP160, PRIMARY_IMU_ADDRESS_ONE, IMU_ROTATION, PIN_IMU_SCL,
52+
PIN_IMU_SDA, PRIMARY_IMU_OPTIONAL, BMI160_QMC_REMAP) \
53+
*/
3354

3455
#ifndef IMU_DESC_LIST
35-
#define IMU_DESC_LIST \
36-
IMU_DESC_ENTRY( \
37-
IMU, \
38-
PRIMARY_IMU_ADDRESS_ONE, \
39-
IMU_ROTATION, \
40-
PIN_IMU_SCL, \
41-
PIN_IMU_SDA, \
42-
PRIMARY_IMU_OPTIONAL, \
43-
PIN_IMU_INT \
56+
#define IMU_DESC_LIST \
57+
IMU_DESC_ENTRY( \
58+
IMU, \
59+
PRIMARY_IMU_ADDRESS_ONE, \
60+
IMU_ROTATION, \
61+
PIN_IMU_SCL, \
62+
PIN_IMU_SDA, \
63+
PRIMARY_IMU_OPTIONAL, \
64+
PIN_IMU_INT \
65+
) \
66+
IMU_DESC_ENTRY( \
67+
SECOND_IMU, \
68+
SECONDARY_IMU_ADDRESS_TWO, \
69+
SECOND_IMU_ROTATION, \
70+
PIN_IMU_SCL, \
71+
PIN_IMU_SDA, \
72+
SECONDARY_IMU_OPTIONAL, \
73+
PIN_IMU_INT_2 \
4474
)
4575
#endif
4676

77+
// Battery monitoring options (comment to disable):
78+
// BAT_EXTERNAL for ADC pin,
79+
// BAT_INTERNAL for internal - can detect only low battery,
80+
// BAT_MCP3021 for external ADC connected over I2C
4781
#define BATTERY_MONITOR BAT_EXTERNAL
4882

49-
#define PIN_IMU_SDA 5
50-
#define PIN_IMU_SCL 6
83+
// BAT_EXTERNAL definition override
84+
// D1 Mini boards with ESP8266 have internal resistors. For these boards you only have
85+
// to adjust BATTERY_SHIELD_RESISTANCE. For other boards you can now adjust the other
86+
// resistor values. The diagram looks like this:
87+
// (Battery)--- [BATTERY_SHIELD_RESISTANCE] ---(INPUT_BOARD)--- [BATTERY_SHIELD_R2]
88+
// ---(ESP32_INPUT)--- [BATTERY_SHIELD_R1] --- (GND)
89+
// #define BATTERY_SHIELD_RESISTANCE 180 //130k BatteryShield, 180k SlimeVR or fill in
90+
// external resistor value in kOhm #define BATTERY_SHIELD_R1 100 // Board voltage
91+
// divider resistor Ain to GND in kOhm #define BATTERY_SHIELD_R2 220 // Board voltage
92+
// divider resistor Ain to INPUT_BOARD in kOhm
93+
94+
// LED configuration:
95+
// Configuration Priority 1 = Highest:
96+
// 1. LED_PIN
97+
// 2. LED_BUILTIN
98+
//
99+
// LED_PIN
100+
// - Number or Symbol (D1,..) of the Output
101+
// - To turn off the LED, set LED_PIN to LED_OFF
102+
// LED_INVERTED
103+
// - false for output 3.3V on high
104+
// - true for pull down to GND on high
105+
106+
// Board-specific configurations
107+
#if BOARD == BOARD_SLIMEVR
108+
#define PIN_IMU_SDA 14
109+
#define PIN_IMU_SCL 12
110+
#define PIN_IMU_INT 16
111+
#define PIN_IMU_INT_2 13
112+
#define PIN_BATTERY_LEVEL 17
113+
#define LED_PIN 2
114+
#define LED_INVERTED true
115+
#ifndef BATTERY_SHIELD_RESISTANCE
116+
#define BATTERY_SHIELD_RESISTANCE 0
117+
#endif
118+
#ifndef BATTERY_SHIELD_R1
119+
#define BATTERY_SHIELD_R1 10
120+
#endif
121+
#ifndef BATTERY_SHIELD_R2
122+
#define BATTERY_SHIELD_R2 40.2
123+
#endif
124+
#elif BOARD == BOARD_SLIMEVR_LEGACY || BOARD == BOARD_SLIMEVR_DEV
125+
#define PIN_IMU_SDA 4
126+
#define PIN_IMU_SCL 5
127+
#define PIN_IMU_INT 10
128+
#define PIN_IMU_INT_2 13
129+
#define PIN_BATTERY_LEVEL 17
130+
#define LED_PIN 2
131+
#define LED_INVERTED true
132+
#ifndef BATTERY_SHIELD_RESISTANCE
133+
#define BATTERY_SHIELD_RESISTANCE 0
134+
#endif
135+
#ifndef BATTERY_SHIELD_R1
136+
#define BATTERY_SHIELD_R1 10
137+
#endif
138+
#ifndef BATTERY_SHIELD_R2
139+
#define BATTERY_SHIELD_R2 40.2
140+
#endif
141+
#elif BOARD == BOARD_NODEMCU || BOARD == BOARD_WEMOSD1MINI
142+
#define PIN_IMU_SDA D2
143+
#define PIN_IMU_SCL D1
144+
#define PIN_IMU_INT D5
145+
#define PIN_IMU_INT_2 D6
146+
#define PIN_BATTERY_LEVEL A0
147+
// #define LED_PIN 2
148+
// #define LED_INVERTED true
149+
#ifndef BATTERY_SHIELD_RESISTANCE
150+
#define BATTERY_SHIELD_RESISTANCE 180
151+
#endif
152+
#ifndef BATTERY_SHIELD_R1
153+
#define BATTERY_SHIELD_R1 100
154+
#endif
155+
#ifndef BATTERY_SHIELD_R2
156+
#define BATTERY_SHIELD_R2 220
157+
#endif
158+
#elif BOARD == BOARD_ESP01
159+
#define PIN_IMU_SDA 2
160+
#define PIN_IMU_SCL 0
51161
#define PIN_IMU_INT 255
52162
#define PIN_IMU_INT_2 255
163+
#define PIN_BATTERY_LEVEL 255
164+
#define LED_PIN LED_OFF
165+
#define LED_INVERTED false
166+
#elif BOARD == BOARD_TTGO_TBASE
167+
#define PIN_IMU_SDA 5
168+
#define PIN_IMU_SCL 4
169+
#define PIN_IMU_INT 14
170+
#define PIN_IMU_INT_2 13
171+
#define PIN_BATTERY_LEVEL A0
172+
// #define LED_PIN 2
173+
// #define LED_INVERTED false
174+
#elif BOARD == BOARD_CUSTOM
175+
// Define pins by the examples above
176+
#elif BOARD == BOARD_WROOM32
177+
#define PIN_IMU_SDA 21
178+
#define PIN_IMU_SCL 22
179+
#define PIN_IMU_INT 23
180+
#define PIN_IMU_INT_2 25
181+
#define PIN_BATTERY_LEVEL 36
182+
// #define LED_PIN 2
183+
// #define LED_INVERTED false
184+
#elif BOARD == BOARD_LOLIN_C3_MINI
185+
#define PIN_IMU_SDA 5
186+
#define PIN_IMU_SCL 4
187+
#define PIN_IMU_INT 6
188+
#define PIN_IMU_INT_2 8
53189
#define PIN_BATTERY_LEVEL 3
54-
#define LED_PIN 0
190+
#define LED_PIN 7
191+
// #define LED_INVERTED false
192+
#elif BOARD == BOARD_BEETLE32C3
193+
#define PIN_IMU_SDA 8
194+
#define PIN_IMU_SCL 9
195+
#define PIN_IMU_INT 6
196+
#define PIN_IMU_INT_2 7
197+
#define PIN_BATTERY_LEVEL 3
198+
#define LED_PIN 10
199+
#define LED_INVERTED false
200+
#elif BOARD == BOARD_ES32C3DEVKITM1 || BOARD == BOARD_ES32C6DEVKITC1
201+
#define PIN_IMU_SDA 5
202+
#define PIN_IMU_SCL 4
203+
#define PIN_IMU_INT 6
204+
#define PIN_IMU_INT_2 7
205+
#define PIN_BATTERY_LEVEL 3
206+
#define LED_PIN \
207+
LED_OFF // RGB LED Protocol would need to be implementetet did not brother for the
208+
// test, because the board ideal for tracker ifself
209+
// #define LED_INVERTED false
210+
#elif BOARD == BOARD_WEMOSWROOM02
211+
#define PIN_IMU_SDA 2
212+
#define PIN_IMU_SCL 14
213+
#define PIN_IMU_INT 0
214+
#define PIN_IMU_INT_2 4
215+
#define PIN_BATTERY_LEVEL A0
216+
#define LED_PIN 16
55217
#define LED_INVERTED true
218+
#elif BOARD == BOARD_XIAO_ESP32C3
219+
#define PIN_IMU_SDA 6 // D4
220+
#define PIN_IMU_SCL 7 // D5
221+
#define PIN_IMU_INT 5 // D3
222+
#define PIN_IMU_INT_2 10 // D10
223+
#define LED_PIN 4 // D2
224+
#define LED_INVERTED false
225+
#define PIN_BATTERY_LEVEL 2 // D0 / A0
56226
#ifndef BATTERY_SHIELD_RESISTANCE
57227
#define BATTERY_SHIELD_RESISTANCE 0
58228
#endif
59229
#ifndef BATTERY_SHIELD_R1
60-
#define BATTERY_SHIELD_R1 150
230+
#define BATTERY_SHIELD_R1 100
61231
#endif
62232
#ifndef BATTERY_SHIELD_R2
63-
#define BATTERY_SHIELD_R2 150
233+
#define BATTERY_SHIELD_R2 100
234+
#endif
64235
#endif

0 commit comments

Comments
 (0)