Advertisement
Guest User

MEMORY_READER.ino

a guest
Sep 20th, 2023
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.78 KB | None | 0 0
  1. #include <avr/pgmspace.h>
  2. const byte hexChars[17][5] PROGMEM = {
  3.         { B111, B101, B101, B101, B111 }, // 0
  4.         { B010, B110, B010, B010, B111 }, // 1
  5.         { B111, B001, B111, B100, B111 }, // 2
  6.         { B111, B001, B111, B001, B111 }, // 3
  7.         { B101, B101, B111, B001, B001 }, // 4
  8.         { B111, B100, B111, B001, B111 }, // 5
  9.         { B111, B100, B111, B101, B111 }, // 6
  10.         { B111, B001, B001, B001, B001 }, // 7
  11.         { B111, B101, B111, B101, B111 }, // 8
  12.         { B111, B101, B111, B001, B111 }, // 9
  13.         { B111, B101, B111, B101, B101 }, // A
  14.         { B110, B101, B110, B101, B110 }, // B
  15.         { B011, B100, B100, B100, B011 }, // C
  16.         { B110, B101, B101, B101, B110 }, // D
  17.         { B111, B100, B111, B100, B111 }, // E
  18.         { B111, B100, B111, B100, B100 }, // F
  19.         { B000, B000, B101, B010, B101 }  // x
  20. };
  21. /* Uncomment the initialize the I2C address , uncomment only one, If you get a totally blank screen try the other*/
  22. #define i2c_Address 0x3c //initialize with the I2C addr 0x3C Typically eBay OLED's
  23. //#define i2c_Address 0x3d //initialize with the I2C addr 0x3D Typically Adafruit OLED's
  24.  
  25. #define SCREEN_WIDTH 128 // OLED display width, in pixels
  26. #define SCREEN_HEIGHT 64 // OLED display height, in pixels
  27. #define OLED_RESET -1   //   QT-PY / XIAO
  28.  
  29. #include <Wire.h>
  30. #include <Adafruit_GFX.h>
  31. #include <U8g2_for_Adafruit_GFX.h>
  32. // Select what model you have, just comment "/*"s
  33.  
  34. ///* // SH110x
  35.   #include <Adafruit_SH110X.h>
  36.   Adafruit_SH1106G oled = Adafruit_SH1106G(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
  37.   #define C_WHITE SH110X_WHITE
  38.   #define C_BLACK SH110X_BLACK
  39.   void dispbegin() {
  40.     if(!oled.begin(i2c_Address)) {
  41.         //Serial.println("SCREEN PROBLEM");
  42.         for(;;);
  43.     } else {
  44.         //Serial.println("SCREEN INITIALIZED");
  45.     }
  46.   }
  47. // */
  48.  
  49. /* // SSD1306
  50. #include <Adafruit_SSD1306.h>
  51. Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
  52. #define C_WHITE SSD1306_WHITE
  53. #define C_BLACK SSD1306_BLACK
  54. void dispbegin() {
  55.   if(!oled.begin(SSD1306_SWITCHCAPVCC, i2c_Address)) {
  56.       Serial.println("SCREEN PROBLEM");
  57.       for(;;);
  58.   }
  59. }
  60. // */
  61.  
  62. U8G2_FOR_ADAFRUIT_GFX fonts;
  63.  
  64. // are you reading SRAM (false) or ProgMEM (true)?
  65. bool progMem = false;
  66.  
  67. byte screen = 1;
  68.  
  69. // how many bytes of the memory we want to print on our screen
  70. #define BUFFER_SIZE 48
  71.  
  72. // adjust for your hardware / curiousity
  73. #define SRAM_SIZE 2048
  74. #define PGM_SIZE 32768
  75.  
  76. long MEMORY_SIZE() {
  77.   return progMem ? PGM_SIZE : SRAM_SIZE;
  78. }
  79.  
  80. // can you browse with buttons?
  81. #define BROWSE_MODE true
  82.  
  83. // up arrow
  84. #define btn_up 12
  85. // down arrow
  86. #define btn_dw 13
  87. // right arrow
  88. #define btn_r  11
  89. // left arrow
  90. #define btn_l  10
  91.  
  92. int bPTimes = 1;
  93.  
  94. //address we're in now
  95. long currentAddress = 0;
  96.  
  97. //functions
  98. void setup() {
  99.   Serial.begin(115200);
  100.   delay(100);
  101.   dispbegin();
  102.   delay(400);
  103.   fonts.begin(oled);
  104.   oled.clearDisplay();
  105.   oled.setTextWrap(false);
  106.   fonts.setFont(u8g2_font_5x7_mf);
  107.   fonts.setFontMode(0);
  108.   pinMode(btn_up, INPUT_PULLcuUP);
  109.   pinMode(btn_dw, INPUT_PULLUP);
  110.   pinMode(btn_r, INPUT_PULLUP);
  111.   pinMode(btn_l, INPUT_PULLUP);
  112.   fonts.print(F("Easter egg, made by pje."));
  113. }
  114.  
  115. void drawHexBitmap(const byte bitmap[], const byte& x, const byte& y, byte wM = 1, byte h = 5, bool sw = false) {
  116.   byte width = sw ? h : wM * 8;
  117.   byte height = sw ? wM * 8 : h;
  118.  
  119.   for (byte row = 0; row < height; row++) {
  120.     for (byte col = 0; col < width; col++) {
  121.       byte bitmapRow = sw ? col : row;
  122.       byte bitmapCol = sw ? height - 1 - row : col;
  123.  
  124.       byte mask = 0x80 >> bitmapCol;
  125.  
  126.       if (pgm_read_byte(bitmap + bitmapRow) & mask) {
  127.         oled.drawPixel(x + col, y + row, C_WHITE);
  128.       }
  129.     }
  130.   }
  131. }
  132.  
  133. void drawHexBitmap(byte bitmap[], const int8_t& x, const int8_t& y, int8_t wM = 1, byte h = 8, bool sw = true) {
  134.   byte width = sw ? h : wM * 8;
  135.   byte height = sw ? wM * 8 : h;
  136.  
  137.   for (byte row = 0; row < height; row++) {
  138.     for (byte col = 0; col < width; col++) {
  139.       byte bitmapRow = sw ? col : row;
  140.       byte bitmapCol = sw ? height - 1 - row : col;
  141.  
  142.       byte mask = 0x80 >> bitmapCol;
  143.  
  144.       if (bitmap[bitmapRow] & mask) {
  145.         oled.drawPixel(x + col, y + row, C_WHITE);
  146.       }
  147.     }
  148.   }
  149. }
  150.  
  151. void printHEX(byte val, const bool& has0x = false) {
  152.   String tmp1 = val <= 0xF ? "0" : "" + String(val, HEX);
  153.  
  154.   //if (has0x) {
  155.   //  drawHexBitmap(hexChars[0], oled.getCursorX() - 5, oled.getCursorY());
  156.   //  drawHexBitmap(hexChars[17], oled.getCursorX() - 1, oled.getCursorY());
  157.   //}
  158.   for (int i = 0; i < 2; i++) {
  159.     byte tmp2 = (byte)strtol(String(tmp1.charAt(i)).c_str(), NULL, 16);
  160.     Serial.println("Good to go");
  161.     Serial.println(tmp1 + ", " + (String)tmp2);
  162.     drawHexBitmap(hexChars[tmp2], oled.getCursorX() + i * 4 - (has0x ? -2 : 5), oled.getCursorY());
  163.   }
  164. }
  165.  
  166. void printHEX(String val, const bool& has0x = true) {
  167.   String tmp1 = val.length() % 2 == 1 ? "0" : "" + val;
  168.   //if (has0x) {
  169.   //  drawHexBitmap(hexChars[0], oled.getCursorX() - 5, oled.getCursorY());
  170.   //  drawHexBitmap(hexChars[17], oled.getCursorX() - 1, oled.getCursorY());
  171.   //}
  172.   for (int i = 0; i < val.length(); i++) {
  173.     for (int j = 0; j < 2; j++) {
  174.       byte tmp2 = (byte)strtol(String(val.charAt(i*j)).c_str(), NULL, 16);
  175.       drawHexBitmap(hexChars[tmp2], oled.getCursorX() + i*j * 4 - 5, oled.getCursorY());
  176.     }
  177.   }
  178. }
  179.  
  180.  
  181.  
  182.  
  183. void CheckButtons() {
  184.   // When clicked DOWN button, move down the address;
  185.   if (digitalRead(btn_dw) == LOW && BROWSE_MODE) {
  186.     if(bPTimes % (screen==2?1:2) == 0 || bPTimes == 1) {
  187.       if (currentAddress + 8 < MEMORY_SIZE())
  188.         currentAddress += 8;
  189.       else if (MEMORY_SIZE() - currentAddress > 1) currentAddress = MEMORY_SIZE();
  190.       else currentAddress = 0;
  191.     }
  192.     delay(32 - bPTimes);
  193.     if(bPTimes + 4 <= 32) bPTimes+=4;
  194.   }
  195.   //When clicked PGM button, switch modes between program memory and variable memory.
  196.   else if (digitalRead(btn_r) == LOW) {
  197.     currentAddress = 0;
  198.     progMem = !progMem;
  199.     delay(256);
  200.   }
  201.   //When clicked PGM button, switch modes between program memory and variable memory.
  202.   else if (digitalRead(btn_l) == LOW) {
  203.     screen + 1 <= 2 ? screen++ : screen = 1;
  204.     delay(256);
  205.   }
  206.   // When clicked UP button, move up the address;
  207.   else if (digitalRead(btn_up) == LOW && BROWSE_MODE) {
  208.     if(bPTimes % (screen==2?1:2) == 0 || bPTimes == 1) {
  209.       if (currentAddress - 8 >= 0)
  210.         currentAddress -= 8;
  211.       else currentAddress = MEMORY_SIZE();
  212.     }
  213.     delay(32 - bPTimes);
  214.     if(bPTimes + 4 <= 32) bPTimes+=4;
  215.   }
  216.   else bPTimes = 0;
  217. }
  218.  
  219. void drawStats(byte s1 = 0, byte s2 = 0, byte s3 = 0) {
  220. fonts.setCursor(0, 6);
  221. fonts.print("Addr 0x");
  222. fonts.print(currentAddress, HEX);
  223. fonts.print(" (");
  224. fonts.print(currentAddress, DEC);
  225. fonts.print(")");
  226. //fonts.print(s1); fonts.print(" Y:"); fonts.print(s2);
  227. //fonts.print("                        ");
  228. //fonts.setCursor(0, 58);
  229. //fonts.println("                                      ");
  230. //fonts.print("                                      ");
  231. oled.display();
  232. }
  233.  
  234. void drawLayout() {
  235.    
  236. unsigned int t_buf_size = BUFFER_SIZE;
  237. int i = 0; byte x = 0; byte y = 1; byte z = 0; byte tmpbmp[8];
  238.    
  239. while (i < t_buf_size)
  240. {
  241.   byte MemByte = '.';
  242.   if (!progMem) MemByte = *(byte*)(currentAddress + i);
  243.   else MemByte = pgm_read_byte(currentAddress + i);
  244.        
  245.   switch(screen) {
  246.  
  247.   case 1:
  248.                      
  249.  
  250.   if (i < BUFFER_SIZE) {
  251.     //if (x == 0) {
  252.       //oled.setCursor(0, y * 8);
  253.      // String tmp = String(currentAddress, HEX);
  254.     //  printHEX(tmp.substring(tmp.length() - 3), false);
  255.     //}
  256.     oled.setCursor(x * 2 * 4.5f + 16, y * 8);
  257.     printHEX(MemByte, false);
  258.     fonts.setCursor(x * 5 + 88, y * 8 + 5);
  259.     if (MemByte >= 0x20 && MemByte < 0x80) {
  260.       fonts.print((char)MemByte);
  261.     } else if (MemByte >= 0x80) {
  262.       fonts.drawGlyph(fonts.tx, fonts.ty, (short)MemByte);
  263.     } else {
  264.       oled.drawPixel(fonts.tx + 2, fonts.ty - 3, C_WHITE);
  265.     }
  266.   }
  267.   tmpbmp[x] = MemByte;
  268.   if (x >= 7) {
  269.     //drawStats(x, y);
  270.     x = 0; y++;
  271.     drawHexBitmap(tmpbmp, (y-1) * 8, 56);
  272.     if (y < 16) t_buf_size += 8; else t_buf_size = 1;
  273.   } else x++;
  274.   break;
  275.            
  276.            
  277.   case 2:
  278.   //t_buf_size *= 8;
  279.   tmpbmp[x] = MemByte;
  280.   if (x >= 7) {
  281.     x = 0; y++;
  282.     drawHexBitmap(tmpbmp, y * 8, (x + 1) * 8);
  283.     if (y < 16) t_buf_size += 8; else t_buf_size = 1;
  284.   } else x++;
  285.      
  286.   break;                              
  287.   }
  288.  
  289.   i++;
  290. }  
  291. drawStats();
  292.  
  293. //oled.display();    
  294. }
  295.  
  296. void loop() {
  297.   oled.clearDisplay();
  298.  
  299.   CheckButtons();
  300.    
  301.   drawLayout();
  302.  
  303.   if (!BROWSE_MODE)
  304.     currentAddress += 8;
  305.  
  306.   if (currentAddress > MEMORY_SIZE())
  307.     currentAddress = 0;
  308.   if (!BROWSE_MODE)
  309.     delay(500);
  310. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement