#pragma once #include int pin(int x); class OmPixel { public: unsigned short r; unsigned short g; unsigned short b; OmPixel() { this->r = 0; this->g = 0; this->b = 0; } OmPixel(long color) { this->r = (color >> 16) & 0xff; this->g = (color >> 8) & 0xff; this->b = (color >> 0) & 0xff; } long color() { return ((long)this->r << 16) | (this->g << 8) | this->b; } void clear() { this->r = 0; this->g = 0; this->b = 0; } }; class OmNeoGrid { public: Adafruit_NeoPixel strip; OmPixel *pixels; short *ledIndex; // insert pixel index, get ledIndex map for nonrectangular pixel positions int width = 0; int height = 0; int k; OmNeoGrid(int pin, int width, int height) { Serial.print("a"); this->strip = Adafruit_NeoPixel(16, pin, NEO_GRB + NEO_KHZ800); this->strip.begin(); this->clear(); Serial.print("-b-"); Serial.print('z'); this->k = width * height; this->width = width; this->height = height; // this->pixels = (OmPixel *)calloc(this->k, sizeof(OmPixel)); this->pixels = new OmPixel[this->k]; this->ledIndex = (short *)calloc(this->k, sizeof(short)); // default to to-fro pattern... for(int x = 0; x < this->width; x++) { for(int y = 0; y < this->height; y++) { int xp = x; if(y & 1) xp = this->width - 1 - x; int pixelIndex = y * this->width + x; int ledIndex = y * this->width + xp; this->ledIndex[pixelIndex] = ledIndex; } } } ~OmNeoGrid() { delete this->pixels; free((void *)this->ledIndex); } void clear() { for(int ix = 0; ix < this->k; ix++) this->pixels[ix].clear(); } void setPixel(int pixelIndex, long color) { this->strip.setPixelColor(pixelIndex, color); } void setPixelXy(int x, int y, OmPixel &color) { if(x < 0 || x >= this->width || y < 0 || y >= this->height) return; int n = y * this->width + x; OmPixel &pixel = this->pixels[n]; pixel.r = pin(pixel.r + color.r); pixel.g = pin(pixel.g + color.g); pixel.b = pin(pixel.b + color.b); } void setPixelXy(int x, int y, long color) { OmPixel p(color); this->setPixelXy(x, y, p); } long color(int r, int g, int b) { return this->strip.Color(pin(r), pin(g), pin(b)); } void show() { for(int ix = 0; ix < this->k; ix++) { int n = this->ledIndex[ix]; this->strip.setPixelColor(n, this->pixels[ix].color()); } this->strip.show(); } // specific drawing helps void drawGradientDisk(float x, float y, float radius, long color); // fade linearly by radius void drawRect(float left, float top, float right, float bottom, long color); // antialias at edges. };