Arduino style GUI
Drivers/SSD1306_I2C.cpp@0:90962b684403, 2016-05-06 (annotated)
- Committer:
- jonebuckman
- Date:
- Fri May 06 16:51:15 2016 +0000
- Revision:
- 0:90962b684403
Updated drivers.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jonebuckman | 0:90962b684403 | 1 | /* NeatGUI Library |
jonebuckman | 0:90962b684403 | 2 | * Copyright (c) 2013 Neil Thiessen |
jonebuckman | 0:90962b684403 | 3 | * |
jonebuckman | 0:90962b684403 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
jonebuckman | 0:90962b684403 | 5 | * you may not use this file except in compliance with the License. |
jonebuckman | 0:90962b684403 | 6 | * You may obtain a copy of the License at |
jonebuckman | 0:90962b684403 | 7 | * |
jonebuckman | 0:90962b684403 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
jonebuckman | 0:90962b684403 | 9 | * |
jonebuckman | 0:90962b684403 | 10 | * Unless required by applicable law or agreed to in writing, software |
jonebuckman | 0:90962b684403 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
jonebuckman | 0:90962b684403 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
jonebuckman | 0:90962b684403 | 13 | * See the License for the specific language governing permissions and |
jonebuckman | 0:90962b684403 | 14 | * limitations under the License. |
jonebuckman | 0:90962b684403 | 15 | */ |
jonebuckman | 0:90962b684403 | 16 | |
jonebuckman | 0:90962b684403 | 17 | #include "SSD1306_I2C.h" |
jonebuckman | 0:90962b684403 | 18 | |
jonebuckman | 0:90962b684403 | 19 | SSD1306_I2C::SSD1306_I2C(PinName sda, PinName scl, Address addr) : Display(128, 64), m_I2C(sda, scl), m_ADDR((int)addr) |
jonebuckman | 0:90962b684403 | 20 | { |
jonebuckman | 0:90962b684403 | 21 | //Set the I2C frequency to 400kHz |
jonebuckman | 0:90962b684403 | 22 | m_I2C.frequency(400000); |
jonebuckman | 0:90962b684403 | 23 | } |
jonebuckman | 0:90962b684403 | 24 | |
jonebuckman | 0:90962b684403 | 25 | bool SSD1306_I2C::open() |
jonebuckman | 0:90962b684403 | 26 | { |
jonebuckman | 0:90962b684403 | 27 | //Probe for the SSD1306 using a Zero Length Transfer |
jonebuckman | 0:90962b684403 | 28 | if (!m_I2C.write(m_ADDR, NULL, 0)) { |
jonebuckman | 0:90962b684403 | 29 | //Init sequence for 128x64 OLED module |
jonebuckman | 0:90962b684403 | 30 | writeCommand(CMD_DISPLAYOFF); |
jonebuckman | 0:90962b684403 | 31 | writeCommand(CMD_SETDISPLAYCLOCKDIV); |
jonebuckman | 0:90962b684403 | 32 | writeCommand(0x80); |
jonebuckman | 0:90962b684403 | 33 | writeCommand(CMD_SETMULTIPLEX); |
jonebuckman | 0:90962b684403 | 34 | writeCommand(0x3F); |
jonebuckman | 0:90962b684403 | 35 | writeCommand(CMD_SETDISPLAYOFFSET); |
jonebuckman | 0:90962b684403 | 36 | writeCommand(0x0); |
jonebuckman | 0:90962b684403 | 37 | writeCommand(CMD_SETSTARTLINE | 0x0); |
jonebuckman | 0:90962b684403 | 38 | writeCommand(CMD_CHARGEPUMP); |
jonebuckman | 0:90962b684403 | 39 | writeCommand(CMD_CHARGEPUMPON); |
jonebuckman | 0:90962b684403 | 40 | writeCommand(CMD_MEMORYMODE); |
jonebuckman | 0:90962b684403 | 41 | writeCommand(0x00); |
jonebuckman | 0:90962b684403 | 42 | writeCommand(CMD_SEGREMAP | 0x1); |
jonebuckman | 0:90962b684403 | 43 | writeCommand(CMD_COMSCANDEC); |
jonebuckman | 0:90962b684403 | 44 | writeCommand(CMD_SETCOMPINS); |
jonebuckman | 0:90962b684403 | 45 | writeCommand(0x12); |
jonebuckman | 0:90962b684403 | 46 | writeCommand(CMD_SETCONTRAST); |
jonebuckman | 0:90962b684403 | 47 | writeCommand(0xCF); |
jonebuckman | 0:90962b684403 | 48 | writeCommand(CMD_SETPRECHARGE); |
jonebuckman | 0:90962b684403 | 49 | writeCommand(0xF1); |
jonebuckman | 0:90962b684403 | 50 | writeCommand(CMD_SETVCOMDETECT); |
jonebuckman | 0:90962b684403 | 51 | writeCommand(0x40); |
jonebuckman | 0:90962b684403 | 52 | writeCommand(CMD_DISPLAYALLON_RESUME); |
jonebuckman | 0:90962b684403 | 53 | writeCommand(CMD_NORMALDISPLAY); |
jonebuckman | 0:90962b684403 | 54 | |
jonebuckman | 0:90962b684403 | 55 | //Return success |
jonebuckman | 0:90962b684403 | 56 | return true; |
jonebuckman | 0:90962b684403 | 57 | } else { |
jonebuckman | 0:90962b684403 | 58 | //Return failure |
jonebuckman | 0:90962b684403 | 59 | return false; |
jonebuckman | 0:90962b684403 | 60 | } |
jonebuckman | 0:90962b684403 | 61 | } |
jonebuckman | 0:90962b684403 | 62 | |
jonebuckman | 0:90962b684403 | 63 | void SSD1306_I2C::flush() |
jonebuckman | 0:90962b684403 | 64 | { |
jonebuckman | 0:90962b684403 | 65 | //Select low col 0, hi col 0, line 0 |
jonebuckman | 0:90962b684403 | 66 | writeCommand(CMD_SETLOWCOLUMN | 0x0); |
jonebuckman | 0:90962b684403 | 67 | writeCommand(CMD_SETHIGHCOLUMN | 0x0); |
jonebuckman | 0:90962b684403 | 68 | writeCommand(CMD_SETSTARTLINE | 0x0); |
jonebuckman | 0:90962b684403 | 69 | |
jonebuckman | 0:90962b684403 | 70 | //Make sure the first byte in the buffer is the control byte |
jonebuckman | 0:90962b684403 | 71 | m_Buffer[0] = CONTROL_DATA; |
jonebuckman | 0:90962b684403 | 72 | |
jonebuckman | 0:90962b684403 | 73 | //Write the buffer |
jonebuckman | 0:90962b684403 | 74 | m_I2C.write(m_ADDR, m_Buffer, 1025); |
jonebuckman | 0:90962b684403 | 75 | } |
jonebuckman | 0:90962b684403 | 76 | |
jonebuckman | 0:90962b684403 | 77 | Display::State SSD1306_I2C::state() |
jonebuckman | 0:90962b684403 | 78 | { |
jonebuckman | 0:90962b684403 | 79 | //Return the base class's state |
jonebuckman | 0:90962b684403 | 80 | return Display::state(); |
jonebuckman | 0:90962b684403 | 81 | } |
jonebuckman | 0:90962b684403 | 82 | |
jonebuckman | 0:90962b684403 | 83 | void SSD1306_I2C::state(State s) |
jonebuckman | 0:90962b684403 | 84 | { |
jonebuckman | 0:90962b684403 | 85 | //Check what the requested state is |
jonebuckman | 0:90962b684403 | 86 | if (s == Display::DISPLAY_ON) { |
jonebuckman | 0:90962b684403 | 87 | //Turn the display on |
jonebuckman | 0:90962b684403 | 88 | writeCommand(CMD_DISPLAYON); |
jonebuckman | 0:90962b684403 | 89 | } else if (s == Display::DISPLAY_OFF) { |
jonebuckman | 0:90962b684403 | 90 | //Turn the display off |
jonebuckman | 0:90962b684403 | 91 | writeCommand(CMD_DISPLAYOFF); |
jonebuckman | 0:90962b684403 | 92 | } |
jonebuckman | 0:90962b684403 | 93 | |
jonebuckman | 0:90962b684403 | 94 | //Update the base class |
jonebuckman | 0:90962b684403 | 95 | Display::state(s); |
jonebuckman | 0:90962b684403 | 96 | } |
jonebuckman | 0:90962b684403 | 97 | |
jonebuckman | 0:90962b684403 | 98 | void SSD1306_I2C::drawPixel(int x, int y, unsigned int c) |
jonebuckman | 0:90962b684403 | 99 | { |
jonebuckman | 0:90962b684403 | 100 | //Range check the pixel |
jonebuckman | 0:90962b684403 | 101 | if ((x < 0) || (x >= width()) || (y < 0) || (y >= height())) |
jonebuckman | 0:90962b684403 | 102 | return; |
jonebuckman | 0:90962b684403 | 103 | |
jonebuckman | 0:90962b684403 | 104 | //Make sure the color is fully opaque |
jonebuckman | 0:90962b684403 | 105 | if ((c >> 24) != 255) |
jonebuckman | 0:90962b684403 | 106 | return; |
jonebuckman | 0:90962b684403 | 107 | |
jonebuckman | 0:90962b684403 | 108 | //Determine the pixel byte index |
jonebuckman | 0:90962b684403 | 109 | unsigned short byteIndex = x + (y / 8) * width(); |
jonebuckman | 0:90962b684403 | 110 | |
jonebuckman | 0:90962b684403 | 111 | //HACK: Fix the whole 1025 byte i2c buffer thing |
jonebuckman | 0:90962b684403 | 112 | byteIndex++; |
jonebuckman | 0:90962b684403 | 113 | |
jonebuckman | 0:90962b684403 | 114 | //Set or clear the pixel |
jonebuckman | 0:90962b684403 | 115 | if ((c & 0x00FFFFFF) > 0) |
jonebuckman | 0:90962b684403 | 116 | m_Buffer[byteIndex] |= (1 << (y % 8)); |
jonebuckman | 0:90962b684403 | 117 | else |
jonebuckman | 0:90962b684403 | 118 | m_Buffer[byteIndex] &= ~(1 << (y % 8)); |
jonebuckman | 0:90962b684403 | 119 | } |
jonebuckman | 0:90962b684403 | 120 | |
jonebuckman | 0:90962b684403 | 121 | void SSD1306_I2C::writeCommand(char command) |
jonebuckman | 0:90962b684403 | 122 | { |
jonebuckman | 0:90962b684403 | 123 | //Create a temporary buffer |
jonebuckman | 0:90962b684403 | 124 | char buff[2]; |
jonebuckman | 0:90962b684403 | 125 | |
jonebuckman | 0:90962b684403 | 126 | //Load the control byte and 8-bit command |
jonebuckman | 0:90962b684403 | 127 | buff[0] = CONTROL_COMMAND; |
jonebuckman | 0:90962b684403 | 128 | buff[1] = command; |
jonebuckman | 0:90962b684403 | 129 | |
jonebuckman | 0:90962b684403 | 130 | //Write the command |
jonebuckman | 0:90962b684403 | 131 | m_I2C.write(m_ADDR, buff, 2); |
jonebuckman | 0:90962b684403 | 132 | } |
jonebuckman | 0:90962b684403 | 133 | |
jonebuckman | 0:90962b684403 | 134 | void SSD1306_I2C::writeData(char data) |
jonebuckman | 0:90962b684403 | 135 | { |
jonebuckman | 0:90962b684403 | 136 | //Create a temporary buffer |
jonebuckman | 0:90962b684403 | 137 | char buff[2]; |
jonebuckman | 0:90962b684403 | 138 | |
jonebuckman | 0:90962b684403 | 139 | //Load the control byte and 8-bit data |
jonebuckman | 0:90962b684403 | 140 | buff[0] = CONTROL_DATA; |
jonebuckman | 0:90962b684403 | 141 | buff[1] = data; |
jonebuckman | 0:90962b684403 | 142 | |
jonebuckman | 0:90962b684403 | 143 | //Write the data |
jonebuckman | 0:90962b684403 | 144 | m_I2C.write(m_ADDR, buff, 2); |
jonebuckman | 0:90962b684403 | 145 | } |