test
Dependencies: mbed AsyncBuzzerLib X_NUCLEO_53L1A1_mbed WS2812
LED_WS2812/Pixel/PixelArray.cpp@0:89b87662e64b, 2020-09-29 (annotated)
- Committer:
- elab
- Date:
- Tue Sep 29 09:08:55 2020 +0000
- Revision:
- 0:89b87662e64b
first version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elab | 0:89b87662e64b | 1 | #include "PixelArray.h" |
elab | 0:89b87662e64b | 2 | |
elab | 0:89b87662e64b | 3 | PixelArray::PixelArray(int size) |
elab | 0:89b87662e64b | 4 | { |
elab | 0:89b87662e64b | 5 | pbufsize = size; |
elab | 0:89b87662e64b | 6 | pbuf = new int[pbufsize]; |
elab | 0:89b87662e64b | 7 | SetAll(0x0); // initialise memory to zeros |
elab | 0:89b87662e64b | 8 | |
elab | 0:89b87662e64b | 9 | } |
elab | 0:89b87662e64b | 10 | |
elab | 0:89b87662e64b | 11 | PixelArray::~PixelArray() |
elab | 0:89b87662e64b | 12 | { |
elab | 0:89b87662e64b | 13 | delete[] pbuf; |
elab | 0:89b87662e64b | 14 | } |
elab | 0:89b87662e64b | 15 | |
elab | 0:89b87662e64b | 16 | void PixelArray::SetAll(unsigned int value) |
elab | 0:89b87662e64b | 17 | { |
elab | 0:89b87662e64b | 18 | // for each pixel |
elab | 0:89b87662e64b | 19 | for (int i=0 ; i < pbufsize; i++) { |
elab | 0:89b87662e64b | 20 | __set_pixel(i,value); |
elab | 0:89b87662e64b | 21 | } |
elab | 0:89b87662e64b | 22 | } |
elab | 0:89b87662e64b | 23 | |
elab | 0:89b87662e64b | 24 | |
elab | 0:89b87662e64b | 25 | void PixelArray::SetAllI(unsigned char value) |
elab | 0:89b87662e64b | 26 | { |
elab | 0:89b87662e64b | 27 | // for each pixel |
elab | 0:89b87662e64b | 28 | for (int i=0 ; i < pbufsize; i++) { |
elab | 0:89b87662e64b | 29 | __set_pixel_component(i,3,value); |
elab | 0:89b87662e64b | 30 | } |
elab | 0:89b87662e64b | 31 | } |
elab | 0:89b87662e64b | 32 | |
elab | 0:89b87662e64b | 33 | |
elab | 0:89b87662e64b | 34 | |
elab | 0:89b87662e64b | 35 | void PixelArray::SetAllR(unsigned char value) |
elab | 0:89b87662e64b | 36 | { |
elab | 0:89b87662e64b | 37 | // for each pixel |
elab | 0:89b87662e64b | 38 | for (int i=0 ; i < pbufsize; i++) { |
elab | 0:89b87662e64b | 39 | __set_pixel_component(i,2,value); |
elab | 0:89b87662e64b | 40 | } |
elab | 0:89b87662e64b | 41 | } |
elab | 0:89b87662e64b | 42 | |
elab | 0:89b87662e64b | 43 | void PixelArray::SetAllG(unsigned char value) |
elab | 0:89b87662e64b | 44 | { |
elab | 0:89b87662e64b | 45 | // for each pixel |
elab | 0:89b87662e64b | 46 | for (int i=0 ; i < pbufsize; i++) { |
elab | 0:89b87662e64b | 47 | __set_pixel_component(i,1,value); |
elab | 0:89b87662e64b | 48 | } |
elab | 0:89b87662e64b | 49 | } |
elab | 0:89b87662e64b | 50 | |
elab | 0:89b87662e64b | 51 | void PixelArray::SetAllB(unsigned char value) |
elab | 0:89b87662e64b | 52 | { |
elab | 0:89b87662e64b | 53 | // for each pixel |
elab | 0:89b87662e64b | 54 | for (int i=0 ; i < pbufsize; i++) { |
elab | 0:89b87662e64b | 55 | __set_pixel_component(i,0,value); |
elab | 0:89b87662e64b | 56 | } |
elab | 0:89b87662e64b | 57 | } |
elab | 0:89b87662e64b | 58 | |
elab | 0:89b87662e64b | 59 | |
elab | 0:89b87662e64b | 60 | |
elab | 0:89b87662e64b | 61 | |
elab | 0:89b87662e64b | 62 | |
elab | 0:89b87662e64b | 63 | void PixelArray::Set(int i, unsigned int value) |
elab | 0:89b87662e64b | 64 | { |
elab | 0:89b87662e64b | 65 | if ((i >= 0) && (i < pbufsize)) { |
elab | 0:89b87662e64b | 66 | __set_pixel(i,value); |
elab | 0:89b87662e64b | 67 | } |
elab | 0:89b87662e64b | 68 | } |
elab | 0:89b87662e64b | 69 | |
elab | 0:89b87662e64b | 70 | |
elab | 0:89b87662e64b | 71 | |
elab | 0:89b87662e64b | 72 | void PixelArray::SetI(int i, unsigned char value) |
elab | 0:89b87662e64b | 73 | { |
elab | 0:89b87662e64b | 74 | if ((i >= 0) && (i < pbufsize)) { |
elab | 0:89b87662e64b | 75 | __set_pixel_component(i,3,value); |
elab | 0:89b87662e64b | 76 | } |
elab | 0:89b87662e64b | 77 | } |
elab | 0:89b87662e64b | 78 | |
elab | 0:89b87662e64b | 79 | |
elab | 0:89b87662e64b | 80 | void PixelArray::SetR(int i, unsigned char value) |
elab | 0:89b87662e64b | 81 | { |
elab | 0:89b87662e64b | 82 | if ((i >= 0) && (i < pbufsize)) { |
elab | 0:89b87662e64b | 83 | __set_pixel_component(i,2,value); |
elab | 0:89b87662e64b | 84 | } |
elab | 0:89b87662e64b | 85 | } |
elab | 0:89b87662e64b | 86 | |
elab | 0:89b87662e64b | 87 | void PixelArray::SetG(int i, unsigned char value) |
elab | 0:89b87662e64b | 88 | { |
elab | 0:89b87662e64b | 89 | if ((i >= 0) && (i < pbufsize)) { |
elab | 0:89b87662e64b | 90 | __set_pixel_component(i,1,value); |
elab | 0:89b87662e64b | 91 | } |
elab | 0:89b87662e64b | 92 | } |
elab | 0:89b87662e64b | 93 | |
elab | 0:89b87662e64b | 94 | void PixelArray::SetB(int i, unsigned char value) |
elab | 0:89b87662e64b | 95 | { |
elab | 0:89b87662e64b | 96 | if ((i >= 0) && (i < pbufsize)) { |
elab | 0:89b87662e64b | 97 | __set_pixel_component(i,0,value); |
elab | 0:89b87662e64b | 98 | } |
elab | 0:89b87662e64b | 99 | } |
elab | 0:89b87662e64b | 100 | |
elab | 0:89b87662e64b | 101 | |
elab | 0:89b87662e64b | 102 | unsigned int PixelArray::Get(int i) |
elab | 0:89b87662e64b | 103 | { |
elab | 0:89b87662e64b | 104 | return __get_pixel(i); |
elab | 0:89b87662e64b | 105 | } |
elab | 0:89b87662e64b | 106 | |
elab | 0:89b87662e64b | 107 | unsigned int PixelArray::GetI(int i) |
elab | 0:89b87662e64b | 108 | { |
elab | 0:89b87662e64b | 109 | return __get_pixel_component(i,3); |
elab | 0:89b87662e64b | 110 | } |
elab | 0:89b87662e64b | 111 | |
elab | 0:89b87662e64b | 112 | unsigned int PixelArray::GetR(int i) |
elab | 0:89b87662e64b | 113 | { |
elab | 0:89b87662e64b | 114 | return __get_pixel_component(i,2); |
elab | 0:89b87662e64b | 115 | } |
elab | 0:89b87662e64b | 116 | |
elab | 0:89b87662e64b | 117 | unsigned int PixelArray::GetG(int i) |
elab | 0:89b87662e64b | 118 | { |
elab | 0:89b87662e64b | 119 | return __get_pixel_component(i,1); |
elab | 0:89b87662e64b | 120 | } |
elab | 0:89b87662e64b | 121 | |
elab | 0:89b87662e64b | 122 | unsigned int PixelArray::GetB(int i) |
elab | 0:89b87662e64b | 123 | { |
elab | 0:89b87662e64b | 124 | return __get_pixel_component(i,0); |
elab | 0:89b87662e64b | 125 | } |
elab | 0:89b87662e64b | 126 | |
elab | 0:89b87662e64b | 127 | |
elab | 0:89b87662e64b | 128 | |
elab | 0:89b87662e64b | 129 | int* PixelArray::getBuf() |
elab | 0:89b87662e64b | 130 | { |
elab | 0:89b87662e64b | 131 | return (pbuf); |
elab | 0:89b87662e64b | 132 | } |
elab | 0:89b87662e64b | 133 | |
elab | 0:89b87662e64b | 134 | |
elab | 0:89b87662e64b | 135 | // set either the I,R,G,B value of specific pixel channel |
elab | 0:89b87662e64b | 136 | void PixelArray::__set_pixel_component(int index, int channel, int value) |
elab | 0:89b87662e64b | 137 | { |
elab | 0:89b87662e64b | 138 | |
elab | 0:89b87662e64b | 139 | // AND with 0x00 shifted to the right location to clear the bits |
elab | 0:89b87662e64b | 140 | pbuf[index] &= ~(0xFF << (8 * channel)); |
elab | 0:89b87662e64b | 141 | |
elab | 0:89b87662e64b | 142 | // Set the bits with an OR |
elab | 0:89b87662e64b | 143 | pbuf[index] |= (value << (8 * channel)); |
elab | 0:89b87662e64b | 144 | } |
elab | 0:89b87662e64b | 145 | |
elab | 0:89b87662e64b | 146 | |
elab | 0:89b87662e64b | 147 | // set either the I,R,G,B value of specific pixel channel |
elab | 0:89b87662e64b | 148 | void PixelArray::__set_pixel(int index, int value) |
elab | 0:89b87662e64b | 149 | { |
elab | 0:89b87662e64b | 150 | // AND with 0x00 shifted to the right location to clear the bits |
elab | 0:89b87662e64b | 151 | pbuf[index] = value; |
elab | 0:89b87662e64b | 152 | } |
elab | 0:89b87662e64b | 153 | |
elab | 0:89b87662e64b | 154 | |
elab | 0:89b87662e64b | 155 | // get specific pixel |
elab | 0:89b87662e64b | 156 | int PixelArray::__get_pixel(int index) |
elab | 0:89b87662e64b | 157 | { |
elab | 0:89b87662e64b | 158 | if ((index >= 0) && (index < pbufsize)) { |
elab | 0:89b87662e64b | 159 | return pbuf[index]; |
elab | 0:89b87662e64b | 160 | } else { |
elab | 0:89b87662e64b | 161 | return 0; |
elab | 0:89b87662e64b | 162 | } |
elab | 0:89b87662e64b | 163 | } |
elab | 0:89b87662e64b | 164 | |
elab | 0:89b87662e64b | 165 | |
elab | 0:89b87662e64b | 166 | // get specific pixel |
elab | 0:89b87662e64b | 167 | int PixelArray::__get_pixel_component(int index, int channel) |
elab | 0:89b87662e64b | 168 | { |
elab | 0:89b87662e64b | 169 | // AND with 0xFF shifted to the right location to get the bits |
elab | 0:89b87662e64b | 170 | return ( (__get_pixel(index) & (0xFF << (8 * channel))) >> (8*channel) ); |
elab | 0:89b87662e64b | 171 | } |
elab | 0:89b87662e64b | 172 | |
elab | 0:89b87662e64b | 173 |