Library for drawing on the Hexiwear's OLED - more features being actively worked on
Dependents: Hexidraw_Demo Hexiwear-FinalProject_v2
Discussion: https://developer.mbed.org/forum/team-4615-Hexiwear-community/topic/26595/
Hexidraw is a library for drawing on the Hexiwear's OLED. Be aware that it is not very optimized, so drawing may be slow, especially when drawing on large areas of the screen.
Please see the wiki for API documentation.
Features:
- Screen fill with a color
- Drawing filled rectangles
- Drawing circles with a set radius and line width
- Setting individual pixels
- Turning on and off the OLED's sleep mode
- Drawing images
Example project: https://developer.mbed.org/users/keithm01/code/Hexidraw_Demo/
hexidraw.cpp@5:00876457cad4, 2016-08-20 (annotated)
- Committer:
- keithm01
- Date:
- Sat Aug 20 16:35:58 2016 +0000
- Revision:
- 5:00876457cad4
- Parent:
- 4:9c0b59439725
Changed default value to not using the buffer
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
keithm01 | 0:2d3fcb6dabd4 | 1 | #include "oled_info.h" |
keithm01 | 0:2d3fcb6dabd4 | 2 | #include "hexidraw.h" |
keithm01 | 0:2d3fcb6dabd4 | 3 | |
keithm01 | 0:2d3fcb6dabd4 | 4 | #include "mbed.h" |
keithm01 | 0:2d3fcb6dabd4 | 5 | |
keithm01 | 2:ef14c6dd6b93 | 6 | #include <math.h> |
keithm01 | 2:ef14c6dd6b93 | 7 | |
keithm01 | 0:2d3fcb6dabd4 | 8 | /* |
keithm01 | 0:2d3fcb6dabd4 | 9 | Command descriptions from: |
keithm01 | 0:2d3fcb6dabd4 | 10 | https://cdn-shop.adafruit.com/datasheets/SSD1351-Revision+1.3.pdf |
keithm01 | 0:2d3fcb6dabd4 | 11 | */ |
keithm01 | 0:2d3fcb6dabd4 | 12 | |
keithm01 | 0:2d3fcb6dabd4 | 13 | OLED::OLED () : |
keithm01 | 0:2d3fcb6dabd4 | 14 | _spi(oled_sdi_pin, NC, oled_sck_pin), |
keithm01 | 0:2d3fcb6dabd4 | 15 | _cs(oled_cs_pin), |
keithm01 | 0:2d3fcb6dabd4 | 16 | _reset(oled_rst_pin), |
keithm01 | 0:2d3fcb6dabd4 | 17 | _dc(oled_dc_pin) |
keithm01 | 4:9c0b59439725 | 18 | { |
keithm01 | 4:9c0b59439725 | 19 | if(USE_BUFFER) { |
keithm01 | 4:9c0b59439725 | 20 | for(uint16_t i = 0; i < OLED_WIDTH * OLED_HEIGHT; ++ i) { |
keithm01 | 4:9c0b59439725 | 21 | displayBuffer[i] = BLACK; |
keithm01 | 4:9c0b59439725 | 22 | } |
keithm01 | 4:9c0b59439725 | 23 | } |
keithm01 | 4:9c0b59439725 | 24 | } |
keithm01 | 0:2d3fcb6dabd4 | 25 | |
keithm01 | 0:2d3fcb6dabd4 | 26 | void OLED::begin() |
keithm01 | 0:2d3fcb6dabd4 | 27 | { |
keithm01 | 2:ef14c6dd6b93 | 28 | |
keithm01 | 2:ef14c6dd6b93 | 29 | #if 0 |
keithm01 | 0:2d3fcb6dabd4 | 30 | // set pin directions |
keithm01 | 0:2d3fcb6dabd4 | 31 | pinMode(_rs, OUTPUT); |
keithm01 | 2:ef14c6dd6b93 | 32 | |
keithm01 | 0:2d3fcb6dabd4 | 33 | if (_sclk) { |
keithm01 | 0:2d3fcb6dabd4 | 34 | pinMode(_sclk, OUTPUT); |
keithm01 | 2:ef14c6dd6b93 | 35 | |
keithm01 | 0:2d3fcb6dabd4 | 36 | pinMode(_sid, OUTPUT); |
keithm01 | 0:2d3fcb6dabd4 | 37 | } else { |
keithm01 | 0:2d3fcb6dabd4 | 38 | // using the hardware SPI |
keithm01 | 0:2d3fcb6dabd4 | 39 | SPI.begin(); |
keithm01 | 0:2d3fcb6dabd4 | 40 | SPI.setDataMode(SPI_MODE3); |
keithm01 | 0:2d3fcb6dabd4 | 41 | } |
keithm01 | 2:ef14c6dd6b93 | 42 | |
keithm01 | 0:2d3fcb6dabd4 | 43 | // Toggle RST low to reset; CS low so it'll listen to us |
keithm01 | 0:2d3fcb6dabd4 | 44 | pinMode(_cs, OUTPUT); |
keithm01 | 0:2d3fcb6dabd4 | 45 | digitalWrite(_cs, LOW); |
keithm01 | 2:ef14c6dd6b93 | 46 | |
keithm01 | 0:2d3fcb6dabd4 | 47 | if (_rst) { |
keithm01 | 0:2d3fcb6dabd4 | 48 | pinMode(_rst, OUTPUT); |
keithm01 | 0:2d3fcb6dabd4 | 49 | digitalWrite(_rst, HIGH); |
keithm01 | 0:2d3fcb6dabd4 | 50 | delay(500); |
keithm01 | 0:2d3fcb6dabd4 | 51 | digitalWrite(_rst, LOW); |
keithm01 | 0:2d3fcb6dabd4 | 52 | delay(500); |
keithm01 | 0:2d3fcb6dabd4 | 53 | digitalWrite(_rst, HIGH); |
keithm01 | 0:2d3fcb6dabd4 | 54 | delay(500); |
keithm01 | 0:2d3fcb6dabd4 | 55 | } |
keithm01 | 0:2d3fcb6dabd4 | 56 | #endif |
keithm01 | 0:2d3fcb6dabd4 | 57 | DigitalOut BOOSTEN(oled_power_enable); //Enable OLED |
keithm01 | 0:2d3fcb6dabd4 | 58 | |
keithm01 | 0:2d3fcb6dabd4 | 59 | //Set SPI modes |
keithm01 | 0:2d3fcb6dabd4 | 60 | // _spi.format(8,3); |
keithm01 | 0:2d3fcb6dabd4 | 61 | _spi.frequency(8000000); |
keithm01 | 0:2d3fcb6dabd4 | 62 | |
keithm01 | 2:ef14c6dd6b93 | 63 | _dc =0; |
keithm01 | 0:2d3fcb6dabd4 | 64 | BOOSTEN = 0; |
keithm01 | 0:2d3fcb6dabd4 | 65 | wait(0.1f); |
keithm01 | 2:ef14c6dd6b93 | 66 | _reset = 0 ; |
keithm01 | 0:2d3fcb6dabd4 | 67 | wait(0.1f); |
keithm01 | 0:2d3fcb6dabd4 | 68 | _reset = 1 ; |
keithm01 | 0:2d3fcb6dabd4 | 69 | wait(0.1f); |
keithm01 | 0:2d3fcb6dabd4 | 70 | BOOSTEN = 1; |
keithm01 | 0:2d3fcb6dabd4 | 71 | |
keithm01 | 0:2d3fcb6dabd4 | 72 | writeCommand(OLED_CMD_SET_CMD_LOCK); |
keithm01 | 0:2d3fcb6dabd4 | 73 | writeData(0x12); |
keithm01 | 2:ef14c6dd6b93 | 74 | |
keithm01 | 0:2d3fcb6dabd4 | 75 | writeCommand(OLED_CMD_SET_CMD_LOCK); |
keithm01 | 0:2d3fcb6dabd4 | 76 | writeData(0xB1); |
keithm01 | 0:2d3fcb6dabd4 | 77 | |
keithm01 | 0:2d3fcb6dabd4 | 78 | writeCommand(OLED_CMD_DISPLAYOFF); |
keithm01 | 2:ef14c6dd6b93 | 79 | |
keithm01 | 2:ef14c6dd6b93 | 80 | |
keithm01 | 0:2d3fcb6dabd4 | 81 | writeCommand(OLED_CMD_SET_OSC_FREQ_AND_CLOCKDIV); |
keithm01 | 0:2d3fcb6dabd4 | 82 | writeData(0xF1); |
keithm01 | 0:2d3fcb6dabd4 | 83 | |
keithm01 | 0:2d3fcb6dabd4 | 84 | writeCommand(OLED_CMD_SET_MUX_RATIO); |
keithm01 | 0:2d3fcb6dabd4 | 85 | writeData(0x5F); |
keithm01 | 2:ef14c6dd6b93 | 86 | |
keithm01 | 0:2d3fcb6dabd4 | 87 | writeCommand(OLED_CMD_SET_REMAP); |
keithm01 | 0:2d3fcb6dabd4 | 88 | writeData(OLED_REMAP_SETTINGS); |
keithm01 | 2:ef14c6dd6b93 | 89 | |
keithm01 | 0:2d3fcb6dabd4 | 90 | writeCommand(OLED_CMD_SET_COLUMN); |
keithm01 | 0:2d3fcb6dabd4 | 91 | writeData(0x00); |
keithm01 | 0:2d3fcb6dabd4 | 92 | writeData(0x5F); |
keithm01 | 2:ef14c6dd6b93 | 93 | |
keithm01 | 0:2d3fcb6dabd4 | 94 | writeCommand(OLED_CMD_SET_ROW); |
keithm01 | 0:2d3fcb6dabd4 | 95 | writeData(0x00); |
keithm01 | 0:2d3fcb6dabd4 | 96 | writeData(0x5F); |
keithm01 | 2:ef14c6dd6b93 | 97 | |
keithm01 | 0:2d3fcb6dabd4 | 98 | writeCommand(OLED_CMD_STARTLINE); |
keithm01 | 0:2d3fcb6dabd4 | 99 | writeData(0x80); |
keithm01 | 2:ef14c6dd6b93 | 100 | |
keithm01 | 0:2d3fcb6dabd4 | 101 | writeCommand(OLED_CMD_DISPLAYOFFSET); |
keithm01 | 0:2d3fcb6dabd4 | 102 | writeData(0x60); |
keithm01 | 2:ef14c6dd6b93 | 103 | |
keithm01 | 0:2d3fcb6dabd4 | 104 | writeCommand(OLED_CMD_PRECHARGE); |
keithm01 | 0:2d3fcb6dabd4 | 105 | writeData(0x32); |
keithm01 | 2:ef14c6dd6b93 | 106 | |
keithm01 | 0:2d3fcb6dabd4 | 107 | writeCommand(OLED_CMD_VCOMH); |
keithm01 | 0:2d3fcb6dabd4 | 108 | writeData(0x05); |
keithm01 | 2:ef14c6dd6b93 | 109 | |
keithm01 | 0:2d3fcb6dabd4 | 110 | writeCommand(OLED_CMD_NORMALDISPLAY); |
keithm01 | 2:ef14c6dd6b93 | 111 | |
keithm01 | 0:2d3fcb6dabd4 | 112 | writeCommand(OLED_CMD_CONTRASTABC); |
keithm01 | 0:2d3fcb6dabd4 | 113 | writeData(0x8A); |
keithm01 | 0:2d3fcb6dabd4 | 114 | writeData(0x51); |
keithm01 | 0:2d3fcb6dabd4 | 115 | writeData(0x8A); |
keithm01 | 2:ef14c6dd6b93 | 116 | |
keithm01 | 0:2d3fcb6dabd4 | 117 | writeCommand(OLED_CMD_CONTRASTMASTER); |
keithm01 | 0:2d3fcb6dabd4 | 118 | writeData(0xCF); |
keithm01 | 2:ef14c6dd6b93 | 119 | |
keithm01 | 0:2d3fcb6dabd4 | 120 | writeCommand(OLED_CMD_SETVSL); |
keithm01 | 0:2d3fcb6dabd4 | 121 | writeData(0xA0); |
keithm01 | 0:2d3fcb6dabd4 | 122 | writeData(0xB5); |
keithm01 | 0:2d3fcb6dabd4 | 123 | writeData(0x55); |
keithm01 | 2:ef14c6dd6b93 | 124 | |
keithm01 | 0:2d3fcb6dabd4 | 125 | writeCommand(OLED_CMD_PRECHARGE2); |
keithm01 | 0:2d3fcb6dabd4 | 126 | writeData(0x01); |
keithm01 | 2:ef14c6dd6b93 | 127 | |
keithm01 | 0:2d3fcb6dabd4 | 128 | writeCommand(OLED_CMD_DISPLAYON); |
keithm01 | 0:2d3fcb6dabd4 | 129 | } |
keithm01 | 0:2d3fcb6dabd4 | 130 | |
keithm01 | 0:2d3fcb6dabd4 | 131 | void OLED::writeCommand(uint8_t code) |
keithm01 | 0:2d3fcb6dabd4 | 132 | { |
keithm01 | 0:2d3fcb6dabd4 | 133 | _dc = 0; |
keithm01 | 2:ef14c6dd6b93 | 134 | |
keithm01 | 0:2d3fcb6dabd4 | 135 | _cs = 0; |
keithm01 | 0:2d3fcb6dabd4 | 136 | _spi.write(code); |
keithm01 | 0:2d3fcb6dabd4 | 137 | _cs = 1; |
keithm01 | 0:2d3fcb6dabd4 | 138 | } |
keithm01 | 0:2d3fcb6dabd4 | 139 | |
keithm01 | 0:2d3fcb6dabd4 | 140 | void OLED::writeData(uint8_t value) |
keithm01 | 0:2d3fcb6dabd4 | 141 | { |
keithm01 | 0:2d3fcb6dabd4 | 142 | _dc = 1; |
keithm01 | 2:ef14c6dd6b93 | 143 | |
keithm01 | 0:2d3fcb6dabd4 | 144 | _cs = 0; |
keithm01 | 0:2d3fcb6dabd4 | 145 | _spi.write(value); |
keithm01 | 0:2d3fcb6dabd4 | 146 | _cs = 1; |
keithm01 | 0:2d3fcb6dabd4 | 147 | } |
keithm01 | 0:2d3fcb6dabd4 | 148 | //Turns on OLED sleep mode |
keithm01 | 0:2d3fcb6dabd4 | 149 | void OLED::sleep() |
keithm01 | 0:2d3fcb6dabd4 | 150 | { |
keithm01 | 0:2d3fcb6dabd4 | 151 | writeCommand(0xAE); |
keithm01 | 0:2d3fcb6dabd4 | 152 | } |
keithm01 | 0:2d3fcb6dabd4 | 153 | //Turns off OLED sleep mode |
keithm01 | 0:2d3fcb6dabd4 | 154 | void OLED::wake() |
keithm01 | 0:2d3fcb6dabd4 | 155 | { |
keithm01 | 0:2d3fcb6dabd4 | 156 | writeCommand(0xAF); |
keithm01 | 0:2d3fcb6dabd4 | 157 | } |
keithm01 | 0:2d3fcb6dabd4 | 158 | |
keithm01 | 4:9c0b59439725 | 159 | void OLED::draw() |
keithm01 | 4:9c0b59439725 | 160 | { |
keithm01 | 4:9c0b59439725 | 161 | if(USE_BUFFER) { |
keithm01 | 4:9c0b59439725 | 162 | writeCommand(OLED_CMD_SET_COLUMN); |
keithm01 | 4:9c0b59439725 | 163 | writeData(16); |
keithm01 | 4:9c0b59439725 | 164 | writeData(111); |
keithm01 | 4:9c0b59439725 | 165 | writeCommand(OLED_CMD_SET_ROW); |
keithm01 | 4:9c0b59439725 | 166 | writeData(0); |
keithm01 | 4:9c0b59439725 | 167 | writeData(95); |
keithm01 | 4:9c0b59439725 | 168 | writeCommand(OLED_CMD_WRITERAM); |
keithm01 | 4:9c0b59439725 | 169 | |
keithm01 | 4:9c0b59439725 | 170 | _dc = 1; |
keithm01 | 4:9c0b59439725 | 171 | _cs = 0; |
keithm01 | 4:9c0b59439725 | 172 | |
keithm01 | 4:9c0b59439725 | 173 | for(uint32_t i = 0; i < OLED_WIDTH * OLED_HEIGHT; ++ i) { |
keithm01 | 4:9c0b59439725 | 174 | writeData(displayBuffer[i] >> 8); |
keithm01 | 4:9c0b59439725 | 175 | writeData(displayBuffer[i]); |
keithm01 | 4:9c0b59439725 | 176 | } |
keithm01 | 4:9c0b59439725 | 177 | |
keithm01 | 4:9c0b59439725 | 178 | _cs = 1; |
keithm01 | 4:9c0b59439725 | 179 | } |
keithm01 | 4:9c0b59439725 | 180 | } |
keithm01 | 4:9c0b59439725 | 181 | |
keithm01 | 0:2d3fcb6dabd4 | 182 | void OLED::cursor(int x, int y) //goTo |
keithm01 | 0:2d3fcb6dabd4 | 183 | { |
keithm01 | 0:2d3fcb6dabd4 | 184 | if ((x >= OLED_WIDTH) || (y >= OLED_HEIGHT)) return; |
keithm01 | 1:82ccc138bbe6 | 185 | x+=OLED_COL_OFFSET; |
keithm01 | 0:2d3fcb6dabd4 | 186 | // set x and y coordinate |
keithm01 | 0:2d3fcb6dabd4 | 187 | writeCommand(OLED_CMD_SET_COLUMN); |
keithm01 | 0:2d3fcb6dabd4 | 188 | writeData(x); |
keithm01 | 0:2d3fcb6dabd4 | 189 | writeData(OLED_WIDTH-1); |
keithm01 | 0:2d3fcb6dabd4 | 190 | |
keithm01 | 0:2d3fcb6dabd4 | 191 | writeCommand(OLED_CMD_SET_ROW); |
keithm01 | 0:2d3fcb6dabd4 | 192 | writeData(y); |
keithm01 | 0:2d3fcb6dabd4 | 193 | writeData(OLED_HEIGHT-1); |
keithm01 | 0:2d3fcb6dabd4 | 194 | |
keithm01 | 0:2d3fcb6dabd4 | 195 | writeCommand(OLED_CMD_WRITERAM); |
keithm01 | 0:2d3fcb6dabd4 | 196 | } |
keithm01 | 0:2d3fcb6dabd4 | 197 | |
keithm01 | 2:ef14c6dd6b93 | 198 | void OLED::pixel(int16_t x, int16_t y, uint16_t color) |
keithm01 | 2:ef14c6dd6b93 | 199 | { |
keithm01 | 2:ef14c6dd6b93 | 200 | // Bounds check. |
keithm01 | 2:ef14c6dd6b93 | 201 | if ((x >= OLED_WIDTH) || (y >= OLED_HEIGHT)) return; |
keithm01 | 2:ef14c6dd6b93 | 202 | if ((x < 0) || (y < 0)) return; |
keithm01 | 4:9c0b59439725 | 203 | if(!USE_BUFFER) { |
keithm01 | 4:9c0b59439725 | 204 | cursor(x, y); |
keithm01 | 2:ef14c6dd6b93 | 205 | |
keithm01 | 4:9c0b59439725 | 206 | writeCommand(OLED_CMD_WRITERAM); |
keithm01 | 4:9c0b59439725 | 207 | _dc = 1; |
keithm01 | 4:9c0b59439725 | 208 | _cs = 0; |
keithm01 | 2:ef14c6dd6b93 | 209 | |
keithm01 | 4:9c0b59439725 | 210 | writeData(color >> 8); |
keithm01 | 4:9c0b59439725 | 211 | writeData(color); |
keithm01 | 2:ef14c6dd6b93 | 212 | |
keithm01 | 4:9c0b59439725 | 213 | _cs = 1; |
keithm01 | 4:9c0b59439725 | 214 | } else { |
keithm01 | 4:9c0b59439725 | 215 | displayBuffer[x+OLED_WIDTH*y] = color; |
keithm01 | 4:9c0b59439725 | 216 | } |
keithm01 | 2:ef14c6dd6b93 | 217 | } |
keithm01 | 2:ef14c6dd6b93 | 218 | |
keithm01 | 2:ef14c6dd6b93 | 219 | void OLED::clear (uint16_t color) |
keithm01 | 2:ef14c6dd6b93 | 220 | { |
keithm01 | 1:82ccc138bbe6 | 221 | rect(0, 0, OLED_WIDTH, OLED_HEIGHT, color); |
keithm01 | 1:82ccc138bbe6 | 222 | } |
keithm01 | 0:2d3fcb6dabd4 | 223 | void OLED::rect(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t fillcolor) |
keithm01 | 0:2d3fcb6dabd4 | 224 | { |
keithm01 | 0:2d3fcb6dabd4 | 225 | // Bounds check |
keithm01 | 0:2d3fcb6dabd4 | 226 | if ((x >= OLED_WIDTH) || (y >= OLED_HEIGHT)) |
keithm01 | 0:2d3fcb6dabd4 | 227 | return; |
keithm01 | 0:2d3fcb6dabd4 | 228 | |
keithm01 | 0:2d3fcb6dabd4 | 229 | // Y bounds check |
keithm01 | 0:2d3fcb6dabd4 | 230 | if (y+h > OLED_HEIGHT) { |
keithm01 | 0:2d3fcb6dabd4 | 231 | h = OLED_HEIGHT - y - 1; |
keithm01 | 0:2d3fcb6dabd4 | 232 | } |
keithm01 | 0:2d3fcb6dabd4 | 233 | |
keithm01 | 0:2d3fcb6dabd4 | 234 | // X bounds check |
keithm01 | 0:2d3fcb6dabd4 | 235 | if (x+w > OLED_WIDTH) { |
keithm01 | 0:2d3fcb6dabd4 | 236 | w = OLED_WIDTH - x - 1; |
keithm01 | 0:2d3fcb6dabd4 | 237 | } |
keithm01 | 4:9c0b59439725 | 238 | if(!USE_BUFFER) { |
keithm01 | 4:9c0b59439725 | 239 | x+= OLED_COL_OFFSET; |
keithm01 | 0:2d3fcb6dabd4 | 240 | |
keithm01 | 4:9c0b59439725 | 241 | // set location |
keithm01 | 4:9c0b59439725 | 242 | writeCommand(OLED_CMD_SET_COLUMN); |
keithm01 | 4:9c0b59439725 | 243 | writeData(x); |
keithm01 | 4:9c0b59439725 | 244 | writeData(x+(w-1)); |
keithm01 | 4:9c0b59439725 | 245 | writeCommand(OLED_CMD_SET_ROW); |
keithm01 | 4:9c0b59439725 | 246 | writeData(y); |
keithm01 | 4:9c0b59439725 | 247 | writeData(y+(h-1)); |
keithm01 | 4:9c0b59439725 | 248 | // fill! |
keithm01 | 4:9c0b59439725 | 249 | writeCommand(OLED_CMD_WRITERAM); |
keithm01 | 2:ef14c6dd6b93 | 250 | |
keithm01 | 4:9c0b59439725 | 251 | for (uint16_t i=0; i < w*h; i++) { |
keithm01 | 4:9c0b59439725 | 252 | writeData(fillcolor >> 8); |
keithm01 | 4:9c0b59439725 | 253 | writeData(fillcolor); |
keithm01 | 4:9c0b59439725 | 254 | } |
keithm01 | 4:9c0b59439725 | 255 | } else { |
keithm01 | 4:9c0b59439725 | 256 | for(uint16_t _w = 0; _w < w; ++_w) { |
keithm01 | 4:9c0b59439725 | 257 | for(uint16_t _h = 0; _h < h; ++_h) { |
keithm01 | 4:9c0b59439725 | 258 | displayBuffer[((x+_w)+OLED_WIDTH*(y+_h))] = fillcolor; |
keithm01 | 4:9c0b59439725 | 259 | } |
keithm01 | 4:9c0b59439725 | 260 | } |
keithm01 | 0:2d3fcb6dabd4 | 261 | } |
keithm01 | 0:2d3fcb6dabd4 | 262 | } |
keithm01 | 0:2d3fcb6dabd4 | 263 | |
keithm01 | 2:ef14c6dd6b93 | 264 | void OLED::circle(uint16_t x, uint16_t y, uint16_t radius, uint16_t color, uint16_t width) |
keithm01 | 2:ef14c6dd6b93 | 265 | { |
keithm01 | 2:ef14c6dd6b93 | 266 | const float DEG2RAD = 3.14159f/180; |
keithm01 | 2:ef14c6dd6b93 | 267 | for(uint16_t r = 0; r < width; ++r) { |
keithm01 | 2:ef14c6dd6b93 | 268 | for(uint16_t i = 0; i < 360; ++i) { |
keithm01 | 2:ef14c6dd6b93 | 269 | float degInRad = i*DEG2RAD; |
keithm01 | 2:ef14c6dd6b93 | 270 | uint16_t x2 = x + cos(degInRad) * (radius+r); |
keithm01 | 2:ef14c6dd6b93 | 271 | uint16_t y2 = y + sin(degInRad) * (radius+r); |
keithm01 | 4:9c0b59439725 | 272 | if(USE_BUFFER) { |
keithm01 | 4:9c0b59439725 | 273 | displayBuffer[((x2)+OLED_WIDTH*(y2))] = color; |
keithm01 | 4:9c0b59439725 | 274 | } else { |
keithm01 | 4:9c0b59439725 | 275 | pixel(x2, y2, color); |
keithm01 | 4:9c0b59439725 | 276 | } |
keithm01 | 2:ef14c6dd6b93 | 277 | } |
keithm01 | 2:ef14c6dd6b93 | 278 | } |
keithm01 | 2:ef14c6dd6b93 | 279 | } |
keithm01 | 2:ef14c6dd6b93 | 280 | |
keithm01 | 4:9c0b59439725 | 281 | void buffer_swap( |
keithm01 | 4:9c0b59439725 | 282 | uint16_t* imgDst, |
keithm01 | 4:9c0b59439725 | 283 | const uint8_t* imgSrc, |
keithm01 | 4:9c0b59439725 | 284 | uint16_t imgSize |
keithm01 | 4:9c0b59439725 | 285 | ) |
keithm01 | 4:9c0b59439725 | 286 | { |
keithm01 | 4:9c0b59439725 | 287 | for ( int var = 0; var < imgSize; var++ ) { |
keithm01 | 4:9c0b59439725 | 288 | *imgDst = *imgSrc << 8; |
keithm01 | 4:9c0b59439725 | 289 | imgSrc++; |
keithm01 | 4:9c0b59439725 | 290 | *imgDst |= *imgSrc; |
keithm01 | 4:9c0b59439725 | 291 | imgDst++; |
keithm01 | 4:9c0b59439725 | 292 | imgSrc++; |
keithm01 | 4:9c0b59439725 | 293 | } |
keithm01 | 4:9c0b59439725 | 294 | } |
keithm01 | 4:9c0b59439725 | 295 | |
keithm01 | 4:9c0b59439725 | 296 | |
keithm01 | 4:9c0b59439725 | 297 | void OLED::image (uint16_t x, uint16_t y, const uint8_t* image) |
keithm01 | 2:ef14c6dd6b93 | 298 | { |
keithm01 | 4:9c0b59439725 | 299 | x+= OLED_COL_OFFSET; |
keithm01 | 4:9c0b59439725 | 300 | //cursor(x, y); |
keithm01 | 4:9c0b59439725 | 301 | |
keithm01 | 4:9c0b59439725 | 302 | uint16_t w = image[2]; |
keithm01 | 4:9c0b59439725 | 303 | uint16_t h = image[4]; |
keithm01 | 4:9c0b59439725 | 304 | |
keithm01 | 4:9c0b59439725 | 305 | uint16_t image_data_size = w * h * OLED_BYTES_PER_PIXEL; |
keithm01 | 2:ef14c6dd6b93 | 306 | |
keithm01 | 4:9c0b59439725 | 307 | if(!USE_BUFFER) { |
keithm01 | 4:9c0b59439725 | 308 | writeCommand(OLED_CMD_SET_COLUMN); |
keithm01 | 4:9c0b59439725 | 309 | writeData(x); |
keithm01 | 4:9c0b59439725 | 310 | writeData(x+(w-1)); |
keithm01 | 4:9c0b59439725 | 311 | writeCommand(OLED_CMD_SET_ROW); |
keithm01 | 4:9c0b59439725 | 312 | writeData(y); |
keithm01 | 4:9c0b59439725 | 313 | writeData(y+(h-1)); |
keithm01 | 2:ef14c6dd6b93 | 314 | |
keithm01 | 4:9c0b59439725 | 315 | writeCommand(OLED_CMD_WRITERAM); |
keithm01 | 4:9c0b59439725 | 316 | _dc = 1; |
keithm01 | 4:9c0b59439725 | 317 | _cs = 0; |
keithm01 | 4:9c0b59439725 | 318 | |
keithm01 | 4:9c0b59439725 | 319 | const uint8_t* buffer = OLED_SKIP_IMAGE_HEADER(image); |
keithm01 | 4:9c0b59439725 | 320 | |
keithm01 | 4:9c0b59439725 | 321 | for ( uint32_t i = 0; i < image_data_size; i++) { |
keithm01 | 4:9c0b59439725 | 322 | _spi.write(*buffer); |
keithm01 | 4:9c0b59439725 | 323 | buffer += 1; |
keithm01 | 4:9c0b59439725 | 324 | } |
keithm01 | 4:9c0b59439725 | 325 | |
keithm01 | 4:9c0b59439725 | 326 | } else { |
keithm01 | 4:9c0b59439725 | 327 | const uint8_t* buffer = OLED_SKIP_IMAGE_HEADER(image); |
keithm01 | 4:9c0b59439725 | 328 | buffer_swap(displayBuffer, buffer, image_data_size); |
keithm01 | 2:ef14c6dd6b93 | 329 | } |
keithm01 | 2:ef14c6dd6b93 | 330 | |
keithm01 | 2:ef14c6dd6b93 | 331 | } |
keithm01 | 2:ef14c6dd6b93 | 332 | |
keithm01 | 4:9c0b59439725 | 333 | |
keithm01 | 1:82ccc138bbe6 | 334 | void OLED::dim() |
keithm01 | 1:82ccc138bbe6 | 335 | { |
keithm01 | 2:ef14c6dd6b93 | 336 | for ( int i = 0; i < 16; i++ ) { |
keithm01 | 1:82ccc138bbe6 | 337 | writeCommand( OLED_CMD_CONTRASTMASTER); |
keithm01 | 1:82ccc138bbe6 | 338 | writeData( 0xC0 | (0xF-i)); |
keithm01 | 1:82ccc138bbe6 | 339 | wait( 20 ); |
keithm01 | 1:82ccc138bbe6 | 340 | } |
keithm01 | 1:82ccc138bbe6 | 341 | } |
keithm01 | 1:82ccc138bbe6 | 342 | |
keithm01 | 4:9c0b59439725 | 343 | void OLED::hline(uint16_t x, uint16_t y, uint16_t width, uint16_t color, uint16_t thickness) |
keithm01 | 4:9c0b59439725 | 344 | { |
keithm01 | 4:9c0b59439725 | 345 | if(!USE_BUFFER) { |
keithm01 | 4:9c0b59439725 | 346 | //x+= OLED_COL_OFFSET; |
keithm01 | 4:9c0b59439725 | 347 | // set location |
keithm01 | 4:9c0b59439725 | 348 | writeCommand(OLED_CMD_SET_COLUMN); |
keithm01 | 4:9c0b59439725 | 349 | writeData(x); |
keithm01 | 4:9c0b59439725 | 350 | writeData(x+width-1); |
keithm01 | 4:9c0b59439725 | 351 | writeCommand(OLED_CMD_SET_ROW); |
keithm01 | 4:9c0b59439725 | 352 | writeData(y); |
keithm01 | 4:9c0b59439725 | 353 | writeData(y+thickness-1); |
keithm01 | 4:9c0b59439725 | 354 | // fill! |
keithm01 | 4:9c0b59439725 | 355 | writeCommand(OLED_CMD_WRITERAM); |
keithm01 | 4:9c0b59439725 | 356 | |
keithm01 | 4:9c0b59439725 | 357 | for (uint16_t i=0; i < width*thickness; i++) { |
keithm01 | 4:9c0b59439725 | 358 | writeData(color >> 8); |
keithm01 | 4:9c0b59439725 | 359 | writeData(color); |
keithm01 | 4:9c0b59439725 | 360 | } |
keithm01 | 4:9c0b59439725 | 361 | } else { |
keithm01 | 4:9c0b59439725 | 362 | |
keithm01 | 4:9c0b59439725 | 363 | for (uint16_t w=0; w < width; w++) { |
keithm01 | 4:9c0b59439725 | 364 | for (uint16_t h=0; h < thickness; h++) { |
keithm01 | 4:9c0b59439725 | 365 | displayBuffer[((x+w)+OLED_WIDTH*(y+h))] = color; |
keithm01 | 4:9c0b59439725 | 366 | } |
keithm01 | 4:9c0b59439725 | 367 | } |
keithm01 | 4:9c0b59439725 | 368 | } |
keithm01 | 4:9c0b59439725 | 369 | |
keithm01 | 4:9c0b59439725 | 370 | } |
keithm01 | 4:9c0b59439725 | 371 | |
keithm01 | 4:9c0b59439725 | 372 | void OLED::vline(uint16_t x, uint16_t y, uint16_t height, uint16_t color, uint16_t thickness) |
keithm01 | 4:9c0b59439725 | 373 | { |
keithm01 | 4:9c0b59439725 | 374 | if(!USE_BUFFER) { |
keithm01 | 4:9c0b59439725 | 375 | x+= OLED_COL_OFFSET; |
keithm01 | 4:9c0b59439725 | 376 | // set location |
keithm01 | 4:9c0b59439725 | 377 | writeCommand(OLED_CMD_SET_COLUMN); |
keithm01 | 4:9c0b59439725 | 378 | writeData(x); |
keithm01 | 4:9c0b59439725 | 379 | writeData(x+thickness-1); |
keithm01 | 4:9c0b59439725 | 380 | writeCommand(OLED_CMD_SET_ROW); |
keithm01 | 4:9c0b59439725 | 381 | writeData(y); |
keithm01 | 4:9c0b59439725 | 382 | writeData(y+height-1); |
keithm01 | 4:9c0b59439725 | 383 | // fill! |
keithm01 | 4:9c0b59439725 | 384 | writeCommand(OLED_CMD_WRITERAM); |
keithm01 | 4:9c0b59439725 | 385 | |
keithm01 | 4:9c0b59439725 | 386 | for (uint16_t i=0; i < height*thickness; i++) { |
keithm01 | 4:9c0b59439725 | 387 | writeData(color >> 8); |
keithm01 | 4:9c0b59439725 | 388 | writeData(color); |
keithm01 | 4:9c0b59439725 | 389 | } |
keithm01 | 4:9c0b59439725 | 390 | } else { |
keithm01 | 4:9c0b59439725 | 391 | for (uint16_t w=0; w < thickness; w++) { |
keithm01 | 4:9c0b59439725 | 392 | for (uint16_t h=0; h < height; h++) { |
keithm01 | 4:9c0b59439725 | 393 | displayBuffer[((x+w)+OLED_WIDTH*(y+h))] = color; |
keithm01 | 4:9c0b59439725 | 394 | } |
keithm01 | 4:9c0b59439725 | 395 | } |
keithm01 | 4:9c0b59439725 | 396 | } |
keithm01 | 4:9c0b59439725 | 397 | } |
keithm01 | 4:9c0b59439725 | 398 | |
keithm01 | 4:9c0b59439725 | 399 | void OLED::text(uint16_t x, uint16_t y, const uint8_t* text, const uint8_t* font, uint16_t color) |
keithm01 | 4:9c0b59439725 | 400 | { |
keithm01 | 4:9c0b59439725 | 401 | |
keithm01 | 4:9c0b59439725 | 402 | x += OLED_COL_OFFSET; |
keithm01 | 4:9c0b59439725 | 403 | uint8_t pad = 2; |
keithm01 | 4:9c0b59439725 | 404 | |
keithm01 | 4:9c0b59439725 | 405 | uint8_t firstChar = font[2]; |
keithm01 | 4:9c0b59439725 | 406 | uint8_t charHeight = font[6]; |
keithm01 | 4:9c0b59439725 | 407 | |
keithm01 | 4:9c0b59439725 | 408 | uint16_t current_char = 0; |
keithm01 | 4:9c0b59439725 | 409 | while(text[current_char] != 0) { |
keithm01 | 4:9c0b59439725 | 410 | |
keithm01 | 4:9c0b59439725 | 411 | ++current_char; |
keithm01 | 4:9c0b59439725 | 412 | } |
keithm01 | 4:9c0b59439725 | 413 | } |
keithm01 | 4:9c0b59439725 | 414 | |
keithm01 | 0:2d3fcb6dabd4 | 415 | uint16_t Color565(uint8_t r, uint8_t g, uint8_t b) |
keithm01 | 0:2d3fcb6dabd4 | 416 | { |
keithm01 | 0:2d3fcb6dabd4 | 417 | uint16_t c; |
keithm01 | 0:2d3fcb6dabd4 | 418 | c = r >> 3; |
keithm01 | 0:2d3fcb6dabd4 | 419 | c <<= 6; |
keithm01 | 0:2d3fcb6dabd4 | 420 | c |= g >> 2; |
keithm01 | 0:2d3fcb6dabd4 | 421 | c <<= 5; |
keithm01 | 0:2d3fcb6dabd4 | 422 | c |= b >> 3; |
keithm01 | 0:2d3fcb6dabd4 | 423 | |
keithm01 | 0:2d3fcb6dabd4 | 424 | return c; |
keithm01 | 0:2d3fcb6dabd4 | 425 | } |