-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Usermod SbusControl #4464
base: main
Are you sure you want to change the base?
Usermod SbusControl #4464
Changes from all commits
926896c
b0e3b8e
509d8d7
52ad73c
e607cc1
a4c7675
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
This user mod can be used to control WLED via the sbus. | ||
Sbus is a protocol that RC receivers use to transmit control signals to other components. | ||
By connecting the ESP32 to the receiver, WLED can be used as LEDController in RC plains, cars or ships. | ||
|
||
You can change the brightness and between 9 presets. Setting up the Leds needs to be done via APP or WebUi. | ||
|
||
To build this usermod the following lines needs to be added to your target in the platformio.ini file: | ||
|
||
build_flags = DUSERMOD_SBUS_CONTROL | ||
lib_deps = https://github.com/bolderflight/sbus.git | ||
|
||
Hardware setup: | ||
|
||
Connect ground of receiver and ESP32. | ||
Connect receiver sbus_out pin with ESP32 pin configured for sbus_in in config. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
#pragma once | ||
|
||
#include "wled.h" | ||
#include <sbus.h> | ||
#include <math.h> | ||
|
||
class SbusControl : public Usermod { | ||
public: | ||
SbusControl(){} | ||
|
||
void setup() override | ||
{ | ||
sbus_rx_ = bfs::SbusRx(&Serial1, static_cast<int8_t>(sbus_pin_), 0, true); | ||
sbus_rx_.Begin(); | ||
} | ||
|
||
void loop() override | ||
{ | ||
const uint32_t ts_ms = millis(); | ||
|
||
if (ts_ms < (last_ts_ms_ + update_period_ms_)) | ||
{ | ||
return; | ||
} | ||
|
||
if (sbus_rx_.Read()) { | ||
/* Grab the received data */ | ||
bfs::SbusData data = sbus_rx_.data(); | ||
|
||
int16_t brightness = data.ch[ch_brightness_ - 1]; | ||
brightness = std::min(brightness, static_cast<int16_t>(2000)); | ||
brightness -= 250; | ||
brightness = std::max(brightness, static_cast<int16_t>(0)); | ||
brightness /= 7; | ||
if (brightness != last_bri_) | ||
{ | ||
bri = brightness; | ||
applyFinalBri(); | ||
} | ||
|
||
uint8_t swicth_1 = getSwitchPosition(data.ch[ch_mode_select_1_ - 1]); | ||
uint8_t swicth_2 = getSwitchPosition(data.ch[ch_mode_select_2_ - 1]); | ||
uint8_t mode = swicth_1 + (swicth_2 * 3) + 1; | ||
if (mode != last_mode_) | ||
{ | ||
applyPreset(mode); | ||
} | ||
|
||
last_bri_ = brightness; | ||
last_mode_ = mode; | ||
} | ||
|
||
last_ts_ms_ = ts_ms; | ||
} | ||
|
||
void addToConfig(JsonObject& root) | ||
{ | ||
JsonObject top = root.createNestedObject(FPSTR(kConfigNode)); | ||
top[kConfigNodeBrightnessCh] = ch_brightness_; | ||
top[kConfigNodeMode1Ch] = ch_mode_select_1_; | ||
top[kConfigNodeMode2Ch] = ch_mode_select_2_; | ||
top[kConfigNodeSbusPin] = sbus_pin_; | ||
top[kConfigNodeUpdatePeriod] = update_period_ms_; | ||
} | ||
|
||
bool readFromConfig(JsonObject& root) | ||
{ | ||
JsonObject top = root[FPSTR(kConfigNode)]; | ||
|
||
bool configComplete = !top.isNull(); | ||
|
||
configComplete &= getJsonValue(top[kConfigNodeBrightnessCh], ch_brightness_, ch_brightness_); | ||
configComplete &= getJsonValue(top[kConfigNodeMode1Ch], ch_mode_select_1_, ch_mode_select_1_); | ||
configComplete &= getJsonValue(top[kConfigNodeMode2Ch], ch_mode_select_2_, ch_mode_select_2_); | ||
configComplete &= getJsonValue(top[kConfigNodeSbusPin], sbus_pin_, sbus_pin_); | ||
configComplete &= getJsonValue(top[kConfigNodeUpdatePeriod], update_period_ms_, update_period_ms_); | ||
return configComplete; | ||
} | ||
|
||
uint16_t getId() | ||
{ | ||
return USERMOD_ID_SBUS_CONTROL; | ||
} | ||
|
||
private: | ||
static constexpr auto* kConfigNode PROGMEM = "sbusControl"; | ||
static constexpr auto* kConfigNodeBrightnessCh PROGMEM = "brightnessChannel"; | ||
static constexpr auto* kConfigNodeMode1Ch PROGMEM = "ModeSelect1Channel"; | ||
static constexpr auto* kConfigNodeMode2Ch PROGMEM = "ModeSelect2Channel"; | ||
static constexpr auto* kConfigNodeSbusPin PROGMEM = "SbusPin"; | ||
static constexpr auto* kConfigNodeUpdatePeriod PROGMEM = "UpdatePrtiodMs"; | ||
|
||
uint32_t update_period_ms_ = 1000; | ||
|
||
uint8_t sbus_pin_ = 16; | ||
uint8_t ch_mode_select_1_ = 10; | ||
uint8_t ch_mode_select_2_ = 11; | ||
uint8_t ch_brightness_ = 12; | ||
|
||
uint32_t last_ts_ms_ = 0; | ||
uint8_t last_mode_ = 1; | ||
uint8_t last_bri_ = 1; | ||
|
||
bfs::SbusRx sbus_rx_= {&Serial1, static_cast<int8_t>(sbus_pin_), 0, true}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use pin manager to reserver the pin (or it can be assigned to other functions as well, leading to conflicts) |
||
|
||
uint8_t getSwitchPosition(int16_t ch_value) | ||
{ | ||
Serial.println(ch_value); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use debug output |
||
// Decoding a switch position from ch_values | ||
if (ch_value < 666) return 0; | ||
if (ch_value < 1333) return 1; | ||
return 2; | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -250,6 +250,10 @@ | |
#include "../usermods/usermod_v2_RF433/usermod_v2_RF433.h" | ||
#endif | ||
|
||
#ifdef USERMOD_SBUS_CONTROL | ||
#include "../usermods/sbus_control/usermod_sbus_control.h" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. indentation |
||
#endif | ||
|
||
void registerUsermods() | ||
{ | ||
/* | ||
|
@@ -486,4 +490,8 @@ void registerUsermods() | |
#ifdef USERMOD_RF433 | ||
UsermodManager::add(new RF433Usermod()); | ||
#endif | ||
|
||
#ifdef USERMOD_SBUS_CONTROL | ||
usermods.add(new SbusControl()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is outdated |
||
#endif | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this will also assign GPIO0 to serial, intentional or an oversight?
also: Serial1 is used by DMX as well. @netmindz can Serial1 be used here without restrictions? If not: there is another UM doing it this way that may need updating.