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