Simple line-by-line way to use SSD1306 screens.
SSD1306_simple.h@2:574d1b329593, 2020-05-24 (annotated)
- Committer:
- austinbrown124
- Date:
- Sun May 24 17:32:24 2020 +0000
- Revision:
- 2:574d1b329593
- Parent:
- 1:e08f6c4332d3
Included ResetI2C function.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
austinbrown124 | 0:bef1b3bca5d9 | 1 | #ifndef SSD1306_SIMPLE_H |
austinbrown124 | 0:bef1b3bca5d9 | 2 | #define SSD1306_SIMPLE_H |
austinbrown124 | 0:bef1b3bca5d9 | 3 | |
austinbrown124 | 0:bef1b3bca5d9 | 4 | #include "mbed.h" |
austinbrown124 | 0:bef1b3bca5d9 | 5 | #include "font_8x8.h" |
austinbrown124 | 0:bef1b3bca5d9 | 6 | #include "font_16x24.h" |
austinbrown124 | 0:bef1b3bca5d9 | 7 | #include "font_16x12.h" |
austinbrown124 | 0:bef1b3bca5d9 | 8 | |
austinbrown124 | 0:bef1b3bca5d9 | 9 | #define SSD1306_128_64 |
austinbrown124 | 0:bef1b3bca5d9 | 10 | // #define SSD1306_128_32 |
austinbrown124 | 0:bef1b3bca5d9 | 11 | // #define SSD1306_96_16 |
austinbrown124 | 0:bef1b3bca5d9 | 12 | /*=========================================================================*/ |
austinbrown124 | 0:bef1b3bca5d9 | 13 | |
austinbrown124 | 0:bef1b3bca5d9 | 14 | #if defined SSD1306_128_64 && defined SSD1306_128_32 |
austinbrown124 | 0:bef1b3bca5d9 | 15 | #error "Only one SSD1306 display can be specified at once in SSD1306.h" |
austinbrown124 | 0:bef1b3bca5d9 | 16 | #endif |
austinbrown124 | 0:bef1b3bca5d9 | 17 | #if !defined SSD1306_128_64 && !defined SSD1306_128_32 && !defined SSD1306_96_16 |
austinbrown124 | 0:bef1b3bca5d9 | 18 | #error "At least one SSD1306 display must be specified in SSD1306.h" |
austinbrown124 | 0:bef1b3bca5d9 | 19 | #endif |
austinbrown124 | 0:bef1b3bca5d9 | 20 | |
austinbrown124 | 0:bef1b3bca5d9 | 21 | #if defined SSD1306_128_64 |
austinbrown124 | 0:bef1b3bca5d9 | 22 | #define SSD1306_LCDWIDTH 128 |
austinbrown124 | 0:bef1b3bca5d9 | 23 | #define SSD1306_LCDHEIGHT 64 |
austinbrown124 | 0:bef1b3bca5d9 | 24 | #endif |
austinbrown124 | 0:bef1b3bca5d9 | 25 | #if defined SSD1306_128_32 |
austinbrown124 | 0:bef1b3bca5d9 | 26 | #define SSD1306_LCDWIDTH 128 |
austinbrown124 | 0:bef1b3bca5d9 | 27 | #define SSD1306_LCDHEIGHT 32 |
austinbrown124 | 0:bef1b3bca5d9 | 28 | #endif |
austinbrown124 | 0:bef1b3bca5d9 | 29 | #if defined SSD1306_96_16 |
austinbrown124 | 0:bef1b3bca5d9 | 30 | #define SSD1306_LCDWIDTH 96 |
austinbrown124 | 0:bef1b3bca5d9 | 31 | #define SSD1306_LCDHEIGHT 16 |
austinbrown124 | 0:bef1b3bca5d9 | 32 | #endif |
austinbrown124 | 0:bef1b3bca5d9 | 33 | |
austinbrown124 | 0:bef1b3bca5d9 | 34 | #define SSD1306_SETCONTRAST 0x81 |
austinbrown124 | 0:bef1b3bca5d9 | 35 | #define SSD1306_DISPLAYALLON_RESUME 0xA4 |
austinbrown124 | 0:bef1b3bca5d9 | 36 | #define SSD1306_DISPLAYALLON 0xA5 |
austinbrown124 | 0:bef1b3bca5d9 | 37 | #define SSD1306_NORMALDISPLAY 0xA6 |
austinbrown124 | 0:bef1b3bca5d9 | 38 | #define SSD1306_INVERTDISPLAY 0xA7 |
austinbrown124 | 0:bef1b3bca5d9 | 39 | #define SSD1306_DISPLAYOFF 0xAE |
austinbrown124 | 0:bef1b3bca5d9 | 40 | #define SSD1306_DISPLAYON 0xAF |
austinbrown124 | 0:bef1b3bca5d9 | 41 | |
austinbrown124 | 0:bef1b3bca5d9 | 42 | #define SSD1306_SETDISPLAYOFFSET 0xD3 |
austinbrown124 | 0:bef1b3bca5d9 | 43 | #define SSD1306_SETCOMPINS 0xDA |
austinbrown124 | 0:bef1b3bca5d9 | 44 | |
austinbrown124 | 0:bef1b3bca5d9 | 45 | #define SSD1306_SETVCOMDETECT 0xDB |
austinbrown124 | 0:bef1b3bca5d9 | 46 | |
austinbrown124 | 0:bef1b3bca5d9 | 47 | #define SSD1306_SETDISPLAYCLOCKDIV 0xD5 |
austinbrown124 | 0:bef1b3bca5d9 | 48 | #define SSD1306_SETPRECHARGE 0xD9 |
austinbrown124 | 0:bef1b3bca5d9 | 49 | |
austinbrown124 | 0:bef1b3bca5d9 | 50 | #define SSD1306_SETMULTIPLEX 0xA8 |
austinbrown124 | 0:bef1b3bca5d9 | 51 | |
austinbrown124 | 0:bef1b3bca5d9 | 52 | #define SSD1306_SETLOWCOLUMN 0x00 |
austinbrown124 | 0:bef1b3bca5d9 | 53 | #define SSD1306_SETHIGHCOLUMN 0x10 |
austinbrown124 | 0:bef1b3bca5d9 | 54 | |
austinbrown124 | 0:bef1b3bca5d9 | 55 | #define SSD1306_SETSTARTLINE 0x40 |
austinbrown124 | 0:bef1b3bca5d9 | 56 | |
austinbrown124 | 0:bef1b3bca5d9 | 57 | #define SSD1306_MEMORYMODE 0x20 |
austinbrown124 | 0:bef1b3bca5d9 | 58 | #define SSD1306_COLUMNADDR 0x21 |
austinbrown124 | 0:bef1b3bca5d9 | 59 | #define SSD1306_PAGEADDR 0x22 |
austinbrown124 | 0:bef1b3bca5d9 | 60 | |
austinbrown124 | 0:bef1b3bca5d9 | 61 | #define SSD1306_COMSCANINC 0xC0 |
austinbrown124 | 0:bef1b3bca5d9 | 62 | #define SSD1306_COMSCANDEC 0xC8 |
austinbrown124 | 0:bef1b3bca5d9 | 63 | |
austinbrown124 | 0:bef1b3bca5d9 | 64 | #define SSD1306_SEGREMAP 0xA0 |
austinbrown124 | 0:bef1b3bca5d9 | 65 | |
austinbrown124 | 0:bef1b3bca5d9 | 66 | #define SSD1306_CHARGEPUMP 0x8D |
austinbrown124 | 0:bef1b3bca5d9 | 67 | |
austinbrown124 | 0:bef1b3bca5d9 | 68 | #define SSD1306_EXTERNALVCC 0x1 |
austinbrown124 | 0:bef1b3bca5d9 | 69 | #define SSD1306_SWITCHCAPVCC 0x2 |
austinbrown124 | 0:bef1b3bca5d9 | 70 | |
austinbrown124 | 0:bef1b3bca5d9 | 71 | // Scrolling #defines |
austinbrown124 | 0:bef1b3bca5d9 | 72 | #define SSD1306_ACTIVATE_SCROLL 0x2F |
austinbrown124 | 0:bef1b3bca5d9 | 73 | #define SSD1306_DEACTIVATE_SCROLL 0x2E |
austinbrown124 | 0:bef1b3bca5d9 | 74 | #define SSD1306_SET_VERTICAL_SCROLL_AREA 0xA3 |
austinbrown124 | 0:bef1b3bca5d9 | 75 | #define SSD1306_RIGHT_HORIZONTAL_SCROLL 0x26 |
austinbrown124 | 0:bef1b3bca5d9 | 76 | #define SSD1306_LEFT_HORIZONTAL_SCROLL 0x27 |
austinbrown124 | 0:bef1b3bca5d9 | 77 | #define SSD1306_VERTICAL_AND_RIGHT_HORIZONTAL_SCROLL 0x29 |
austinbrown124 | 0:bef1b3bca5d9 | 78 | #define SSD1306_VERTICAL_AND_LEFT_HORIZONTAL_SCROLL 0x2A |
austinbrown124 | 0:bef1b3bca5d9 | 79 | |
austinbrown124 | 0:bef1b3bca5d9 | 80 | |
austinbrown124 | 0:bef1b3bca5d9 | 81 | #define DATA_MODE 0x40 |
austinbrown124 | 0:bef1b3bca5d9 | 82 | |
austinbrown124 | 0:bef1b3bca5d9 | 83 | |
austinbrown124 | 0:bef1b3bca5d9 | 84 | // Display dimensions |
austinbrown124 | 0:bef1b3bca5d9 | 85 | #define ROWS 64 |
austinbrown124 | 0:bef1b3bca5d9 | 86 | #define COLUMNS 128 |
austinbrown124 | 0:bef1b3bca5d9 | 87 | #define PAGES (ROWS / 8) |
austinbrown124 | 0:bef1b3bca5d9 | 88 | #define MAX_PAGE (PAGES - 1) |
austinbrown124 | 0:bef1b3bca5d9 | 89 | #define MAX_ROW (ROWS - 1) |
austinbrown124 | 0:bef1b3bca5d9 | 90 | #define MAX_COL (COLUMNS - 1) |
austinbrown124 | 0:bef1b3bca5d9 | 91 | |
austinbrown124 | 0:bef1b3bca5d9 | 92 | // Character dimensions 8x8 font |
austinbrown124 | 0:bef1b3bca5d9 | 93 | #define CHARS (COLUMNS / FONT8x8_WIDTH) // = |
austinbrown124 | 0:bef1b3bca5d9 | 94 | |
austinbrown124 | 0:bef1b3bca5d9 | 95 | // Command and Datamode |
austinbrown124 | 0:bef1b3bca5d9 | 96 | #define COMMAND_MODE 0x80 // continuation bit is set! Just means we could stream cotinous coomands |
austinbrown124 | 0:bef1b3bca5d9 | 97 | // as is done in in triple comand mode |
austinbrown124 | 0:bef1b3bca5d9 | 98 | |
austinbrown124 | 0:bef1b3bca5d9 | 99 | |
austinbrown124 | 0:bef1b3bca5d9 | 100 | #define SET_COLUMN_ADDRESS 0x21 // takes two bytes, start address and end address of display data RAM |
austinbrown124 | 0:bef1b3bca5d9 | 101 | #define SET_PAGE_ADDRESS 0x22 // takes two bytes, start address and end address of display data RAM |
austinbrown124 | 0:bef1b3bca5d9 | 102 | |
austinbrown124 | 0:bef1b3bca5d9 | 103 | |
austinbrown124 | 0:bef1b3bca5d9 | 104 | #define FRAME_BUF_OFFSET 13 |
austinbrown124 | 0:bef1b3bca5d9 | 105 | |
austinbrown124 | 0:bef1b3bca5d9 | 106 | #define FONT_8x8 0 |
austinbrown124 | 0:bef1b3bca5d9 | 107 | #define FONT_16x12_0 1 |
austinbrown124 | 0:bef1b3bca5d9 | 108 | #define FONT_16x12_1 2 |
austinbrown124 | 0:bef1b3bca5d9 | 109 | |
austinbrown124 | 2:574d1b329593 | 110 | #define TIMEOUT_INIT 400 |
austinbrown124 | 2:574d1b329593 | 111 | |
austinbrown124 | 0:bef1b3bca5d9 | 112 | |
austinbrown124 | 0:bef1b3bca5d9 | 113 | class SSD1306 { |
austinbrown124 | 0:bef1b3bca5d9 | 114 | |
austinbrown124 | 0:bef1b3bca5d9 | 115 | public: |
austinbrown124 | 0:bef1b3bca5d9 | 116 | SSD1306(); |
austinbrown124 | 0:bef1b3bca5d9 | 117 | |
austinbrown124 | 1:e08f6c4332d3 | 118 | void writeString(uint8_t col, const char * text) ; |
austinbrown124 | 1:e08f6c4332d3 | 119 | void writeInt( uint8_t col, int16_t num); |
austinbrown124 | 1:e08f6c4332d3 | 120 | void WriteRow( uint8_t page ); |
austinbrown124 | 1:e08f6c4332d3 | 121 | void ClearBuf(); |
austinbrown124 | 1:e08f6c4332d3 | 122 | bool IsReady(); |
austinbrown124 | 1:e08f6c4332d3 | 123 | void setFont( uint8_t font ); |
austinbrown124 | 1:e08f6c4332d3 | 124 | |
austinbrown124 | 1:e08f6c4332d3 | 125 | private: |
austinbrown124 | 1:e08f6c4332d3 | 126 | |
austinbrown124 | 1:e08f6c4332d3 | 127 | void clearDisplay(); |
austinbrown124 | 2:574d1b329593 | 128 | void ResetI2C(); |
austinbrown124 | 1:e08f6c4332d3 | 129 | bool i2cWrite( uint8_t *buf, uint8_t num_bytes); |
austinbrown124 | 1:e08f6c4332d3 | 130 | void DMAi2cWrite( uint8_t *buf, uint8_t num_bytes); |
austinbrown124 | 1:e08f6c4332d3 | 131 | void writeFrameBufRow( uint8_t page ); |
austinbrown124 | 1:e08f6c4332d3 | 132 | void writeCharToBuf( uint8_t col, char chr ); |
austinbrown124 | 0:bef1b3bca5d9 | 133 | bool ssd1306_command(uint8_t c); |
austinbrown124 | 0:bef1b3bca5d9 | 134 | void _sendCommand(uint8_t command, uint8_t param1, uint8_t param2); |
austinbrown124 | 0:bef1b3bca5d9 | 135 | void _sendData(uint8_t data); |
austinbrown124 | 0:bef1b3bca5d9 | 136 | void setPageAddress(uint8_t start, uint8_t end); |
austinbrown124 | 0:bef1b3bca5d9 | 137 | void setColumnAddress(uint8_t start, uint8_t end); |
austinbrown124 | 0:bef1b3bca5d9 | 138 | void writeChar(char chr); |
austinbrown124 | 0:bef1b3bca5d9 | 139 | uint8_t framebuf[COLUMNS+FRAME_BUF_OFFSET]; // add control commands. Only a single row is buffered at a time. |
austinbrown124 | 2:574d1b329593 | 140 | int timeout_cnt; |
austinbrown124 | 0:bef1b3bca5d9 | 141 | |
austinbrown124 | 0:bef1b3bca5d9 | 142 | void SetupFrameBuf(); |
austinbrown124 | 0:bef1b3bca5d9 | 143 | uint8_t _char_width; |
austinbrown124 | 0:bef1b3bca5d9 | 144 | uint8_t _font; |
austinbrown124 | 0:bef1b3bca5d9 | 145 | |
austinbrown124 | 0:bef1b3bca5d9 | 146 | }; |
austinbrown124 | 0:bef1b3bca5d9 | 147 | |
austinbrown124 | 0:bef1b3bca5d9 | 148 | //char *convert(unsigned int num, int base); |
austinbrown124 | 0:bef1b3bca5d9 | 149 | |
austinbrown124 | 0:bef1b3bca5d9 | 150 | |
austinbrown124 | 0:bef1b3bca5d9 | 151 | |
austinbrown124 | 0:bef1b3bca5d9 | 152 | #endif |