Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed DebounceIn WS2812
PixelArray.cpp
00001 #include "PixelArray.h" 00002 00003 PixelArray::PixelArray(int size) 00004 { 00005 pbufsize = size; 00006 pbuf = new int[pbufsize]; 00007 SetAll(0x0); // initialise memory to zeros 00008 00009 } 00010 00011 PixelArray::~PixelArray () 00012 { 00013 delete[] pbuf; 00014 } 00015 00016 void PixelArray::SetAll(unsigned int value) 00017 { 00018 // for each pixel 00019 for (int i=0 ; i < pbufsize; i++) { 00020 __set_pixel(i,value); 00021 } 00022 } 00023 00024 00025 void PixelArray::SetAllI(unsigned char value) 00026 { 00027 // for each pixel 00028 for (int i=0 ; i < pbufsize; i++) { 00029 __set_pixel_component(i,3,value); 00030 } 00031 } 00032 00033 00034 00035 void PixelArray::SetAllR(unsigned char value) 00036 { 00037 // for each pixel 00038 for (int i=0 ; i < pbufsize; i++) { 00039 __set_pixel_component(i,2,value); 00040 } 00041 } 00042 00043 void PixelArray::SetAllG(unsigned char value) 00044 { 00045 // for each pixel 00046 for (int i=0 ; i < pbufsize; i++) { 00047 __set_pixel_component(i,1,value); 00048 } 00049 } 00050 00051 void PixelArray::SetAllB(unsigned char value) 00052 { 00053 // for each pixel 00054 for (int i=0 ; i < pbufsize; i++) { 00055 __set_pixel_component(i,0,value); 00056 } 00057 } 00058 00059 00060 00061 00062 00063 void PixelArray::Set(int i, unsigned int value) 00064 { 00065 if ((i >= 0) && (i < pbufsize)) { 00066 __set_pixel(i,value); 00067 } 00068 } 00069 00070 00071 00072 void PixelArray::SetI(int i, unsigned char value) 00073 { 00074 if ((i >= 0) && (i < pbufsize)) { 00075 __set_pixel_component(i,3,value); 00076 } 00077 } 00078 00079 00080 void PixelArray::SetR(int i, unsigned char value) 00081 { 00082 if ((i >= 0) && (i < pbufsize)) { 00083 __set_pixel_component(i,2,value); 00084 } 00085 } 00086 00087 void PixelArray::SetG(int i, unsigned char value) 00088 { 00089 if ((i >= 0) && (i < pbufsize)) { 00090 __set_pixel_component(i,1,value); 00091 } 00092 } 00093 00094 void PixelArray::SetB(int i, unsigned char value) 00095 { 00096 if ((i >= 0) && (i < pbufsize)) { 00097 __set_pixel_component(i,0,value); 00098 } 00099 } 00100 00101 00102 unsigned int PixelArray::Get(int i) 00103 { 00104 return __get_pixel(i); 00105 } 00106 00107 unsigned int PixelArray::GetI(int i) 00108 { 00109 return __get_pixel_component(i,3); 00110 } 00111 00112 unsigned int PixelArray::GetR(int i) 00113 { 00114 return __get_pixel_component(i,2); 00115 } 00116 00117 unsigned int PixelArray::GetG(int i) 00118 { 00119 return __get_pixel_component(i,1); 00120 } 00121 00122 unsigned int PixelArray::GetB(int i) 00123 { 00124 return __get_pixel_component(i,0); 00125 } 00126 00127 00128 00129 int* PixelArray::getBuf() 00130 { 00131 return (pbuf); 00132 } 00133 00134 00135 // set either the I,R,G,B value of specific pixel channel 00136 void PixelArray::__set_pixel_component(int index, int channel, int value) 00137 { 00138 00139 // AND with 0x00 shifted to the right location to clear the bits 00140 pbuf[index] &= ~(0xFF << (8 * channel)); 00141 00142 // Set the bits with an OR 00143 pbuf[index] |= (value << (8 * channel)); 00144 } 00145 00146 00147 // set either the I,R,G,B value of specific pixel channel 00148 void PixelArray::__set_pixel(int index, int value) 00149 { 00150 // AND with 0x00 shifted to the right location to clear the bits 00151 pbuf[index] = value; 00152 } 00153 00154 00155 // get specific pixel 00156 int PixelArray::__get_pixel(int index) 00157 { 00158 if ((index >= 0) && (index < pbufsize)) { 00159 return pbuf[index]; 00160 } else { 00161 return 0; 00162 } 00163 } 00164 00165 00166 // get specific pixel 00167 int PixelArray::__get_pixel_component(int index, int channel) 00168 { 00169 // AND with 0xFF shifted to the right location to get the bits 00170 return ( (__get_pixel(index) & (0xFF << (8 * channel))) >> (8*channel) ); 00171 } 00172 00173
Generated on Sat Jul 16 2022 08:24:20 by
1.7.2