Blame view

RIOT/tests/driver_apa102/main.c 3.15 KB
a752c7ab   elopes   add first test an...
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
136
137
138
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;
  }