Skip to content

Commit 691be3a

Browse files
committed
analog + esp32c3
1 parent 4f26eef commit 691be3a

File tree

8 files changed

+333
-99
lines changed

8 files changed

+333
-99
lines changed

platformio.ini

+60-3
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,81 @@
1010

1111
[env]
1212
monitor_speed = 115200
13-
upload_speed = 460800
13+
;upload_speed = 460800
1414
lib_deps =
1515
yoursunny/WifiEspNow @ ^0.0.20230713
1616
https://github.com/rtlopez/espnow-rclink/archive/refs/tags/v0.1.1.zip
1717
; EspNowRcLink=symlink://../espnow-rclink
1818

1919
build_flags =
20-
-std=c++14
20+
-std=gnu++17
2121
-Wall
22-
-O2
22+
23+
build_unflags =
24+
-std=gnu++11
25+
-ggdb
2326

2427
[env:esp32]
2528
platform = espressif32
2629
board = lolin32
2730
framework = arduino
2831
extra_scripts = merge_firmware.py
32+
lib_deps =
33+
${env.lib_deps}
34+
build_unflags =
35+
${env.build_unflags}
36+
build_flags =
37+
${env.build_flags}
38+
-DPIN_PPM=13
39+
40+
[env:esp32c3_ppm]
41+
board = esp32-c3-devkitm-1
42+
platform = espressif32
43+
framework = arduino
44+
extra_scripts = merge_firmware.py
45+
lib_deps =
46+
${env.lib_deps}
47+
build_unflags =
48+
${env.build_unflags}
49+
build_flags =
50+
${env.build_flags}
51+
-DESP32C3
52+
-DARDUINO_USB_MODE=1
53+
-DARDUINO_USB_CDC_ON_BOOT=1
54+
-DPIN_PPM=6
55+
56+
[env:esp32c3_analog]
57+
board = esp32-c3-devkitm-1
58+
platform = espressif32
59+
framework = arduino
60+
extra_scripts = merge_firmware.py
61+
lib_deps =
62+
${env.lib_deps}
63+
build_unflags =
64+
${env.build_unflags}
65+
build_flags =
66+
${env.build_flags}
67+
-DESP32C3
68+
-DARDUINO_USB_MODE=1
69+
-DARDUINO_USB_CDC_ON_BOOT=1
70+
-DUSE_ANALOG=1
71+
-DPIN_POT0=0
72+
-DPIN_POT1=1
73+
-DPIN_POT2=3
74+
-DPIN_POT3=4
75+
-DPIN_SW0=8
76+
-DPIN_SW1=6
77+
-DPIN_SW2=-1
78+
; -DPRINT_INFO=1 ; print some details to console
2979

3080
[env:esp8266]
3181
platform = espressif8266
3282
board = d1_mini
3383
framework = arduino
84+
lib_deps =
85+
${env.lib_deps}
86+
build_unflags =
87+
${env.build_unflags}
88+
build_flags =
89+
${env.build_flags}
90+
-DPIN_PPM=13

src/analog.hpp

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#pragma once
2+
#include <cstdint>
3+
#include <cstddef>
4+
#include <Arduino.h>
5+
6+
class Analog
7+
{
8+
public:
9+
enum PinMode { MODE_OFF, MODE_ANALOG, MODE_DIGITAL };
10+
11+
Analog() {};
12+
13+
void attach(size_t channel, PinMode mode, int8_t pin)
14+
{
15+
_input[channel].mode = mode;
16+
_input[channel].pin = pin;
17+
_input[channel].value = 0;
18+
if(pin == -1) return;
19+
switch(mode)
20+
{
21+
case MODE_DIGITAL:
22+
pinMode(pin, INPUT_PULLUP);
23+
break;
24+
25+
case MODE_ANALOG:
26+
pinMode(pin, ANALOG);
27+
break;
28+
29+
case MODE_OFF:
30+
default:
31+
break;
32+
}
33+
}
34+
35+
void update()
36+
{
37+
for(auto& in: _input)
38+
{
39+
if(in.pin == -1) continue;
40+
switch(in.mode)
41+
{
42+
case MODE_DIGITAL:
43+
in.value = digitalRead(in.pin) ? 0 : 4096;
44+
break;
45+
46+
case MODE_ANALOG:
47+
in.value = analogRead(in.pin);
48+
break;
49+
50+
case MODE_OFF:
51+
default:
52+
break;
53+
}
54+
}
55+
}
56+
57+
int get(size_t channel) const
58+
{
59+
return map(_input[channel].value, 0, 4095, 1000, 2000);
60+
}
61+
62+
private:
63+
struct {
64+
PinMode mode = MODE_OFF;
65+
int pin = -1;
66+
int value = 0;
67+
} _input[16];
68+
};

