Skip to content

Commit 367dc71

Browse files
committed
[ws2812] resumable function example and bug
1 parent 9e5e77e commit 367dc71

File tree

4 files changed

+105
-2
lines changed

4 files changed

+105
-2
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright (c) 2019, Niklas Hauser
3+
*
4+
* This file is part of the modm project.
5+
*
6+
* This Source Code Form is subject to the terms of the Mozilla Public
7+
* License, v. 2.0. If a copy of the MPL was not distributed with this
8+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
// ----------------------------------------------------------------------------
11+
12+
#include <modm/board.hpp>
13+
#include <modm/driver/pwm/ws2812b.hpp>
14+
#include <modm/ui/led/tables.hpp>
15+
#include <modm/processing/timer.hpp>
16+
#include <modm/processing.hpp>
17+
18+
using namespace Board;
19+
20+
using Output = Board::D11;
21+
using DmaRx = Dma2::Channel0;
22+
using DmaTx = Dma2::Channel3;
23+
using SpiLed = SpiMaster1_Dma<DmaRx, DmaTx>;
24+
modm::Ws2812b<SpiLed, Output, 8*8> leds;
25+
modm::ShortPeriodicTimer tmr{33ms};
26+
27+
constexpr uint8_t max = 62;
28+
uint8_t r=0, g=max/3, b=max/3*2;
29+
30+
class Ws2812Thread : public modm::pt::Protothread
31+
{
32+
public:
33+
34+
bool
35+
update()
36+
{
37+
PT_BEGIN();
38+
39+
LedD13::setOutput();
40+
Dma2::enable();
41+
leds.initialize<Board::SystemClock>();
42+
43+
while (true)
44+
{
45+
for (size_t ii=0; ii < leds.size; ii++)
46+
{
47+
leds.setColor(ii,
48+
{modm::ui::table22_8_256[r*3/2],
49+
modm::ui::table22_8_256[g*3/2],
50+
modm::ui::table22_8_256[b*3/2]});
51+
if (r++ >= max) r = 0;
52+
if (g++ >= max) g = 0;
53+
if (b++ >= max) b = 0;
54+
}
55+
PT_CALL(leds.write());
56+
57+
LedD13::toggle();
58+
timeout.restart(500ms);
59+
PT_WAIT_UNTIL(timeout.isExpired());
60+
}
61+
62+
PT_END();
63+
}
64+
65+
private:
66+
modm::ShortTimeout timeout;
67+
};
68+
69+
Ws2812Thread ws2812_thread;
70+
71+
int
72+
main()
73+
{
74+
Board::initialize();
75+
76+
77+
78+
while (true)
79+
{
80+
ws2812_thread.update();
81+
}
82+
83+
return 0;
84+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
2+
<library>
3+
<extends>modm:nucleo-f411re</extends>
4+
<options>
5+
<option name=":build:build.path">../../../build/nucleo_f411re/ws2812b_dma</option>
6+
</options>
7+
<modules>
8+
<module>modm:driver:ws2812</module>
9+
<module>modm:platform:spi:1</module>
10+
<module>modm:platform:dma</module>
11+
<module>modm:ui:led</module>
12+
<module>modm:build:scons</module>
13+
<module>modm:processing:protothread</module>
14+
<module>modm:processing:timer</module>
15+
</modules>
16+
</library>

src/modm/driver/pwm/ws2812.lb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def prepare(module, options):
4343
":architecture:spi",
4444
":architecture:unaligned",
4545
":math:units",
46+
":processing:resumable",
4647
":ui:color")
4748
return options[":target"].identifier.platform == "stm32"
4849

src/modm/driver/pwm/ws2812b.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ namespace modm
2121

2222
/// @ingroup modm_driver_ws2812
2323
template< class SpiMaster, class Output, size_t LEDs >
24-
class Ws2812b
24+
class Ws2812b : protected modm::NestedResumable<3>
2525
{
2626
protected: // 7654 3210 7654 3210 7654 3210
2727
static constexpr uint32_t base_mask = 0b0010'0100'1001'0010'0100'1001;
@@ -104,7 +104,9 @@ class Ws2812b
104104
modm::ResumableResult<void>
105105
write()
106106
{
107-
return SpiMaster::transfer(data, nullptr, length+1);
107+
RF_BEGIN();
108+
RF_CALL(SpiMaster::transfer(data, nullptr, length+1));
109+
RF_END_RETURN();
108110
}
109111
};
110112

0 commit comments

Comments
 (0)