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 13 20:26:32 2014 +0000
Revision:
2:b45a70faaa83
Parent:
1:41b9f8ec0b6a
Added a call in the constructor to clear the memory before its used

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