RGB Pixel Array with rendering features. Designed to be used with WS2812 library

Dependents:   ChrisRGB-Ring ChrisRGB-Ring WS2812_Example WS2812_Example_fade ... more

Committer:
chris
Date:
Wed Aug 06 08:23:13 2014 +0000
Revision:
1:41b9f8ec0b6a
Parent:
0:bdce2379bd67
Child:
2:b45a70faaa83
Added dynamic (compile time) buffer size

Who changed what in which revision?

UserRevisionLine numberNew contents of line
chris 0:bdce2379bd67 1 #include "PixelArray.h"
chris 0:bdce2379bd67 2
chris 0:bdce2379bd67 3 PixelArray::PixelArray(int size)
chris 0:bdce2379bd67 4 {
chris 1:41b9f8ec0b6a 5 pbufsize = size;
chris 1:41b9f8ec0b6a 6 pbuf = new int[pbufsize];
chris 1:41b9f8ec0b6a 7
chris 0:bdce2379bd67 8 }
chris 0:bdce2379bd67 9
chris 0:bdce2379bd67 10 PixelArray::~PixelArray()
chris 0:bdce2379bd67 11 {
chris 1:41b9f8ec0b6a 12 delete[] pbuf;
chris 0:bdce2379bd67 13 }
chris 0:bdce2379bd67 14
chris 0:bdce2379bd67 15 void PixelArray::SetAll(unsigned int value)
chris 0:bdce2379bd67 16 {
chris 0:bdce2379bd67 17 // for each pixel
chris 1:41b9f8ec0b6a 18 for (int i=0 ; i < pbufsize; i++) {
chris 0:bdce2379bd67 19 __set_pixel(i,value);
chris 0:bdce2379bd67 20 }
chris 0:bdce2379bd67 21 }
chris 0:bdce2379bd67 22
chris 0:bdce2379bd67 23
chris 0:bdce2379bd67 24 void PixelArray::SetAllI(unsigned char value)
chris 0:bdce2379bd67 25 {
chris 0:bdce2379bd67 26 // for each pixel
chris 1:41b9f8ec0b6a 27 for (int i=0 ; i < pbufsize; i++) {
chris 0:bdce2379bd67 28 __set_pixel_component(i,3,value);
chris 0:bdce2379bd67 29 }
chris 0:bdce2379bd67 30 }
chris 0:bdce2379bd67 31
chris 0:bdce2379bd67 32
chris 0:bdce2379bd67 33
chris 0:bdce2379bd67 34 void PixelArray::SetAllR(unsigned char value)
chris 0:bdce2379bd67 35 {
chris 0:bdce2379bd67 36 // for each pixel
chris 1:41b9f8ec0b6a 37 for (int i=0 ; i < pbufsize; i++) {
chris 0:bdce2379bd67 38 __set_pixel_component(i,2,value);
chris 0:bdce2379bd67 39 }
chris 0:bdce2379bd67 40 }
chris 0:bdce2379bd67 41
chris 0:bdce2379bd67 42 void PixelArray::SetAllG(unsigned char value)
chris 0:bdce2379bd67 43 {
chris 0:bdce2379bd67 44 // for each pixel
chris 1:41b9f8ec0b6a 45 for (int i=0 ; i < pbufsize; i++) {
chris 0:bdce2379bd67 46 __set_pixel_component(i,1,value);
chris 0:bdce2379bd67 47 }
chris 0:bdce2379bd67 48 }
chris 0:bdce2379bd67 49
chris 0:bdce2379bd67 50 void PixelArray::SetAllB(unsigned char value)
chris 0:bdce2379bd67 51 {
chris 0:bdce2379bd67 52 // for each pixel
chris 1:41b9f8ec0b6a 53 for (int i=0 ; i < pbufsize; i++) {
chris 0:bdce2379bd67 54 __set_pixel_component(i,0,value);
chris 0:bdce2379bd67 55 }
chris 0:bdce2379bd67 56 }
chris 0:bdce2379bd67 57
chris 0:bdce2379bd67 58
chris 0:bdce2379bd67 59
chris 0:bdce2379bd67 60
chris 0:bdce2379bd67 61
chris 0:bdce2379bd67 62 void PixelArray::Set(int i, unsigned int value)
chris 0:bdce2379bd67 63 {
chris 1:41b9f8ec0b6a 64 if ((i >= 0) && (i < pbufsize)) {
chris 0:bdce2379bd67 65 __set_pixel(i,value);
chris 0:bdce2379bd67 66 }
chris 0:bdce2379bd67 67 }
chris 0:bdce2379bd67 68
chris 0:bdce2379bd67 69
chris 0:bdce2379bd67 70
chris 0:bdce2379bd67 71 void PixelArray::SetI(int i, unsigned char value)
chris 0:bdce2379bd67 72 {
chris 1:41b9f8ec0b6a 73 if ((i >= 0) && (i < pbufsize)) {
chris 0:bdce2379bd67 74 __set_pixel_component(i,3,value);
chris 0:bdce2379bd67 75 }
chris 0:bdce2379bd67 76 }
chris 0:bdce2379bd67 77
chris 0:bdce2379bd67 78
chris 0:bdce2379bd67 79 void PixelArray::SetR(int i, unsigned char value)
chris 0:bdce2379bd67 80 {
chris 1:41b9f8ec0b6a 81 if ((i >= 0) && (i < pbufsize)) {
chris 0:bdce2379bd67 82 __set_pixel_component(i,2,value);
chris 0:bdce2379bd67 83 }
chris 0:bdce2379bd67 84 }
chris 0:bdce2379bd67 85
chris 0:bdce2379bd67 86 void PixelArray::SetG(int i, unsigned char value)
chris 0:bdce2379bd67 87 {
chris 1:41b9f8ec0b6a 88 if ((i >= 0) && (i < pbufsize)) {
chris 0:bdce2379bd67 89 __set_pixel_component(i,1,value);
chris 0:bdce2379bd67 90 }
chris 0:bdce2379bd67 91 }
chris 0:bdce2379bd67 92
chris 0:bdce2379bd67 93 void PixelArray::SetB(int i, unsigned char value)
chris 0:bdce2379bd67 94 {
chris 1:41b9f8ec0b6a 95 if ((i >= 0) && (i < pbufsize)) {
chris 0:bdce2379bd67 96 __set_pixel_component(i,0,value);
chris 0:bdce2379bd67 97 }
chris 0:bdce2379bd67 98 }
chris 0:bdce2379bd67 99
chris 0:bdce2379bd67 100
chris 0:bdce2379bd67 101 int* PixelArray::getBuf()
chris 0:bdce2379bd67 102 {
chris 1:41b9f8ec0b6a 103 return (pbuf);
chris 0:bdce2379bd67 104 }
chris 0:bdce2379bd67 105
chris 0:bdce2379bd67 106
chris 0:bdce2379bd67 107 // set either the I,R,G,B value of specific pixel channel
chris 0:bdce2379bd67 108 void PixelArray::__set_pixel_component(int index, int channel, int value)
chris 0:bdce2379bd67 109 {
chris 0:bdce2379bd67 110
chris 0:bdce2379bd67 111 // AND with 0x00 shifted to the right location to clear the bits
chris 1:41b9f8ec0b6a 112 pbuf[index] &= ~(0xFF << (8 * channel));
chris 0:bdce2379bd67 113
chris 0:bdce2379bd67 114 // Set the bits with an OR
chris 1:41b9f8ec0b6a 115 pbuf[index] |= (value << (8 * channel));
chris 0:bdce2379bd67 116 }
chris 0:bdce2379bd67 117
chris 0:bdce2379bd67 118
chris 0:bdce2379bd67 119 // set either the I,R,G,B value of specific pixel channel
chris 0:bdce2379bd67 120 void PixelArray::__set_pixel(int index, int value)
chris 0:bdce2379bd67 121 {
chris 0:bdce2379bd67 122 // AND with 0x00 shifted to the right location to clear the bits
chris 1:41b9f8ec0b6a 123 pbuf[index] = value;
chris 0:bdce2379bd67 124 }
chris 0:bdce2379bd67 125
chris 0:bdce2379bd67 126