Skip to content

Commit

Permalink
[ws2812] resumable function example and bug
Browse files Browse the repository at this point in the history
  • Loading branch information
hshose committed May 26, 2023
1 parent 9e5e77e commit 367dc71
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 2 deletions.
84 changes: 84 additions & 0 deletions examples/nucleo_f411re/ws2812b_dma/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright (c) 2019, Niklas Hauser
*
* This file is part of the modm project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
// ----------------------------------------------------------------------------

#include <modm/board.hpp>
#include <modm/driver/pwm/ws2812b.hpp>
#include <modm/ui/led/tables.hpp>
#include <modm/processing/timer.hpp>
#include <modm/processing.hpp>

using namespace Board;

using Output = Board::D11;
using DmaRx = Dma2::Channel0;
using DmaTx = Dma2::Channel3;
using SpiLed = SpiMaster1_Dma<DmaRx, DmaTx>;
modm::Ws2812b<SpiLed, Output, 8*8> leds;
modm::ShortPeriodicTimer tmr{33ms};

constexpr uint8_t max = 62;
uint8_t r=0, g=max/3, b=max/3*2;

class Ws2812Thread : public modm::pt::Protothread
{
public:

bool
update()
{
PT_BEGIN();

LedD13::setOutput();
Dma2::enable();
leds.initialize<Board::SystemClock>();

while (true)
{
for (size_t ii=0; ii < leds.size; ii++)
{
leds.setColor(ii,
{modm::ui::table22_8_256[r*3/2],
modm::ui::table22_8_256[g*3/2],
modm::ui::table22_8_256[b*3/2]});
if (r++ >= max) r = 0;
if (g++ >= max) g = 0;
if (b++ >= max) b = 0;
}
PT_CALL(leds.write());

LedD13::toggle();
timeout.restart(500ms);
PT_WAIT_UNTIL(timeout.isExpired());
}

PT_END();
}

private:
modm::ShortTimeout timeout;
};

Ws2812Thread ws2812_thread;

int
main()
{
Board::initialize();



while (true)
{
ws2812_thread.update();
}

return 0;
}
16 changes: 16 additions & 0 deletions examples/nucleo_f411re/ws2812b_dma/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version='1.0' encoding='UTF-8'?>
<library>
<extends>modm:nucleo-f411re</extends>
<options>
<option name=":build:build.path">../../../build/nucleo_f411re/ws2812b_dma</option>
</options>
<modules>
<module>modm:driver:ws2812</module>
<module>modm:platform:spi:1</module>
<module>modm:platform:dma</module>
<module>modm:ui:led</module>
<module>modm:build:scons</module>
<module>modm:processing:protothread</module>
<module>modm:processing:timer</module>
</modules>
</library>
1 change: 1 addition & 0 deletions src/modm/driver/pwm/ws2812.lb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def prepare(module, options):
":architecture:spi",
":architecture:unaligned",
":math:units",
":processing:resumable",
":ui:color")
return options[":target"].identifier.platform == "stm32"

Expand Down
6 changes: 4 additions & 2 deletions src/modm/driver/pwm/ws2812b.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace modm

/// @ingroup modm_driver_ws2812
template< class SpiMaster, class Output, size_t LEDs >
class Ws2812b
class Ws2812b : protected modm::NestedResumable<3>
{
protected: // 7654 3210 7654 3210 7654 3210
static constexpr uint32_t base_mask = 0b0010'0100'1001'0010'0100'1001;
Expand Down Expand Up @@ -104,7 +104,9 @@ class Ws2812b
modm::ResumableResult<void>
write()
{
return SpiMaster::transfer(data, nullptr, length+1);
RF_BEGIN();
RF_CALL(SpiMaster::transfer(data, nullptr, length+1));
RF_END_RETURN();
}
};

Expand Down

0 comments on commit 367dc71

Please sign in to comment.