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