Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Written By Kouzerumatsukite; 25 JUNE 2023
- // 8-bit shade demonstration; 29 JUNE 2023
- #include <Arduino.h>
- #include <SPI.h>
- // Assign these ESP32 pins to P10 Board
- #define DR 23 // -> DR // Data-Receive
- #define OE 21 // -> OE // Output-Enable
- #define CLK 18 // -> CLK // Data-Clock
- #define LAT 5 // -> LAT // Data-Latch
- #define RSA 32 // -> A // Row Select A
- #define RSB 33 // -> B // Row Select B
- uint8_t display_data[8][4][16]; // 8 planes of 32x16 bitmap
- // Every single 32x16 bitmap is called plane
- // and each plane will be displayed with certain value of PWM
- // these are the table
- /////////////////
- // PLANE | PWM //
- // ------+---- //
- // bit-0 | 128 //
- // bit-1 | 64 //
- // bit-2 | 32 //
- // bit-3 | 16 //
- // bit-4 | 8 //
- // bit-5 | 4 //
- // bit-6 | 2 //
- // bit-7 | 1 //
- /////////////////
- // and if u wish to write an 8-bit shaded pixel, u must alter all the planes
- // and if u wish to determine what the shade of value just add PWM values of each plane together
- // eg shade 241, u add 128 + 64 + 32 + 16 + 1;
- // which means particular pixel in bit-0, bit-1, bit-2, bit-3, and bit-7 will be 1
- // and the rest, bit-4, bit-5, and bit-6 will be 0
- // and nevermind, this code will do that for you
- void drawPixel(int x, int y, int c){
- if(x>=0 && x<32 && y>=0 && y<16){
- int a = ((~y>>2)&3)|((x>>3&3)<<2);
- int b = y&3;
- int s = x&7;
- for(int i=0; i<8; i++){
- display_data[i][b][a] &= ~(1<<(7-s));
- display_data[i][b][a] |= ((~c>>(7-i))&1)<<(7-s);
- }
- }
- }
- int current_row=0;
- int current_plane=0;
- int current_frame=0; // counts frames
- void displayRow(){
- uint8_t row = current_row;
- uint8_t pln = current_plane;
- uint8_t*src = (uint8_t*)&display_data[pln][row&3][0];
- // turn off display, pwm=0
- ledcWrite(0,0);
- // update row data
- SPI.writeBytes(src, 16);
- // update row select
- digitalWrite(RSA,row>>0&1);
- digitalWrite(RSB,row>>1&1);
- digitalWrite(LAT,HIGH);
- digitalWrite(LAT,LOW);
- // turn on display, pwm = plane value
- ledcWrite(0,128>>current_plane);
- // update states
- current_row++;
- if(current_row>=4){
- current_row = 0;
- current_plane++;
- if(current_plane>=8){
- current_plane = 0;
- current_frame++;
- }
- }
- return;
- }
- void setup() {
- // initialize pin states
- pinMode(DR ,OUTPUT);
- pinMode(OE ,OUTPUT);
- pinMode(CLK,OUTPUT);
- pinMode(LAT,OUTPUT);
- pinMode(RSA,OUTPUT);
- pinMode(RSB,OUTPUT);
- // initialize SPI
- SPI.begin(CLK, -1, DR, -1);
- SPI.beginTransaction(SPISettings(20000000, MSBFIRST, SPI_MODE0));
- // initialize PWM
- ledcSetup(0, 40000000>>8, 8);
- ledcAttachPin(OE, 0);
- ledcWrite(0,0);
- }
- uint8_t bruh[] = {
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0xF4, 0x74, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0xF4, 0x74, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0xF4, 0x74, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0xF4, 0x93, 0xB4, 0xF1, 0xE1, 0x75, 0x00, 0x00, 0xF4, 0x8F, 0xDA, 0xF0, 0x56, 0x00, 0xF8, 0x70, 0x00, 0x00, 0x00, 0xA8, 0xC0, 0x00, 0x00, 0xF0, 0x97, 0xAC, 0xEC, 0xEE, 0x96, 0x08, 0x00,
- 0x00, 0xF4, 0xFF, 0x78, 0x11, 0x52, 0xF5, 0x6C, 0x00, 0xF4, 0xF7, 0x31, 0x2B, 0x14, 0x00, 0xF8, 0x70, 0x00, 0x00, 0x00, 0xA8, 0xC0, 0x00, 0x00, 0xF0, 0xFD, 0x67, 0x0B, 0x3F, 0xF7, 0x7D, 0x00,
- 0x00, 0xF4, 0xBA, 0x00, 0x00, 0x00, 0x8B, 0xE6, 0x02, 0xF4, 0xA7, 0x00, 0x00, 0x00, 0x00, 0xF8, 0x70, 0x00, 0x00, 0x00, 0xA8, 0xC0, 0x00, 0x00, 0xF0, 0xAD, 0x00, 0x00, 0x00, 0xAF, 0xBE, 0x00,
- 0x00, 0xF4, 0x72, 0x00, 0x00, 0x00, 0x4A, 0xFF, 0x1F, 0xF4, 0x85, 0x00, 0x00, 0x00, 0x00, 0xF8, 0x70, 0x00, 0x00, 0x00, 0xA8, 0xC0, 0x00, 0x00, 0xF0, 0x7F, 0x00, 0x00, 0x00, 0x98, 0xCE, 0x00,
- 0x00, 0xF4, 0x5D, 0x00, 0x00, 0x00, 0x37, 0xFF, 0x39, 0xF4, 0x74, 0x00, 0x00, 0x00, 0x00, 0xF8, 0x70, 0x00, 0x00, 0x00, 0xA8, 0xC0, 0x00, 0x00, 0xF0, 0x78, 0x00, 0x00, 0x00, 0x98, 0xD0, 0x00,
- 0x00, 0xF4, 0x6C, 0x00, 0x00, 0x00, 0x4D, 0xFF, 0x26, 0xF4, 0x74, 0x00, 0x00, 0x00, 0x00, 0xF7, 0x72, 0x00, 0x00, 0x00, 0xB1, 0xC0, 0x00, 0x00, 0xF0, 0x78, 0x00, 0x00, 0x00, 0x98, 0xD0, 0x00,
- 0x00, 0xF4, 0xA9, 0x00, 0x00, 0x00, 0x93, 0xE5, 0x01, 0xF4, 0x74, 0x00, 0x00, 0x00, 0x00, 0xE9, 0x85, 0x00, 0x00, 0x01, 0xDD, 0xC0, 0x00, 0x00, 0xF0, 0x78, 0x00, 0x00, 0x00, 0x98, 0xD0, 0x00,
- 0x00, 0xF4, 0xFD, 0x61, 0x0B, 0x5A, 0xF8, 0x62, 0x00, 0xF4, 0x74, 0x00, 0x00, 0x00, 0x00, 0xA0, 0xE1, 0x28, 0x10, 0x8B, 0xF9, 0xC0, 0x00, 0x00, 0xF0, 0x78, 0x00, 0x00, 0x00, 0x98, 0xD0, 0x00,
- 0x00, 0xF4, 0x85, 0xC2, 0xF9, 0xDE, 0x63, 0x00, 0x00, 0xF4, 0x74, 0x00, 0x00, 0x00, 0x00, 0x16, 0xAB, 0xF4, 0xED, 0x97, 0x8A, 0xC0, 0x00, 0x00, 0xF0, 0x78, 0x00, 0x00, 0x00, 0x98, 0xD0, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- };
- uint8_t tsuki[] = {
- 0x00, 0x00, 0x00, 0x00, 0x99, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x33, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x62, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x78, 0x78, 0xF0, 0xF0, 0xF0, 0x78, 0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x99, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x78, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0xA3, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0xD3, 0xD3, 0x95, 0xF0, 0xF0, 0x95, 0xD3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0xA3, 0xA3, 0xA3, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0xD3, 0xF0, 0x95, 0xD3, 0xD3, 0x95, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA3, 0xA3, 0x4F, 0xA3, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA3, 0x4F, 0x4F, 0xA3, 0xA3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x74, 0x9C, 0x9C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xCC, 0x00, 0x00, 0x3A, 0x6D, 0x4F, 0x4F, 0x4F, 0x6D, 0x3A, 0x00, 0x00, 0x00, 0xCC, 0x99, 0x66, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x6D, 0x78, 0xF0, 0xF0, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x66, 0x99, 0x99, 0x99, 0xCC, 0x3A, 0x3A, 0x6D, 0x6D, 0x6D, 0x6D, 0x3A, 0x00, 0xCC, 0x99, 0x99, 0x99, 0x99, 0x66, 0x00,
- 0x00, 0x00, 0x00, 0x6D, 0x78, 0xF0, 0x6D, 0x3D, 0x00, 0x00, 0x00, 0x00, 0x66, 0x66, 0x99, 0x66, 0xCC, 0x99, 0x6D, 0x3A, 0x6D, 0x3A, 0x6D, 0x6D, 0x99, 0x99, 0x66, 0x99, 0x66, 0x99, 0x99, 0x66,
- 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB,
- 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB, 0xAB,
- 0x62, 0x62, 0x62, 0x62, 0x62, 0x62, 0x62, 0x62, 0x62, 0x62, 0x62, 0x62, 0x62, 0x62, 0x62, 0x62, 0x62, 0x62, 0x62, 0x62, 0x62, 0x62, 0x62, 0x62, 0x62, 0x62, 0x62, 0x62, 0x62, 0x62, 0x62, 0x62,
- 0x62, 0x3D, 0x3D, 0x62, 0x62, 0x3D, 0x3D, 0x62, 0x62, 0x3D, 0x3D, 0x62, 0x62, 0x3D, 0x3D, 0x62, 0x62, 0x3D, 0x3D, 0x62, 0x62, 0x3D, 0x3D, 0x62, 0x62, 0x3D, 0x3D, 0x62, 0x62, 0x3D, 0x3D, 0x62,
- };
- void displayPlane(int plane, int frames, int delayus){
- current_row = 0;
- while(frames--){
- current_plane = plane;
- displayRow(); delayMicroseconds(delayus);
- displayRow(); delayMicroseconds(delayus);
- displayRow(); delayMicroseconds(delayus);
- displayRow(); delayMicroseconds(delayus);
- }
- }
- void loop() {
- int delayfor=256;
- // lets draw gradient
- for(int x=0;x<32;x++)for(int y=0;y<16;y++)drawPixel(x,y,x&15|y<<4);
- for(int j=0; j<64; j++){for(int i=0; i<8; i++)displayPlane(i,delayfor,1000);delayfor = delayfor>>1 | delayfor&1;yield();}
- // lets draw bruh pic
- for(int x=0;x<32;x++)for(int y=0;y<16;y++)drawPixel(x,y,bruh[y*32+x]*bruh[y*32+x]>>8);
- for(int j=0; j<64; j++){for(int i=0; i<8; i++)displayPlane(i,1,250);yield();}
- // lets draw gradient again, but no flicker
- for(int x=0;x<32;x++)for(int y=0;y<16;y++)drawPixel(x,y,x&15|y<<4);
- for(int j=0; j<256; j++){for(int i=0; i<8; i++)displayPlane(i,1,250);yield();}
- delayfor=256;
- // lets draw gradient, but gamma corrected, flickered
- for(int x=0;x<32;x++)for(int y=0;y<16;y++)drawPixel(x,y,(x&15|y<<4)*(x&15|y<<4)>>8);
- for(int j=0; j<64; j++){for(int i=0; i<8; i++)displayPlane(i,delayfor,1000);delayfor = delayfor>>1 | delayfor&1;yield();}
- // lets draw gradient, but gamma corrected, no flicker
- for(int x=0;x<32;x++)for(int y=0;y<16;y++)drawPixel(x,y,(x&15|y<<4)*(x&15|y<<4)>>8);
- for(int j=0; j<256; j++){for(int i=0; i<8; i++)displayPlane(i,1,250);yield();}
- // lets draw tsuki pic
- for(int x=0;x<32;x++)for(int y=0;y<16;y++)drawPixel(x,y,tsuki[y*32+x]*tsuki[y*32+x]>>8);
- for(int j=0; j<256; j++){for(int i=0; i<8; i++)displayPlane(i,1,250);yield();}
- }
Advertisement
Add Comment
Please, Sign In to add comment