A-KouZ1

P10 Shade

Jun 29th, 2023 (edited)
1,074
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.48 KB | None | 0 0
  1. // Written By Kouzerumatsukite; 25 JUNE 2023
  2. // 8-bit shade demonstration; 29 JUNE 2023
  3. #include <Arduino.h>
  4. #include <SPI.h>
  5.  
  6. // Assign these ESP32 pins to P10 Board
  7. #define DR  23 // -> DR   // Data-Receive
  8. #define OE  21 // -> OE   // Output-Enable
  9. #define CLK 18 // -> CLK  // Data-Clock
  10. #define LAT  5 // -> LAT  // Data-Latch
  11. #define RSA 32 // -> A    // Row Select A
  12. #define RSB 33 // -> B    // Row Select B
  13.  
  14. uint8_t display_data[8][4][16]; // 8 planes of 32x16 bitmap
  15. // Every single 32x16 bitmap is called plane
  16. // and each plane will be displayed with certain value of PWM
  17. // these are the table
  18. /////////////////
  19. // PLANE | PWM //
  20. // ------+---- //
  21. // bit-0 | 128 //
  22. // bit-1 | 64  //
  23. // bit-2 | 32  //
  24. // bit-3 | 16  //
  25. // bit-4 | 8   //
  26. // bit-5 | 4   //
  27. // bit-6 | 2   //
  28. // bit-7 | 1   //
  29. /////////////////
  30. // and if u wish to write an 8-bit shaded pixel, u must alter all the planes
  31. // and if u wish to determine what the shade of value just add PWM values of each plane together
  32. // eg shade 241, u add 128 + 64 + 32 + 16 + 1;
  33. // which means particular pixel in bit-0, bit-1, bit-2, bit-3, and bit-7 will be 1
  34. // and the rest, bit-4, bit-5, and bit-6 will be 0
  35.  
  36. // and nevermind, this code will do that for you
  37. void drawPixel(int x, int y, int c){
  38.   if(x>=0 && x<32 && y>=0 && y<16){
  39.     int a = ((~y>>2)&3)|((x>>3&3)<<2);
  40.     int b = y&3;
  41.     int s = x&7;
  42.     for(int i=0; i<8; i++){
  43.       display_data[i][b][a] &= ~(1<<(7-s));
  44.       display_data[i][b][a] |= ((~c>>(7-i))&1)<<(7-s);
  45.     }
  46.   }
  47. }
  48.  
  49. int current_row=0;
  50. int current_plane=0;
  51. int current_frame=0; // counts frames
  52.  
  53. void displayRow(){
  54.   uint8_t row = current_row;
  55.   uint8_t pln = current_plane;
  56.   uint8_t*src = (uint8_t*)&display_data[pln][row&3][0];
  57.   // turn off display, pwm=0
  58.   ledcWrite(0,0);
  59.   // update row data
  60.   SPI.writeBytes(src, 16);
  61.   // update row select
  62.   digitalWrite(RSA,row>>0&1);
  63.   digitalWrite(RSB,row>>1&1);
  64.   digitalWrite(LAT,HIGH);
  65.   digitalWrite(LAT,LOW);
  66.   // turn on display, pwm = plane value
  67.   ledcWrite(0,128>>current_plane);
  68.   // update states
  69.   current_row++;
  70.   if(current_row>=4){
  71.     current_row = 0;
  72.     current_plane++;
  73.     if(current_plane>=8){
  74.       current_plane = 0;
  75.       current_frame++;
  76.     }
  77.   }
  78.   return;
  79. }
  80.  
  81. void setup() {
  82.   // initialize pin states
  83.   pinMode(DR ,OUTPUT);
  84.   pinMode(OE ,OUTPUT);
  85.   pinMode(CLK,OUTPUT);
  86.   pinMode(LAT,OUTPUT);
  87.   pinMode(RSA,OUTPUT);
  88.   pinMode(RSB,OUTPUT);
  89.   // initialize SPI
  90.   SPI.begin(CLK, -1, DR, -1);
  91.   SPI.beginTransaction(SPISettings(20000000, MSBFIRST, SPI_MODE0));
  92.   // initialize PWM
  93.   ledcSetup(0, 40000000>>8, 8);
  94.   ledcAttachPin(OE, 0);
  95.   ledcWrite(0,0);
  96. }
  97.  
  98. uint8_t bruh[] = {
  99.  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,
  100.  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,
  101.  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,
  102.  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,
  103.  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,
  104.  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,
  105.  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,
  106.  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,
  107.  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,
  108.  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,
  109.  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,
  110.  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,
  111.  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,
  112.  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,
  113.  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,
  114.  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,
  115. };
  116.  
  117. uint8_t tsuki[] = {
  118.  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,
  119.  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,
  120.  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,
  121.  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,
  122.  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,
  123.  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,
  124.  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,
  125.  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,
  126.  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,
  127.  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,
  128.  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,
  129.  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,
  130.  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,
  131.  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,
  132.  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,
  133.  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,
  134. };
  135.  
  136. void displayPlane(int plane, int frames, int delayus){
  137.   current_row = 0;
  138.   while(frames--){
  139.     current_plane = plane;
  140.     displayRow(); delayMicroseconds(delayus);
  141.     displayRow(); delayMicroseconds(delayus);
  142.     displayRow(); delayMicroseconds(delayus);
  143.     displayRow(); delayMicroseconds(delayus);
  144.   }
  145. }
  146.  
  147.  
  148.  
  149. void loop() {
  150.   int delayfor=256;
  151.   // lets draw gradient
  152.   for(int x=0;x<32;x++)for(int y=0;y<16;y++)drawPixel(x,y,x&15|y<<4);
  153.   for(int j=0; j<64; j++){for(int i=0; i<8; i++)displayPlane(i,delayfor,1000);delayfor = delayfor>>1 | delayfor&1;yield();}
  154.    
  155.   // lets draw bruh pic
  156.   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);
  157.   for(int j=0; j<64; j++){for(int i=0; i<8; i++)displayPlane(i,1,250);yield();}
  158.    
  159.   // lets draw gradient again, but no flicker
  160.   for(int x=0;x<32;x++)for(int y=0;y<16;y++)drawPixel(x,y,x&15|y<<4);
  161.   for(int j=0; j<256; j++){for(int i=0; i<8; i++)displayPlane(i,1,250);yield();}
  162.  
  163.   delayfor=256;
  164.   // lets draw gradient, but gamma corrected, flickered
  165.   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);
  166.   for(int j=0; j<64; j++){for(int i=0; i<8; i++)displayPlane(i,delayfor,1000);delayfor = delayfor>>1 | delayfor&1;yield();}
  167.  
  168.   // lets draw gradient, but gamma corrected, no flicker
  169.   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);
  170.   for(int j=0; j<256; j++){for(int i=0; i<8; i++)displayPlane(i,1,250);yield();}
  171.  
  172.   // lets draw tsuki pic
  173.   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);
  174.   for(int j=0; j<256; j++){for(int i=0; i<8; i++)displayPlane(i,1,250);yield();}
  175. }
  176.  
Advertisement
Add Comment
Please, Sign In to add comment