src/main.cpp

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
#include <Arduino.h>
2+
#include "EspNowRcLink/Transmitter.h"
3+
#include "tx.hpp"
4+
#include "sim.hpp"
5+
6+
#if defined(PIN_PPM) && defined(USE_ANALOG)
7+
#error "PIN_PPM and USE_ANALOG Cannot be defined at the same time!"
8+
#endif
9+
10+
#ifdef PIN_PPM
11+
#include "ppm.h"
12+
PPM rxPpm;
13+
#endif
14+
15+
#ifdef USE_ANALOG
16+
#include "analog.hpp"
17+
Analog rxAnalog;
18+
#endif
19+
20+
// uncomment to activate simultor on channel 3
21+
// #define SIM_INTERVAL_MS 20 // 20ms => 50Hz
22+
23+
Sim rxSim;
24+
Tx txData;
25+
EspNowRcLink::Transmitter txEspNow;
26+
27+
void setup()
28+
{
29+
#ifdef PRINT_INFO
30+
Serial.begin(115200);
31+
#endif
32+
33+
#ifdef PIN_PPM
34+
rxPpm.begin(PIN_PPM, FALLING);
35+
#endif
36+
37+
#ifdef USE_ANALOG
38+
rxAnalog.attach(0, Analog::MODE_ANALOG, PIN_POT0);
39+
rxAnalog.attach(1, Analog::MODE_ANALOG, PIN_POT1);
40+
rxAnalog.attach(2, Analog::MODE_ANALOG, PIN_POT2);
41+
rxAnalog.attach(3, Analog::MODE_ANALOG, PIN_POT3);
42+
rxAnalog.attach(4, Analog::MODE_DIGITAL, PIN_SW0);
43+
rxAnalog.attach(5, Analog::MODE_DIGITAL, PIN_SW1);
44+
rxAnalog.attach(6, Analog::MODE_DIGITAL, PIN_SW2);
45+
#endif
46+
47+
txEspNow.begin(true);
48+
}
49+
50+
template<typename Dst, typename Src>
51+
void update(Dst& dst, const Src& src)
52+
{
53+
for(size_t channel = 0; channel < 8; ++channel)
54+
{
55+
dst.setChannel(channel, src.get(channel));
56+
}
57+
}
58+
59+
void loop()
60+
{
61+
uint32_t now = micros();
62+
uint32_t rxDelta = 0;
63+
64+
// decode inputs
65+
66+
#ifdef SIM_INTERVAL_MS
67+
static uint32_t simNext = 0;
68+
if(now >= simNext)
69+
{
70+
txData.setChannel(2, 1000 + rxSim.get(2));
71+
txData.setAvailable();
72+
simNext = now + SIM_INTERVAL_MS * 1000;
73+
}
74+
#else
75+
76+
#ifdef PIN_PPM
77+
if(rxPpm.available())
78+
{
79+
update(txData, rxPpm);
80+
txData.setAvailable();
81+
}
82+
#endif
83+
84+
#ifdef USE_ANALOG
85+
static uint32_t analogNext = 0;
86+
if(now > analogNext)
87+
{
88+
rxAnalog.update();
89+
update(txData, rxAnalog);
90+
txData.setAvailable();
91+
analogNext = now + 20000;
92+
}
93+
#endif
94+
95+
#endif
96+
97+
// transmit outputs
98+
99+
static uint32_t sendTimeout = 0;
100+
static uint32_t sendAfter = 0;
101+
static uint32_t sendLast = 0;
102+
if((now > sendAfter && txData.getAvailable()) || now > sendTimeout)
103+
{
104+
for(size_t c = 0; c < 8; c++)
105+
{
106+
txEspNow.setChannel(c, txData.getChannel(c));
107+
}
108+
sendTimeout = now + 50000ul;
109+
sendAfter = now + 10000ul;
110+
rxDelta = now - sendLast;
111+
sendLast = now;
112+
113+
txEspNow.commit();
114+
}
115+
txEspNow.update();
116+
117+
// print debug info
118+
119+
#ifdef PRINT_INFO
120+
static uint32_t printNext = 0;
121+
if(now > printNext)
122+
{
123+
//Serial.printf("V: %d, P: %d, D: %d, C: %d\n", v, sim_val, delta / 100, WiFi.channel());
124+
Serial.printf("%d,%d,%d,%d,%d\n",
125+
txData.getChannel(0), txData.getChannel(1), txData.getChannel(2), txData.getChannel(3), txData.getChannel(4)
126+
);
127+
printNext = now + 100000ul;
128+
}
129+
#endif
130+
}

src/ppm.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ bool PPM::available()
4242
return false;
4343
}
4444

45-
int16_t PPM::get(int channel)
45+
int16_t PPM::get(int channel) const
4646
{
4747
return _channels[channel];
4848
}

src/ppm.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class PPM
99
int begin(int pin, int mode = RISING);
1010
void end();
1111
bool available();
12-
int16_t get(int channel);
12+
int16_t get(int channel) const;
1313

1414
private:
1515
void handle();

src/sim.hpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#pragma once
2+
3+
#include <cstddef>
4+
5+
class Sim
6+
{
7+
public:
8+
int get(size_t channel)
9+
{
10+
if(_dir)
11+
{
12+
_val += _rate;
13+
if(_val >= 1000) _dir = false;
14+
}
15+
else
16+
{
17+
_val -= _rate;
18+
if(_val <= 0) _dir = true;
19+
}
20+
return _val;
21+
}
22+
private:
23+
bool _dir = true;
24+
const int _rate = 4;
25+
int _val;
26+
};
27+

0 commit comments

Comments
 (0)