Skip to content

Commit

Permalink
Tie up ble srv and pixels
Browse files Browse the repository at this point in the history
  • Loading branch information
botamochi6277 committed Jan 31, 2024
1 parent 17f2539 commit eb14708
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 101 deletions.
6 changes: 3 additions & 3 deletions include/LedStrip.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ enum class IntensityFuncId : unsigned char {
float travelingWave(float freq, float time, float position, float speed,
float initial_phase) {
return 0.5f *
sinf(2.0f * M_PI * freq * (time - (position / (speed + 1e-9f))) +
0.5f);
sinf(2.0f * M_PI * freq * (time - (position / (speed + 1e-9f)))) +
0.5f;
}

// pulse function
Expand Down Expand Up @@ -233,7 +233,7 @@ void PixelManager::setIntensity(float value, IntensityFuncId func_id) {
break;
case IntensityFuncId::TravelingWave:
for (size_t i = 0; i < NUM_PIXELS; i++) {
travelingWave(1.0f, value, this->pixel_units[i].position(), 1.0f, 0.0f);
travelingWave(0.1f, value, this->pixel_units[i].position(), 0.1f, 0.0f);
}
break;
default:
Expand Down
24 changes: 4 additions & 20 deletions include/MyTasks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,6 @@ void updateColorCache(led_strip::PixelManager &manager,
if (pixel_srv.brightness_chr.written()) {
pixels.setBrightness(pixel_srv.brightness_chr.value());
}

// color parameter was changed, updating static colors
if (pixel_srv.color01_chr.written() || pixel_srv.color02_chr.written() ||
pixel_srv.color03_chr.written() || pixel_srv.color04_chr.written() ||
pixel_srv.colormap_chr.written() || pixel_srv.num_colors_chr.written() ||
loop_count == 0) {
// update clock

// assign palette colors
manager.setPaletteColor(0, pixel_srv.color01_chr.value());
manager.setPaletteColor(1, pixel_srv.color02_chr.value());
manager.setPaletteColor(2, pixel_srv.color03_chr.value());
manager.setPaletteColor(3, pixel_srv.color04_chr.value());

// led_strip::blend(transited_colors, palette, pixels.numPixels(),
// pixel_srv.num_colors_chr.value(),
// pixel_srv.colormap_chr.value());
}
}

// void setHeatColors(led_strip::PixelManager &manager, float temperature) {
Expand Down Expand Up @@ -64,7 +46,7 @@ void setPixelColors(led_strip::PixelManager &manager,
}
}

enum class SensorSource { Timer, ACC };
enum class SensorSource : unsigned char { Timer, Cycle, ACC };

void updatePixelColors(led_strip::PixelManager &manager, SensorSource sensor,
led_strip::IntensityFuncId func_id,
Expand All @@ -75,7 +57,9 @@ void updatePixelColors(led_strip::PixelManager &manager, SensorSource sensor,
case SensorSource::Timer:
sensor_value = millis() / 1.0e3f;
break;

case SensorSource::Cycle:
sensor_value = 0.5f * sinf(2.0f * M_PI * 1.0f * millis() / 1.0e3f) + 0.5f;
break;
default:
break;
}
Expand Down
90 changes: 38 additions & 52 deletions include/NeopixelService.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,15 @@ class NeoPixelService : public BLEService {

// color palette
BLEUnsignedCharCharacteristic num_colors_chr;
BLEUnsignedIntCharacteristic color01_chr;
BLEUnsignedIntCharacteristic color02_chr;
BLEUnsignedIntCharacteristic color03_chr;
BLEUnsignedIntCharacteristic color04_chr;

BLEUnsignedCharCharacteristic colormap_chr; // gradation, cycle
BLEUnsignedCharCharacteristic lighting_mode_chr; // (acc,time)
BLEUnsignedCharCharacteristic transition_chr; // dissolve, slide, etc.
BLEUnsignedCharCharacteristic colormap_chr;
BLEUnsignedCharCharacteristic source_chr;
BLEUnsignedCharCharacteristic intensity_func_chr;

NeoPixelService(/* args */);
~NeoPixelService();
// void init();
void init(uint8_t brightness, uint8_t sensor_id, uint8_t intensity_id,
uint8_t cmap);
};

NeoPixelService::NeoPixelService()
Expand All @@ -67,15 +65,10 @@ NeoPixelService::NeoPixelService()
BLERead | BLEWrite),
num_colors_chr("19B10021-E8F2-537E-4F6C-D104768A1214",
BLERead | BLEWrite),
color01_chr("19B10022-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite),
color02_chr("19B10023-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite),
color03_chr("19B10024-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite),
color04_chr("19B10025-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite),
colormap_chr("19B10026-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite),
transition_chr("19B10027-E8F2-537E-4F6C-D104768A1214",
BLERead | BLEWrite),
lighting_mode_chr("19B10028-E8F2-537E-4F6C-D104768A1214",
BLERead | BLEWrite) {
source_chr("19B10027-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite),
intensity_func_chr("19B10028-E8F2-537E-4F6C-D104768A1214",
BLERead | BLEWrite) {
// add characteristics to service
this->addCharacteristic(this->timer_chr);
this->addCharacteristic(this->imu_available_chr);
Expand All @@ -84,13 +77,9 @@ NeoPixelService::NeoPixelService()
this->addCharacteristic(this->brightness_chr);

this->addCharacteristic(this->num_colors_chr);
this->addCharacteristic(this->color01_chr);
this->addCharacteristic(this->color02_chr);
this->addCharacteristic(this->color03_chr);
this->addCharacteristic(this->color04_chr);
this->addCharacteristic(this->colormap_chr);
this->addCharacteristic(this->transition_chr);
this->addCharacteristic(this->lighting_mode_chr);
this->addCharacteristic(this->source_chr);
this->addCharacteristic(this->intensity_func_chr);

// User Description
// system property characteristic
Expand All @@ -109,21 +98,12 @@ NeoPixelService::NeoPixelService()
BLEDescriptor num_colors_descriptor("2901", "#used_colors");
this->num_colors_chr.addDescriptor(num_colors_descriptor);

BLEDescriptor color01_descriptor("2901", "color01 (HSB)");
BLEDescriptor color02_descriptor("2901", "color02 (HSB)");
BLEDescriptor color03_descriptor("2901", "color03 (HSB)");
BLEDescriptor color04_descriptor("2901", "color04 (HSB)");
this->color01_chr.addDescriptor(color01_descriptor);
this->color02_chr.addDescriptor(color02_descriptor);
this->color03_chr.addDescriptor(color03_descriptor);
this->color04_chr.addDescriptor(color04_descriptor);

BLEDescriptor blending_descriptor("2901", "color blending type");
this->colormap_chr.addDescriptor(blending_descriptor);
BLEDescriptor transition_descriptor("2901", "transition type");
this->transition_chr.addDescriptor(transition_descriptor);
BLEDescriptor fluctuation_descriptor("2901", "fluctuation");
this->lighting_mode_chr.addDescriptor(fluctuation_descriptor);
BLEDescriptor cmap_descriptor("2901", "colormap ID");
this->colormap_chr.addDescriptor(cmap_descriptor);
BLEDescriptor src_descriptor("2901", "source data ID");
this->source_chr.addDescriptor(src_descriptor);
BLEDescriptor i_func_descriptor("2901", "intensity func ID");
this->intensity_func_chr.addDescriptor(i_func_descriptor);

// Format Description
BLEDescriptor millisec_descriptor("2904", this->msec_format_, 7);
Expand All @@ -138,25 +118,31 @@ NeoPixelService::NeoPixelService()

BLEDescriptor num_pixels_unitless_descriptor("2904", this->cmd_format_, 7);
this->num_pixels_chr.addDescriptor(num_pixels_unitless_descriptor);
BLEDescriptor color_unitless_descriptor01("2904", this->color_format_, 7);
this->color01_chr.addDescriptor(color_unitless_descriptor01);
BLEDescriptor color_unitless_descriptor02("2904", this->color_format_, 7);
this->color02_chr.addDescriptor(color_unitless_descriptor02);
BLEDescriptor color_unitless_descriptor03("2904", this->color_format_, 7);
this->color03_chr.addDescriptor(color_unitless_descriptor03);
BLEDescriptor color_unitless_descriptor04("2904", this->color_format_, 7);
this->color04_chr.addDescriptor(color_unitless_descriptor04);

BLEDescriptor trans_unitless_descriptor("2904", this->cmd_format_, 7);
this->transition_chr.addDescriptor(trans_unitless_descriptor);
BLEDescriptor blending_unitless_descriptor("2904", this->cmd_format_, 7);
this->colormap_chr.addDescriptor(blending_unitless_descriptor);
BLEDescriptor fluctuation_unitless_descriptor("2904", this->cmd_format_, 7);
this->lighting_mode_chr.addDescriptor(fluctuation_unitless_descriptor);

BLEDescriptor source_unitless_descriptor("2904", this->cmd_format_, 7);
this->source_chr.addDescriptor(source_unitless_descriptor);
BLEDescriptor colormap_unitless_descriptor("2904", this->cmd_format_, 7);
this->colormap_chr.addDescriptor(colormap_unitless_descriptor);
BLEDescriptor func_unitless_descriptor("2904", this->cmd_format_, 7);
this->intensity_func_chr.addDescriptor(func_unitless_descriptor);
}

NeoPixelService::~NeoPixelService(){};

// void NeoPixelService::init() {
// this->init(20U, uint8_t Timer,
// uint8_t::TravelingWave,
// colormap::ColormapId::Hsv);
// }
void NeoPixelService::init(uint8_t brightness, uint8_t sensor_id,
uint8_t intensity_id, uint8_t cmap) {
this->brightness_chr.writeValue(brightness);
this->num_pixels_chr.writeValue(NUM_PIXELS);
this->source_chr.writeValue(sensor_id);
this->intensity_func_chr.writeValue(intensity_id);
this->colormap_chr.writeValue(cmap);
}

} // namespace ble

#endif
47 changes: 21 additions & 26 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include "BLEPresets.hpp"
#include "ColorUtils.hpp"
#include "Colormap.hpp"
#include "LedStrip.hpp"
#include "MyTasks.hpp"
#include "MyUtils.hpp"
Expand Down Expand Up @@ -94,7 +95,9 @@ void setup() {
BLE.setDeviceName(local_name.c_str());
BLE.setLocalName("NeoPixels");
BLE.setAdvertisedService(pixel_srv);

pixel_srv.init(20U, static_cast<uint8_t>(tasks::SensorSource::Cycle),
static_cast<uint8_t>(led_strip::IntensityFuncId::Heat),
static_cast<uint8_t>(colormap::ColormapId::Hsv));
// add service
BLE.addService(pixel_srv);

Expand All @@ -111,47 +114,39 @@ void setup() {
#endif

// set the initial value for the characteristic:
pixel_srv.brightness_chr.writeValue(20);
pixel_srv.num_pixels_chr.writeValue(NUM_PIXELS);
pixel_srv.num_colors_chr.writeValue(2U);
pixel_srv.transition_chr.writeValue(TRANSITION_DISSOLVE);
pixel_srv.color01_chr.writeValue(color::hsbToHsbhex(0x00, 0xff, 0xff));
pixel_srv.color02_chr.writeValue(color::hsbToHsbhex(0xffff, 255U, 255U));
pixel_srv.color04_chr.writeValue(color::hsbToHsbhex(0xffff, 255U, 255U));

digitalWrite(PIXEL_PIN, LOW);
pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
pixels.setBrightness(20);
pixels.setBrightness(pixel_srv.brightness_chr.value());
pixels.show(); // Turn OFF all pixels

// init color buffer
for (size_t i = 0; i < NUM_PIXELS; i++) {
color_manager.setCurrentColor(i, color::hsbToHsbhex(0xff00, 0xff, 0x00));

color_manager.setPaletteColor(0, pixel_srv.color01_chr.value());
color_manager.setPaletteColor(1, pixel_srv.color02_chr.value());
color_manager.setPaletteColor(2, pixel_srv.color03_chr.value());
color_manager.setPaletteColor(3, pixel_srv.color04_chr.value());
}
// temporal
for (size_t i = 0; i < NUM_PIXELS; i++) {
color_manager.setFluctuationColor(i, color_manager.getPaletteColor(3));
}
// for (size_t i = 0; i < NUM_PIXELS; i++) {
// color_manager.setCurrentColor(i, color::hsbToHsbhex(0xff00, 0xff, 0x00));
// }
// // temporal
// for (size_t i = 0; i < NUM_PIXELS; i++) {
// color_manager.setFluctuationColor(i, color_manager.getPaletteColor(3));
// }

loop_count = 0;

Tasks.add("BLE_polling", [] { BLE.poll(); })->startFps(10);

#ifdef SEEED_XIAO_NRF52840_SENSE
Tasks.add("Heat_beats", [] { digitalWrite(LEDG, !digitalRead(LEDG)); })
Tasks.add("Heart_beats", [] { digitalWrite(LEDG, !digitalRead(LEDG)); })
->startFps(1.0);
#endif
Tasks
.add("UpdateColors",
.add("UpdateColorCache",
[] {
tasks::updatePixelColors(color_manager, tasks::SensorSource::Timer,
led_strip::IntensityFuncId::TravelingWave,
colormap::ColormapId::Spectral);
tasks::updatePixelColors(
color_manager,
static_cast<tasks::SensorSource>(pixel_srv.source_chr.value()),
static_cast<led_strip::IntensityFuncId>(
pixel_srv.intensity_func_chr.value()),
static_cast<colormap::ColormapId>(
pixel_srv.colormap_chr.value()));
})
->startFps(24.0);
Tasks
Expand Down

0 comments on commit eb14708

Please sign in to comment.