libyggdrasil  v1.0.0
rgb_matrix.hpp
Go to the documentation of this file.
1  /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * _____.___. .___ .__.__ *
3  * \__ | | ____ ____ __| _/___________ _____|__| | *
4  * / | |/ ___\ / ___\ / __ |\_ __ \__ \ / ___/ | | *
5  * \____ / /_/ > /_/ > /_/ | | | \// __ \_\___ \| | |__ *
6  * / ______\___ /\___ /\____ | |__| (____ /____ >__|____/ *
7  * \/ /_____//_____/ \/ \/ \/ *
8  * - Yggdrasil - *
9  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
10  * This software can be used by students and other personal of the *
11  * Bern University of Applied Sciences under the terms of the MIT *
12  * license. *
13  * For other persons this software is under the terms of the GNU *
14  * General Public License version 2. *
15  * *
16  * Copyright © 2021, Bern University of Applied Sciences. *
17  * All rights reserved. *
18  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
26 #pragma once
27 
29 #include <cpp/common/types.hpp>
30 #include <cpp/common/utils.hpp>
31 
32 #include <array>
33 
34 namespace bsp::ygg::prph {
35 
40  class RGBMatrix {
41  private:
42  constexpr static inline u8 NumLEDs = 9;
43 
44  public:
45  RGBMatrix() = delete;
46 
52  static bool init() {
53  RGBA8 color = {0};
54  for(u8 i = 0; i <= NumLEDs; i++){
55  setLed(i, color);
56  }
57  return true;
58  }
59 
63  static void enable() {
64  bsp::SPIA::setMode(bsp::drv::SPIMode::_3);
65  SK9822_EN = 1;
66  flush();
67  }
68 
72  static void disable() {
73  SK9822_EN = 0;
74  }
75 
79  static void clear() {
80  RGBA8 color = {0};
81  for(u8 i = 0; i <= NumLEDs; i++){
82  setLed(i, color);
83  }
84  }
85 
86 
93  static void setLed(u8 index, RGBA8 color) {
94  if (index >= 0 && index < NumLEDs){
95  RGBMatrix::s_LEDs[index * 4 + 0] = (0xE0 | color.a);
96  RGBMatrix::s_LEDs[index * 4 + 1] = color.b;
97  RGBMatrix::s_LEDs[index * 4 + 2] = color.g;
98  RGBMatrix::s_LEDs[index * 4 + 3] = color.r;
99  }
100  }
101 
108  static void setLedMasked(u16 enableMask, RGBA8 color) {
109  for(u16 index = 0; index < NumLEDs; index++){
110  if(((enableMask >> index) & 0b1) == 0b1){
111  setLed(index, color);
112  } else {
113  setLed(index, { 0x0000'0000 });
114  }
115  }
116  }
117 
124  static void dice(u8 number, RGBA8 color) {
125  clear();
126 
127  switch (number) {
128  case 1: setLedMasked(0b000'010'000, color); break;
129  case 2: setLedMasked(0b001'000'100, color); break;
130  case 3: setLedMasked(0b001'010'100, color); break;
131  case 4: setLedMasked(0b101'000'101, color); break;
132  case 5: setLedMasked(0b101'010'101, color); break;
133  case 6: setLedMasked(0b101'101'101, color); break;
134  }
135  }
136 
140  static void flush(){
141  sendStartFrame();
142  bsp::SPIA::write(RGBMatrix::s_LEDs);
143  sendEndFrame();
144  }
145 
146  private:
147  static inline std::array<u8, NumLEDs * 4> s_LEDs;
148 
152  static inline void sendStartFrame() {
153  bsp::SPIA::write<std::array<u8, 4>>({ 0x00, 0x00, 0x00, 0x00 });
154  }
155 
159  static inline void sendEndFrame() {
160  bsp::SPIA::write<std::array<u8, 4>>({ 0xFF, 0xFF, 0xFF, 0xFF });
161  }
162 
163  };
164 
165 }
bsp::ygg::RGBA8
RGBA8 color type.
Definition: types.hpp:33
u16
uint16_t u16
Definition: types.h:37
utils.hpp
Commonly used helper functions.
u8
uint8_t u8
Unsigned integer definitions.
Definition: types.h:36
bsp::ygg::prph::RGBMatrix::disable
static void disable()
Disable the SK9822 led.
Definition: rgb_matrix.hpp:72
bsp::ygg::prph::RGBMatrix::RGBMatrix
RGBMatrix()=delete
types.hpp
Commonly used type definitions and helper templates.
bsp::ygg::RGBA8::b
u8 b
Definition: types.hpp:35
bsp::ygg::prph::RGBMatrix::init
static bool init()
Initialization function.
Definition: rgb_matrix.hpp:52
bsp::ygg::RGBA8::r
u8 r
Definition: types.hpp:35
bsp::ygg::prph::RGBMatrix::setLed
static void setLed(u8 index, RGBA8 color)
set a single led to RGBA8 color
Definition: rgb_matrix.hpp:93
bsp::ygg::prph::RGBMatrix::flush
static void flush()
Send the saved color values to the leds.
Definition: rgb_matrix.hpp:140
bsp::ygg::prph::RGBMatrix::enable
static void enable()
Enables the SK9822 led.
Definition: rgb_matrix.hpp:63
bsp::ygg::RGBA8::g
u8 g
Definition: types.hpp:35
bsp::ygg::prph::RGBMatrix::clear
static void clear()
Clear all leds.
Definition: rgb_matrix.hpp:79
bsp::ygg::prph::RGBMatrix
RGB Led driver SK9822 through SPI.
Definition: rgb_matrix.hpp:40
attributes.hpp
Commonly used C++ and GNU attributes.
bsp::ygg::prph
Definition: color_sensor.hpp:32
bsp::drv::SPIMode::_0
@ _0
CPOL = 0 | CPHA = 0.
bsp::ygg::prph::RGBMatrix::setLedMasked
static void setLedMasked(u16 enableMask, RGBA8 color)
set multiple leds to RGBA8 color using a bit mask
Definition: rgb_matrix.hpp:108
bsp::ygg::RGBA8::a
u8 a
Definition: types.hpp:35