Skip to content
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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions usermods/sbus_control/readme.md
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.
114 changes: 114 additions & 0 deletions usermods/sbus_control/usermod_sbus_control.h
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);
Copy link
Collaborator

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.

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};
Copy link
Collaborator

Choose a reason for hiding this comment

The 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);
Copy link
Collaborator

Choose a reason for hiding this comment

The 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;
}
};
1 change: 1 addition & 0 deletions wled00/const.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@
#define USERMOD_ID_PIXELS_DICE_TRAY 54 //Usermod "pixels_dice_tray.h"
#define USERMOD_ID_DEEP_SLEEP 55 //Usermod "usermod_deep_sleep.h"
#define USERMOD_ID_RF433 56 //Usermod "usermod_v2_RF433.h"
#define USERMOD_ID_SBUS_CONTROL 57 //Usermod "usermod_sbus_control.h"

//Access point behavior
#define AP_BEHAVIOR_BOOT_NO_CONN 0 //Open AP when no connection after boot
Expand Down
8 changes: 8 additions & 0 deletions wled00/usermods_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation

#endif

void registerUsermods()
{
/*
Expand Down Expand Up @@ -486,4 +490,8 @@ void registerUsermods()
#ifdef USERMOD_RF433
UsermodManager::add(new RF433Usermod());
#endif

#ifdef USERMOD_SBUS_CONTROL
usermods.add(new SbusControl());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is outdated

#endif
}