CosminVarlan

WOKWI_Tetris_Layout_test_code

Nov 16th, 2025
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.82 KB | None | 0 0
  1. #define CLK 13
  2. #define DIN 11
  3. #define CS  10
  4. #define X_SEGMENTS   4
  5. #define Y_SEGMENTS   2
  6. #define NUM_SEGMENTS (X_SEGMENTS * Y_SEGMENTS)
  7.  
  8. // a framebuffer to hold the state of the entire matrix of LEDs
  9. // laid out in raster order, with (0, 0) at the top-left
  10. byte fb[8 * NUM_SEGMENTS];
  11.  
  12.  
  13. void shiftAll(byte send_to_address, byte send_this_data)
  14. {
  15.   digitalWrite(CS, LOW);
  16.   for (int i = 0; i < NUM_SEGMENTS; i++) {
  17.     shiftOut(DIN, CLK, MSBFIRST, send_to_address);
  18.     shiftOut(DIN, CLK, MSBFIRST, send_this_data);
  19.   }
  20.   digitalWrite(CS, HIGH);
  21. }
  22.  
  23.  
  24. void setup() {
  25.   Serial.begin(115200);
  26.   pinMode(CLK, OUTPUT);
  27.   pinMode(DIN, OUTPUT);
  28.   pinMode(CS, OUTPUT);
  29.  
  30.   // Setup each MAX7219
  31.   shiftAll(0x0f, 0x00); //display test register - test mode off
  32.   shiftAll(0x0b, 0x07); //scan limit register - display digits 0 thru 7
  33.   shiftAll(0x0c, 0x01); //shutdown register - normal operation
  34.   shiftAll(0x0a, 0x0f); //intensity register - max brightness
  35.   shiftAll(0x09, 0x00); //decode mode register - No decode
  36. }
  37.  
  38. void show() {
  39.   for (byte row = 0; row < 8; row++) {
  40.     digitalWrite(CS, LOW);
  41.     byte segment = NUM_SEGMENTS;
  42.     while (segment--) {
  43.       byte x = segment % X_SEGMENTS;
  44.       byte y = segment / X_SEGMENTS * 8;
  45.       byte addr = (row + y) * X_SEGMENTS;
  46.  
  47.       if (segment & X_SEGMENTS) { // odd rows of segments
  48.         shiftOut(DIN, CLK, MSBFIRST, 8 - row);
  49.         shiftOut(DIN, CLK, LSBFIRST, fb[addr + x]);
  50.       } else { // even rows of segments
  51.         shiftOut(DIN, CLK, MSBFIRST, 1 + row);
  52.         shiftOut(DIN, CLK, MSBFIRST, fb[addr - x + X_SEGMENTS - 1]);
  53.       }
  54.     }
  55.     digitalWrite(CS, HIGH);
  56.   }
  57. }
  58.  
  59. void loop(){
  60.   for(int i=0; i<64; i++) fb[i]=0;
  61.  
  62.   fb[(millis()/1000)%64] = (1<<(millis()/125%8)); // inlocuiti asta cu fb[0]=B00000011;  fb[63] = 255;
  63.  
  64.   show();
  65. }
Advertisement
Add Comment
Please, Sign In to add comment