PropWare  3.0.0.229
C++ objects and CMake build system for Parallax Propeller
libws2812.c
1 /*
2  * @file libws2812.c
3  *
4  * @author Parallax Inc.
5  *
6  * @version 0.85
7  *
8  * @copyright
9  * Copyright (c) Parallax Inc. 2014, All Rights MIT Licensed.
10  *
11  * @brief Test program for WS2812 RGB LED driver.
12  */
13 
14 #include <propeller.h>
15 #include "ws2812.h"
16 
17 // led chain
18 #define LED_PIN 13
19 #define LED_COUNT 4
20 
21 // 4 Parallax WS2812B Fun Boards
22 uint32_t ledColors[LED_COUNT];
23 
24 // LED driver state
25 ws2812 *driver;
26 
27 // pattern for chase
28 uint32_t pattern[] = {
29  COLOR_RED,
30  COLOR_ORANGE,
31  COLOR_YELLOW,
32  COLOR_GREEN,
33  COLOR_BLUE,
34  COLOR_INDIGO
35 };
36 
37 #define pattern_count (sizeof(pattern) / sizeof(pattern[0]))
38 
39 // ticks per millisecond
40 int ticks_per_ms;
41 
42 // forward declarations
43 void alternate(int count, int delay);
44 void chase(int count, int delay);
45 void pause(int ms);
46 
47 void main(void)
48 {
49  // calibrate the pause function
50  // CLKFREQ is the clock frequency of the processor
51  // typically this is 80mhz
52  // dividing by 1000 gives the number of clock ticks per millisecond
53  ticks_per_ms = CLKFREQ / 1000;
54 
55  // load the LED driver
56  if (!(driver = ws2812b_open()))
57  return;
58 
59  // repeat the patterns
60  for (;;) {
61 
62  // alternate inner and outer colors
63  alternate(8, 500);
64 
65  // chase
66  chase(32, 200);
67  }
68 
69  // close the driver
70  ws2812_close(driver);
71 }
72 
73 void alternate(int count, int delay)
74 {
75  // start with the outer two LEDs green and the inner two red
76  ledColors[0] = COLOR_GREEN;
77  ledColors[1] = COLOR_RED;
78  ledColors[2] = COLOR_RED;
79  ledColors[3] = COLOR_GREEN;
80 
81  // repeat count times or forever if count < 0
82  while (count < 0 || --count >= 0) {
83 
84  // swap the inner and outer colors
85  ledColors[0] = ledColors[1];
86  ledColors[1] = ledColors[3];
87  ledColors[2] = ledColors[3];
88  ledColors[3] = ledColors[0];
89 
90  // Set LEDs in the chain
91  ws2812_set(driver, LED_PIN, ledColors, LED_COUNT);
92 
93  // delay between frames
94  pause(delay);
95  }
96 }
97 
98 // the chase effect was translated from Spin code by Jon McPhalen
99 void chase(int count, int delay)
100 {
101  int base = 0;
102  int idx, i;
103 
104  // repeat count times or forever if count < 0
105  while (count < 0 || --count >= 0) {
106 
107  // fill the chain with the pattern
108  idx = base; // start at base
109  for (i = 0; i < LED_COUNT; ++i) { // loop through connected leds
110  ledColors[i] = pattern[idx]; // Set channel color
111  if (++idx >= pattern_count) // past end of list?
112  idx = 0; // yes, reset
113  }
114  if (++base >= pattern_count) // Set the base for the next time
115  base = 0;
116 
117  // Set LEDs in the chain
118  ws2812_set(driver, LED_PIN, ledColors, LED_COUNT);
119 
120  // delay between frames
121  pause(delay);
122  }
123 }
124 
125 void pause(int ms)
126 {
127  waitcnt(CNT + ms * ticks_per_ms);
128 }
129 
ws2812_t
Definition: ws2812.h:53
pause
void pause(int time)
Delay cog from moving on to the next statement for a certain length of time.
Definition: libws2812.c:125
ws2812.h
Driver for WS2812 and WS2812B RGB LEDs.
main
int main(void)
Definition: GraphicsTest.c:20
CLKFREQ
#define CLKFREQ
Returns the current clock frequency.
Definition: propeller.h:46
count
long count(int pin, long duration)
Count number of low to high transitions an external input applies to an I/O pin over a certain period...
Definition: count.c:19
ws2812_set
void ws2812_set(ws2812_t *driver, int pin, uint32_t *colors, int count)
Set color pattern on a chain of LEDs.
Definition: ws2812.c:56
waitcnt
#define waitcnt(a)
Wait until system counter reaches a value.
Definition: propeller.h:176
CNT
#define CNT
The system clock count.
Definition: propeller1.h:151
ms
int ms
Propeller system clock ticks in 1 millisecond (ms).
Definition: timeTicks.c:20
ws2812_close
void ws2812_close(ws2812_t *driver)
Close a WS2812 or WS2812B driver.
Definition: ws2812_close.c:17
ws2812b_open
ws2812_t * ws2812b_open(void)
Open a driver for WS2812B chips.
Definition: ws2812b_open.c:17