Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <avr/pgmspace.h>
- const byte hexChars[17][5] PROGMEM = {
- { B111, B101, B101, B101, B111 }, // 0
- { B010, B110, B010, B010, B111 }, // 1
- { B111, B001, B111, B100, B111 }, // 2
- { B111, B001, B111, B001, B111 }, // 3
- { B101, B101, B111, B001, B001 }, // 4
- { B111, B100, B111, B001, B111 }, // 5
- { B111, B100, B111, B101, B111 }, // 6
- { B111, B001, B001, B001, B001 }, // 7
- { B111, B101, B111, B101, B111 }, // 8
- { B111, B101, B111, B001, B111 }, // 9
- { B111, B101, B111, B101, B101 }, // A
- { B110, B101, B110, B101, B110 }, // B
- { B011, B100, B100, B100, B011 }, // C
- { B110, B101, B101, B101, B110 }, // D
- { B111, B100, B111, B100, B111 }, // E
- { B111, B100, B111, B100, B100 }, // F
- { B000, B000, B101, B010, B101 } // x
- };
- /* Uncomment the initialize the I2C address , uncomment only one, If you get a totally blank screen try the other*/
- #define i2c_Address 0x3c //initialize with the I2C addr 0x3C Typically eBay OLED's
- //#define i2c_Address 0x3d //initialize with the I2C addr 0x3D Typically Adafruit OLED's
- #define SCREEN_WIDTH 128 // OLED display width, in pixels
- #define SCREEN_HEIGHT 64 // OLED display height, in pixels
- #define OLED_RESET -1 // QT-PY / XIAO
- #include <Wire.h>
- #include <Adafruit_GFX.h>
- #include <U8g2_for_Adafruit_GFX.h>
- // Select what model you have, just comment "/*"s
- ///* // SH110x
- #include <Adafruit_SH110X.h>
- Adafruit_SH1106G oled = Adafruit_SH1106G(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
- #define C_WHITE SH110X_WHITE
- #define C_BLACK SH110X_BLACK
- void dispbegin() {
- if(!oled.begin(i2c_Address)) {
- //Serial.println("SCREEN PROBLEM");
- for(;;);
- } else {
- //Serial.println("SCREEN INITIALIZED");
- }
- }
- // */
- /* // SSD1306
- #include <Adafruit_SSD1306.h>
- Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
- #define C_WHITE SSD1306_WHITE
- #define C_BLACK SSD1306_BLACK
- void dispbegin() {
- if(!oled.begin(SSD1306_SWITCHCAPVCC, i2c_Address)) {
- Serial.println("SCREEN PROBLEM");
- for(;;);
- }
- }
- // */
- U8G2_FOR_ADAFRUIT_GFX fonts;
- // are you reading SRAM (false) or ProgMEM (true)?
- bool progMem = false;
- byte screen = 1;
- // how many bytes of the memory we want to print on our screen
- #define BUFFER_SIZE 48
- // adjust for your hardware / curiousity
- #define SRAM_SIZE 2048
- #define PGM_SIZE 32768
- long MEMORY_SIZE() {
- return progMem ? PGM_SIZE : SRAM_SIZE;
- }
- // can you browse with buttons?
- #define BROWSE_MODE true
- // up arrow
- #define btn_up 12
- // down arrow
- #define btn_dw 13
- // right arrow
- #define btn_r 11
- // left arrow
- #define btn_l 10
- int bPTimes = 1;
- //address we're in now
- long currentAddress = 0;
- //functions
- void setup() {
- Serial.begin(115200);
- delay(100);
- dispbegin();
- delay(400);
- fonts.begin(oled);
- oled.clearDisplay();
- oled.setTextWrap(false);
- fonts.setFont(u8g2_font_5x7_mf);
- fonts.setFontMode(0);
- pinMode(btn_up, INPUT_PULLcuUP);
- pinMode(btn_dw, INPUT_PULLUP);
- pinMode(btn_r, INPUT_PULLUP);
- pinMode(btn_l, INPUT_PULLUP);
- fonts.print(F("Easter egg, made by pje."));
- }
- void drawHexBitmap(const byte bitmap[], const byte& x, const byte& y, byte wM = 1, byte h = 5, bool sw = false) {
- byte width = sw ? h : wM * 8;
- byte height = sw ? wM * 8 : h;
- for (byte row = 0; row < height; row++) {
- for (byte col = 0; col < width; col++) {
- byte bitmapRow = sw ? col : row;
- byte bitmapCol = sw ? height - 1 - row : col;
- byte mask = 0x80 >> bitmapCol;
- if (pgm_read_byte(bitmap + bitmapRow) & mask) {
- oled.drawPixel(x + col, y + row, C_WHITE);
- }
- }
- }
- }
- void drawHexBitmap(byte bitmap[], const int8_t& x, const int8_t& y, int8_t wM = 1, byte h = 8, bool sw = true) {
- byte width = sw ? h : wM * 8;
- byte height = sw ? wM * 8 : h;
- for (byte row = 0; row < height; row++) {
- for (byte col = 0; col < width; col++) {
- byte bitmapRow = sw ? col : row;
- byte bitmapCol = sw ? height - 1 - row : col;
- byte mask = 0x80 >> bitmapCol;
- if (bitmap[bitmapRow] & mask) {
- oled.drawPixel(x + col, y + row, C_WHITE);
- }
- }
- }
- }
- void printHEX(byte val, const bool& has0x = false) {
- String tmp1 = val <= 0xF ? "0" : "" + String(val, HEX);
- //if (has0x) {
- // drawHexBitmap(hexChars[0], oled.getCursorX() - 5, oled.getCursorY());
- // drawHexBitmap(hexChars[17], oled.getCursorX() - 1, oled.getCursorY());
- //}
- for (int i = 0; i < 2; i++) {
- byte tmp2 = (byte)strtol(String(tmp1.charAt(i)).c_str(), NULL, 16);
- Serial.println("Good to go");
- Serial.println(tmp1 + ", " + (String)tmp2);
- drawHexBitmap(hexChars[tmp2], oled.getCursorX() + i * 4 - (has0x ? -2 : 5), oled.getCursorY());
- }
- }
- void printHEX(String val, const bool& has0x = true) {
- String tmp1 = val.length() % 2 == 1 ? "0" : "" + val;
- //if (has0x) {
- // drawHexBitmap(hexChars[0], oled.getCursorX() - 5, oled.getCursorY());
- // drawHexBitmap(hexChars[17], oled.getCursorX() - 1, oled.getCursorY());
- //}
- for (int i = 0; i < val.length(); i++) {
- for (int j = 0; j < 2; j++) {
- byte tmp2 = (byte)strtol(String(val.charAt(i*j)).c_str(), NULL, 16);
- drawHexBitmap(hexChars[tmp2], oled.getCursorX() + i*j * 4 - 5, oled.getCursorY());
- }
- }
- }
- void CheckButtons() {
- // When clicked DOWN button, move down the address;
- if (digitalRead(btn_dw) == LOW && BROWSE_MODE) {
- if(bPTimes % (screen==2?1:2) == 0 || bPTimes == 1) {
- if (currentAddress + 8 < MEMORY_SIZE())
- currentAddress += 8;
- else if (MEMORY_SIZE() - currentAddress > 1) currentAddress = MEMORY_SIZE();
- else currentAddress = 0;
- }
- delay(32 - bPTimes);
- if(bPTimes + 4 <= 32) bPTimes+=4;
- }
- //When clicked PGM button, switch modes between program memory and variable memory.
- else if (digitalRead(btn_r) == LOW) {
- currentAddress = 0;
- progMem = !progMem;
- delay(256);
- }
- //When clicked PGM button, switch modes between program memory and variable memory.
- else if (digitalRead(btn_l) == LOW) {
- screen + 1 <= 2 ? screen++ : screen = 1;
- delay(256);
- }
- // When clicked UP button, move up the address;
- else if (digitalRead(btn_up) == LOW && BROWSE_MODE) {
- if(bPTimes % (screen==2?1:2) == 0 || bPTimes == 1) {
- if (currentAddress - 8 >= 0)
- currentAddress -= 8;
- else currentAddress = MEMORY_SIZE();
- }
- delay(32 - bPTimes);
- if(bPTimes + 4 <= 32) bPTimes+=4;
- }
- else bPTimes = 0;
- }
- void drawStats(byte s1 = 0, byte s2 = 0, byte s3 = 0) {
- fonts.setCursor(0, 6);
- fonts.print("Addr 0x");
- fonts.print(currentAddress, HEX);
- fonts.print(" (");
- fonts.print(currentAddress, DEC);
- fonts.print(")");
- //fonts.print(s1); fonts.print(" Y:"); fonts.print(s2);
- //fonts.print(" ");
- //fonts.setCursor(0, 58);
- //fonts.println(" ");
- //fonts.print(" ");
- oled.display();
- }
- void drawLayout() {
- unsigned int t_buf_size = BUFFER_SIZE;
- int i = 0; byte x = 0; byte y = 1; byte z = 0; byte tmpbmp[8];
- while (i < t_buf_size)
- {
- byte MemByte = '.';
- if (!progMem) MemByte = *(byte*)(currentAddress + i);
- else MemByte = pgm_read_byte(currentAddress + i);
- switch(screen) {
- case 1:
- if (i < BUFFER_SIZE) {
- //if (x == 0) {
- //oled.setCursor(0, y * 8);
- // String tmp = String(currentAddress, HEX);
- // printHEX(tmp.substring(tmp.length() - 3), false);
- //}
- oled.setCursor(x * 2 * 4.5f + 16, y * 8);
- printHEX(MemByte, false);
- fonts.setCursor(x * 5 + 88, y * 8 + 5);
- if (MemByte >= 0x20 && MemByte < 0x80) {
- fonts.print((char)MemByte);
- } else if (MemByte >= 0x80) {
- fonts.drawGlyph(fonts.tx, fonts.ty, (short)MemByte);
- } else {
- oled.drawPixel(fonts.tx + 2, fonts.ty - 3, C_WHITE);
- }
- }
- tmpbmp[x] = MemByte;
- if (x >= 7) {
- //drawStats(x, y);
- x = 0; y++;
- drawHexBitmap(tmpbmp, (y-1) * 8, 56);
- if (y < 16) t_buf_size += 8; else t_buf_size = 1;
- } else x++;
- break;
- case 2:
- //t_buf_size *= 8;
- tmpbmp[x] = MemByte;
- if (x >= 7) {
- x = 0; y++;
- drawHexBitmap(tmpbmp, y * 8, (x + 1) * 8);
- if (y < 16) t_buf_size += 8; else t_buf_size = 1;
- } else x++;
- break;
- }
- i++;
- }
- drawStats();
- //oled.display();
- }
- void loop() {
- oled.clearDisplay();
- CheckButtons();
- drawLayout();
- if (!BROWSE_MODE)
- currentAddress += 8;
- if (currentAddress > MEMORY_SIZE())
- currentAddress = 0;
- if (!BROWSE_MODE)
- delay(500);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement