mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
tests: added test app for apa102 devices
This commit is contained in:
parent
9bde34df33
commit
ca4ee66867
8
tests/driver_apa102/Makefile
Normal file
8
tests/driver_apa102/Makefile
Normal file
@ -0,0 +1,8 @@
|
||||
APPLICATION = driver_apa102
|
||||
include ../Makefile.tests_common
|
||||
|
||||
USEMODULE += apa102
|
||||
USEMODULE += color
|
||||
USEMODULE += xtimer
|
||||
|
||||
include $(RIOTBASE)/Makefile.include
|
21
tests/driver_apa102/README.md
Normal file
21
tests/driver_apa102/README.md
Normal file
@ -0,0 +1,21 @@
|
||||
# About
|
||||
This test application is made for verification of the APA102 LED strip driver.
|
||||
|
||||
# Usage
|
||||
Connect a APA102 based LED strip to a board of your choice, build, and flash
|
||||
this application. When run, you should see the strip changing its brightness
|
||||
while lighting in red, then green, and then blue. After this sequence, you
|
||||
should see a light moving up and down the strip, changing its color.
|
||||
|
||||
|
||||
You might need to adjust the default parameters (number of LEDs on the strip and
|
||||
pin configuration). You can do this simply by pre-setting the `CFLAGS`
|
||||
environment variable, e.g.:
|
||||
```
|
||||
$ CFLAGS="-DAPA102_PARAM_LED_NUMOF=78"" make all
|
||||
```
|
||||
|
||||
To change the default pins, simply override the default parameters, e.g.:
|
||||
```
|
||||
$ CFLAGS="-DAPA102_PARAM_DATA_PIN=GPIO_PIN\(2,3\) -DAPA102_PARAM_CLK_PIN=GPIO_PIN\(1,17\)" make all
|
||||
```
|
139
tests/driver_apa102/main.c
Normal file
139
tests/driver_apa102/main.c
Normal file
@ -0,0 +1,139 @@
|
||||
/*
|
||||
* Copyright (C) 2017 Freie Universität Berlin
|
||||
*
|
||||
* This file is subject to the terms and conditions of the GNU Lesser
|
||||
* General Public License v2.1. See the file LICENSE in the top level
|
||||
* directory for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @ingroup tests
|
||||
* @{
|
||||
*
|
||||
* @file
|
||||
* @brief Test application for the APA102 LED strip driver
|
||||
*
|
||||
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
||||
*
|
||||
* @}
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "xtimer.h"
|
||||
#include "color.h"
|
||||
|
||||
#include "apa102.h"
|
||||
#include "apa102_params.h"
|
||||
|
||||
/**
|
||||
* @brief Switch to the next LED every 10ms
|
||||
*/
|
||||
#define STEP (200 * US_PER_MS)
|
||||
|
||||
/**
|
||||
* @brief Interval for dimming colors
|
||||
*/
|
||||
#define DIM (100 * US_PER_MS)
|
||||
|
||||
/**
|
||||
* @brief Step through brightness levels (0-255) in 32 steps
|
||||
*/
|
||||
#define BSTEP (8U)
|
||||
|
||||
/**
|
||||
* @brief Allocate the device descriptor
|
||||
*/
|
||||
static apa102_t dev;
|
||||
|
||||
/**
|
||||
* @brief Allocate a color_rgb_t struct for each LED on the strip
|
||||
*/
|
||||
static color_rgba_t leds[APA102_PARAM_LED_NUMOF];
|
||||
|
||||
static void setcolor(int color, uint8_t alpha)
|
||||
{
|
||||
for (int i = 0; i < (int)APA102_PARAM_LED_NUMOF; i++) {
|
||||
leds[i].alpha = alpha;
|
||||
memset(&leds[i].color, 0, sizeof(color_rgb_t));
|
||||
switch (color) {
|
||||
case 0:
|
||||
leds[i].color.r = 255;
|
||||
break;
|
||||
case 1:
|
||||
leds[i].color.g = 255;
|
||||
break;
|
||||
case 2:
|
||||
leds[i].color.b = 255;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int pos = 0;
|
||||
int step = 1;
|
||||
color_hsv_t col = { 0.0, 1.0, 1.0 };
|
||||
|
||||
puts("APA102 Test App");
|
||||
|
||||
/* initialize all LED color values to black (off) */
|
||||
memset(leds, 0, sizeof(color_rgba_t) * APA102_PARAM_LED_NUMOF);
|
||||
|
||||
/* initialize the driver */
|
||||
apa102_init(&dev, &apa102_params[0]);
|
||||
|
||||
puts("Initialization done.");
|
||||
|
||||
/* set to each red, green, and blue, and fade each color in and out */
|
||||
for (int col = 0; col <= 2; col++) {
|
||||
int i = 0;
|
||||
for (; i <= (int)UINT8_MAX; i += BSTEP) {
|
||||
setcolor(col, (uint8_t)i);
|
||||
apa102_load_rgba(&dev, leds);
|
||||
xtimer_usleep(DIM);
|
||||
}
|
||||
i -= BSTEP;
|
||||
for (; i >= 0; i -= BSTEP) {
|
||||
setcolor(col, (uint8_t)i);
|
||||
apa102_load_rgba(&dev, leds);
|
||||
xtimer_usleep(DIM);
|
||||
}
|
||||
}
|
||||
|
||||
puts("Color Fading done");
|
||||
|
||||
/* reset color values */
|
||||
setcolor(-1, 255);
|
||||
apa102_load_rgba(&dev, leds);
|
||||
|
||||
xtimer_ticks32_t now = xtimer_now();
|
||||
|
||||
while (1) {
|
||||
/* change the active color by running around the hue circle */
|
||||
col.h += 1.0;
|
||||
if (col.h > 360.0) {
|
||||
col.h = 0.0;
|
||||
}
|
||||
|
||||
/* set the active LED to the active color value */
|
||||
memset(&leds[pos].color, 0, sizeof(color_rgb_t));
|
||||
pos += step;
|
||||
color_hsv2rgb(&col, &leds[pos].color);
|
||||
|
||||
/* apply the values to the LED strip */
|
||||
apa102_load_rgba(&dev, leds);
|
||||
|
||||
/* switch direction once reaching an end of the strip */
|
||||
if ((pos == (APA102_PARAM_LED_NUMOF - 1)) || (pos == 0)) {
|
||||
step *= -1;
|
||||
}
|
||||
|
||||
xtimer_periodic_wakeup(&now, STEP);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user