-
Notifications
You must be signed in to change notification settings - Fork 2
/
demo_rgbstrip.c
135 lines (110 loc) · 2.77 KB
/
demo_rgbstrip.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//#include <stdio.h>
//#include <string.h>
#include <stdbool.h>
#include <stdint.h>
#include "delay.h"
#include "nrf_gpio.h"
#include "random.h"
/*
* Color LEDs
*/
#define PIN_LED_RED 7
#define PIN_LED_GREEN 5
#define PIN_LED_BLUE 6
void setup_led_pins()
{
nrf_gpio_pin_dir_set(PIN_LED_GREEN, NRF_GPIO_PIN_DIR_OUTPUT);
nrf_gpio_pin_dir_set(PIN_LED_BLUE, NRF_GPIO_PIN_DIR_OUTPUT);
nrf_gpio_pin_dir_set(PIN_LED_RED, NRF_GPIO_PIN_DIR_OUTPUT);
}
#define on(led) nrf_gpio_pin_set(led)
#define off(led) nrf_gpio_pin_clear(led)
typedef struct
{
uint8_t red, green, blue;
} rgb_t;
/*
* One "pulse": Switch LEDs on, make 256 steps
*/
void PWM(rgb_t *pwm)
{
if (pwm->red > 0)
on(PIN_LED_RED);
if (pwm->green > 0)
on(PIN_LED_GREEN);
if (pwm->blue > 0)
on(PIN_LED_BLUE);
/*
* PWM with 100Hz:
* 256 steps in 10ms, 39us per step
*/
uint32_t i;
for (i=0; i<255; i++)
{
if (pwm->red == i)
off(PIN_LED_RED);
if (pwm->green == i)
off(PIN_LED_GREEN);
if (pwm->blue == i)
off(PIN_LED_BLUE);
delay_us(15); //39
}
}
#define abs(val) ((val) < 0 ? (-(val)) : (val))
/*
* Returns a random number between 0 and 255
*/
uint8_t random()
{
// wait until a random number is available
while (RNG_EVENT_VALRDY == 0)
delay_us(10);
// read random number
volatile uint8_t result = RNG_VALUE;
// restart random number generator
RNG_EVENT_VALRDY = 0;
RNG_START = 1;
return result;
}
int main()
{
setup_led_pins();
// current color
volatile rgb_t pwm;
// next color
volatile rgb_t color;
// enable random number generator
RNG_SHORTS = RNG_SHORTCUT_VALRDY_STOP;
RNG_START = 1;
while(true)
{
// fade towards color
while (color.red != pwm.red || color.green != pwm.green || color.blue != pwm.blue)
{
PWM(&pwm);
// for (uint8_t i=0; i<1; i++)
// {
if (color.red > pwm.red)
pwm.red++;
if (color.red < pwm.red)
pwm.red--;
if (color.green > pwm.green)
pwm.green++;
if (color.green < pwm.green)
pwm.green--;
if (color.blue > pwm.blue)
pwm.blue++;
if (color.blue < pwm.blue)
pwm.blue--;
// }
}
// display this color for a while
for (uint8_t i=0; i<100; i++)
PWM(&color);
// choose new, random color
color.red = random();
color.green = random();
color.blue = random();
}
return 0;
}