#define NUMPIXELS 128 #define NUMROWS 8 #define NUMCOLUMNS 8 #define RGB_BLACK 0 #define RGB_BLUE 1 #define RGB_GREEN 2 #define RGB_CYAN 3 #define RGB_RED 4 #define RGB_MAGENTA 5 #define RGB_BROWN 6 #define RGB_LIGHTGRAY 7 #define RGB_DARKGRAY 8 #define RGB_YELLOW 14 #define RGB_WHITE 15 #include "./NPLib2x64.c" #include #include #include #include #include // API // void initialise(NUMPIXELS); // void show(void); // void setPixelTypeRGB(void); // void setPixelTypeGRB(void); // void setPixel(int pixelNumber, unsigned char R, unsigned char G, unsigned char B); // void setPixelRC(int row, int column, unsigned char R, unsigned char G, unsigned char B); // void clearPixel(int pixelNumber); // void clearbuffer(NUMPIXELS); // compile for RC2014 (CPM) using Z88dk : // zcc +cpm -vn neodie2.c -create-app -o neodie2.bin unsigned char sprites[7][8] = { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // 0 { 0x00, 0x00, 0x00, 0x18, 0x18, 0x00, 0x00, 0x00 }, // 1 { 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0xc0, 0xc0 }, // 2 { 0x03, 0x03, 0x00, 0x18, 0x18, 0x00, 0xc0, 0xc0 }, // 3 { 0xc3, 0xc3, 0x00, 0x00, 0x00, 0x00, 0xc3, 0xc3 }, // 4 { 0xc3, 0xc3, 0x00, 0x18, 0x18, 0x00, 0xc3, 0xc3 }, // 5 { 0xc3, 0xc3, 0x00, 0xc3, 0xc3, 0x00, 0xc3, 0xc3 } // 6 }; unsigned char diecolours[7] = {RGB_BLACK, RGB_CYAN, RGB_MAGENTA, RGB_BLUE, RGB_GREEN, RGB_RED, RGB_YELLOW}; // this decides the colour for each image. // Note that both of these arrays have 7 elements, so that we can index by the same number as our throw (1-6) and have a 0 for 'off' if we need it // prototypes void shuffle(); void displayface1(unsigned char throw); void displayface2(unsigned char throw); void writeSprite1(unsigned char* sprite, unsigned char R, unsigned char G, unsigned char B); void writeSprite2(unsigned char* sprite, unsigned char R, unsigned char G, unsigned char B); void smallDelay(); void delay(); void palette(int n, uint8_t *R, uint8_t *G, uint8_t *B); int main() { // always initialise NUMPIXELS initialise(NUMPIXELS); displayface1(0); displayface2(0); printf("neopixel die v0.1\nS Dixon 2023\n\npress the 'any' key to throw, 'esc' to exit.\n\n"); unsigned t; char ch = 0; uint8_t R, G, B; while (true) { // wait for key, // use this delay to randomise while (true) { if(kbhit()) { ch = getch(); // eat it break; } t++; } srand(t); if(ch==27) break; // esc // else... shuffle(); unsigned char throw = (rand() % 6)+1; // 0-5, +1 = 1-6 displayface1(throw); throw = (rand() % 6)+1; // 0-6, +1 = 1-6 displayface2(throw); } return 0; } void shuffle() { for (int i = 0; i < 1000; i++) { unsigned char throw = (rand() % 6)+1; // 0-5, +1 = 1-6 displayface1(throw); throw = (rand() % 6)+1; // 0-5, +1 = 1-6 displayface2(throw); smallDelay(); } } // throw must be 0-7 inclusive // shows but doesn't delay void displayface1(unsigned char throw){ uint8_t R, G, B; palette(diecolours[throw],&R,&G,&B); //printf("writeSprite: %d\n\r", throw); writeSprite1(&sprites[throw][0], R, G, B); show(); } // throw must be 0-7 inclusive // shows but doesn't delay void displayface2(unsigned char throw){ uint8_t R, G, B; palette(diecolours[throw],&R,&G,&B); //printf("writeSprite: %d\n\r", throw); writeSprite2(&sprites[throw][0], R, G, B); show(); } /* * Write a 1.st sprite * * Sprite is the pointer to a byte array, encoding the contains of the sprite */ void writeSprite1(unsigned char* sprite, unsigned char R, unsigned char G, unsigned char B) { for (int row = 0; row < 8; row++) { unsigned char value = sprite[row]; //printf("value: %d\n\r", value); unsigned char mask = 128; // bin 10000000 for (int column = 0; column < 8; column++) { if (value & mask) // pixel is not 0 { setPixelRC(row, column, R, G, B); } else { setPixelRC(row, column, 0, 0, 0); } mask >>= 1; } } } /* * Write a 2.nd sprite * * Sprite is the pointer to a byte array, encoding the contains of the sprite */ void writeSprite2(unsigned char* sprite, unsigned char R, unsigned char G, unsigned char B) { for (int row = 0; row < 8; row++) { unsigned char value = sprite[row]; //printf("value: %d\n\r", value); unsigned char mask = 128; // bin 10000000 for (int column = 8; column < 16; column++) { if (value & mask) // pixel is not 0 { setPixelRC(row, column, R, G, B); } else { setPixelRC(row, column, 0, 0, 0); } mask >>= 1; } } } void smallDelay() { for (int i = 0; i < 200; i++) { ; } } void delay() { for (int i = 0; i < 1000; i++) { smallDelay(); } } void palette(int n, uint8_t *R, uint8_t *G, uint8_t *B ) { switch(n) { case RGB_BLUE: *R = 0; *G = 0; *B = 32; break; case RGB_GREEN: *R = 0; *G = 32; *B = 0; break; case RGB_CYAN: *R = 0; *G = 32; *B = 32; break; case RGB_RED: *R = 32; *G = 0; *B = 0; break; case RGB_MAGENTA: *R = 32; *G = 0; *B = 32; break; case RGB_BROWN: *R = 32; *G = 16; *B = 0; break; case RGB_LIGHTGRAY: *R = 16; *G = 16; *B = 16; break; case RGB_DARKGRAY: *R = 8; *G = 8; *B = 8; break; case RGB_YELLOW: *R = 32; *G = 32; *B = 0; break; case RGB_WHITE: *R = 32; *G = 32; *B = 32; break; default: *R = 0; *G = 0; *B = 0; break; } }