Blame view

RIOT/drivers/lpd8808/lpd8808.c 1.68 KB
fb11e647   vrobic   reseau statique a...
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
  /*
   * Copyright (C) 2016 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     lpd8808
   * @{
   *
   * @file
   * @brief       LPD8808 based LED strip driver implementation
   *
   * @author      Hauke Petersen <mail@haukepetersen.de>
   *
   * @}
   */
  
  #include <string.h>
  
  #include "lpd8808.h"
  
  /**
   * @brief   Shift a single byte to the strip
   *
   * @param[in] dev   device to use
   * @param[in] d     byte to shift out
   */
  static void put_byte(lpd8808_t *dev, uint8_t d)
  {
      for (int i = 0; i < 8; i++) {
          gpio_write(dev->pin_dat, d & 0x80);
          gpio_set(dev->pin_clk);
          gpio_clear(dev->pin_clk);
          d <<= 1;
      }
  }
  
  /**
   * @brief   Flush the previous input
   *
   * LPD8808 based strips need to be flushed after loading values for each LED.
   * This is done by feeding the strip with one zero byte for every 32 LEDs on
   * the strip.
   *
   * @param[in] dev   device to flush
   */
  static void flush(lpd8808_t *dev)
  {
      for (int i = 0; i < ((dev->led_cnt + 31) / 32); i++) {
          put_byte(dev, 0);
      }
  }
  
  int lpd8808_init(lpd8808_t *dev, const lpd8808_params_t *params)
  {
      memcpy(dev, params, sizeof(lpd8808_params_t));
  
      /* initialize pins */
      gpio_init(dev->pin_dat, GPIO_OUT);
      gpio_init(dev->pin_clk, GPIO_OUT);
      flush(dev);
  
      return 0;
  }
  
  void lpd8808_load_rgb(lpd8808_t *dev, color_rgb_t vals[])
  {
      for (int i = 0; i < dev->led_cnt; i++) {
          put_byte(dev, ((vals[i].g >> 1) | 0x80));
          put_byte(dev, ((vals[i].r >> 1) | 0x80));
          put_byte(dev, ((vals[i].b >> 1) | 0x80));
      }
      flush(dev);
  }