/* ATtiny85 Graphics Display with Big Text v2 - see http://www.technoblogy.com/show?LKP David Johnson-Davies - www.technoblogy.com - 3rd December 2018 ATtiny85 @ 8 MHz (internal oscillator; BOD disabled) CC BY 4.0 Licensed under a Creative Commons Attribution 4.0 International license: http://creativecommons.org/licenses/by/4.0/ */ // Pins int const clk = 0; int const data = 1; int const dc = 2; int const cs = 3; // OLED 64x48 monochrome display ********************************************** // Screen buffer const int Buffersize = 64*6; unsigned char Buffer[Buffersize]; // Initialisation sequence for OLED module int const InitLen = 12; const unsigned char Init[InitLen] PROGMEM = { 0xAE, // Display off 0x8D, // Charge pump 0x14, 0x20, // Memory mode 0x00, // Horizontal addressing 0xA1, // 0xA0/0xA1 flip horizontally 0xC8, // 0xC0/0xC8 flip vertically 0xD9, // Set pre charge 0xF1, 0xDB, // Set vcom detect 0x40, 0xAF // Display on }; // Write a data byte to the display void Data(uint8_t d) { uint8_t changes = d ^ (d>>1); PORTB = PORTB & ~(1<>= 1) { PINB = 1<>3; int bit = row & 0x07; // Set correct bit in slice buffer Buffer[page*64 + col] |= 1< -dy) { err = err - dy; x0 = x0 + sx; } if (e2 < dx) { err = err + dx; y0 = y0 + sy; } } } // Plot text from program memory into buffer at x,y void PlotText(int x, int y, PGM_P s) { int p = (int)s; int page = (47 - y - yOrigin)>>3; while (1) { char c = pgm_read_byte(p++); if (c == 0) return; for (uint8_t col = 0 ; col < 6; col++) { Buffer[page*64 + x + xOrigin] = pgm_read_byte(&CharMap[c-32][col]); x++; } } } // Converts bit pattern abcdefgh into aabbccddeeffgghh int Stretch (int x) { x = (x & 0xF0)<<4 | (x & 0x0F); x = (x<<2 | x) & 0x3333; x = (x<<1 | x) & 0x5555; return x | x<<1; } // Plot double-sized text from program memory into buffer at x,y void PlotBigText(int x, int y, PGM_P s) { int p = (int)s; int page = (47 - y - yOrigin)>>3; while (1) { char c = pgm_read_byte(p++); if (c == 0) return; for (uint8_t col = 0 ; col < 6; col++) { int bits = Stretch(pgm_read_byte(&CharMap[c-32][col])); for (int i=2; i--;) { Buffer[(page-1)*64 + x + xOrigin] = bits; Buffer[page*64 + x + xOrigin] = bits>>8; x++; } } } } // Setup ********************************************** void setup() { // Define pins pinMode(dc, OUTPUT); digitalWrite(dc,HIGH); pinMode(clk, OUTPUT); digitalWrite(clk,HIGH); pinMode(data, OUTPUT); pinMode(cs, OUTPUT); digitalWrite(cs,HIGH); InitDisplay(); } // Applications ********************************************** void AnalogueMeter () { xOrigin = 32; yOrigin = 0; const int Delta2 = 16; int x = -(23<<9), y = 0; ClearBuffer(); PlotText(-20, 40, PSTR("Voltage")); for (int i = 0; i<=100; i++) { if (i%20 == 0) { MoveTo(x>>9, (y>>9)); DrawTo((x>>9) - (x>>12), (y>>9) - (y>>12)); } if (i == (analogRead(A2)*25 + 25)>>8) { MoveTo(x>>9, y>>9); DrawTo(0, 0); } MoveTo(x>>9, y>>9); x = x + ((y>>9) * Delta2); y = y - ((x>>9) * Delta2); if (i != 100) DrawTo(x>>9, y>>9); } PlotText(-31, 0, PSTR("0")); PlotText(27, 0, PSTR("5")); PlotText(-27, 16, PSTR("1")); PlotText(23, 16, PSTR("4")); PlotText(-12, 24, PSTR("2 3")); DisplayBuffer(); } void Oscilloscope () { xOrigin = 0; yOrigin = 0; ClearBuffer(); PlotText(20, 40, PSTR("ADC2")); for (int x=1; x<63; x++) { int y = analogRead(A2)>>5; if (x == 1) MoveTo(x, y); else DrawTo(x, y); } DisplayBuffer(); } void RotatingCube () { const int Delta = 9; // Approximation to 1 degree in radians * 2^9 xOrigin = 32; yOrigin = 24; int x = 0, y = 22<<9; for (;;) { ClearBuffer(); int x9 = x>>9, y9 = y>>9, x10 = x>>10, y10 = y>>10; // Top MoveTo(x9, y10 + 12); DrawTo(y9, -x10 + 12); DrawTo(-x9, -y10 + 12); DrawTo(-y9, x10 + 12); DrawTo(x9, y10 + 12); DrawTo(x9, y10 - 12); // Bottom DrawTo(y9, -x10 - 12); DrawTo(-x9, -y10 - 12); DrawTo(-y9, x10 - 12); DrawTo(x9, y10 - 12); // Sides MoveTo(y9, -x10 + 12); DrawTo(y9, -x10 - 12); MoveTo(-x9, -y10 + 12); DrawTo(-x9, -y10 - 12); MoveTo(-y9, x10 + 12); DrawTo(-y9, x10 - 12); // Rotate cube x = x + (y9 * Delta); y = y - ((x>>9) * Delta); DisplayBuffer(); } } void DoubleSizeCharacters () { xOrigin = 0; yOrigin = 0; ClearBuffer(); PlotBigText(0, 32, PSTR("4.98V")); PlotBigText(0, 16, PSTR("0.15A")); PlotText(0, 0, PSTR("10:27:59am")); DisplayBuffer(); } void loop () { DoubleSizeCharacters(); }