Statistics
| Branch: | Tag: | Revision:

amiro-os / components / leds / tlc5947.cpp @ a3c54343

History | View | Annotate | Download (2.407 KB)

1 58fe0e0b Thomas Schöpping
#include <ch.hpp>
2
#include <hal.h>
3
4
#include <amiro/bus/spi/HWSPIDriver.hpp>
5
#include <amiro/leds/tlc5947.hpp>
6
7
using namespace chibios_rt;
8
using namespace amiro;
9
10
TLC5947::TLC5947(HWSPIDriver *spi, ioportid_t blankPort, int blankPad)
11
    : BaseStaticThread<192>(),
12
      spi(spi),
13
      blankPort(blankPort),
14
      blankPad(blankPad),
15
      brightness(0x00u) {
16
  for (int i = 0; i < 8; i++)
17
    this->colors[i] = Color::BLACK;
18
}
19
20
TLC5947::
21
~TLC5947() {
22
23
}
24
25
void TLC5947::disable() {
26
  palWritePad(this->blankPort, this->blankPad, 1);
27
}
28
29
void TLC5947::enable() {
30
  palWritePad(this->blankPort, this->blankPad, 0);
31
}
32
33
void TLC5947::setBrightness(int brightness) {
34
  if (brightness < 0)
35
    this->brightness = 0;
36
  else if (brightness > 100)
37
    this->brightness = 100;
38
  else
39
    this->brightness = brightness;
40
}
41
42
void TLC5947::setColor(int index, Color color) {
43
  this->colors[index] = color;
44
}
45
46
void TLC5947::update() {
47
  this->signalEvents(static_cast<eventmask_t>(1));
48
}
49
50
msg_t TLC5947::main(void) {
51
  uint8_t buffer[36];
52
53
  this->setName("Tlc5947");
54
55
  while (!this->shouldTerminate()) {
56
    int brightness = this->brightness;
57
58
    for (int i = 0, j = 0; i < 8; i += 2) {
59
      Color color1 = this->colors[i];
60
      Color color2 = this->colors[i + 1];
61
62
      int values[6];
63
      values[0] = caluclateBlueGrayscale(color1, brightness);
64
      values[1] = caluclateGreenGrayscale(color1, brightness);
65
      values[2] = caluclateRedGrayscale(color1, brightness);
66
      values[3] = caluclateBlueGrayscale(color2, brightness);
67
      values[4] = caluclateGreenGrayscale(color2, brightness);
68
      values[5] = caluclateRedGrayscale(color2, brightness);
69
70
      for (int k = 0; k < 6; k += 2) {
71
        int value1 = values[k];
72
        int value2 = values[k + 1];
73
74
        buffer[j++] = static_cast<uint8_t>((value1 >> 4) & 0xff);
75
        buffer[j++] = static_cast<uint8_t>((value1 << 4) | ((value2 >> 8) & 0xff));
76
        buffer[j++] = static_cast<uint8_t>(value2 & 0xff);
77
      }
78
    }
79
    this->spi->write(buffer, 36);
80
81
    this->waitAnyEvent(ALL_EVENTS);
82
  }
83
84
  return true;
85
}
86
87
int TLC5947::caluclateRedGrayscale(Color color, int brightness) const {
88
  return color.getRed() * brightness * 16 / 100;
89
}
90
91
int TLC5947::caluclateGreenGrayscale(Color color, int brightness) const {
92
  return color.getGreen() * brightness * 16 / 100;
93
}
94
95
int TLC5947::caluclateBlueGrayscale(Color color, int brightness) const {
96
  return color.getBlue() * brightness * (16 * 3) / (100 * 4);
97
}