summaryrefslogtreecommitdiff
path: root/peripheral/libupm/src/lcd/eboled.h
blob: bd6f3d0837641f185055adaa972e31d9239c1a1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
 * Author: Jon Trulson <jtrulson@ics.com>
 * Copyright (c) 2015 Intel Corporation.
 *
 * Author: Tyler Gibson <tgibson@microsoft.com>
 * Copyright (c) 2015 Microsoft Corporation.
 *
 * Credits to Adafruit.
 * Based on Adafruit ST7735 library, see original license in license.txt file.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#pragma once

#include <string>
#include <mraa/spi.hpp>

#include <mraa/gpio.hpp>

#include "lcd.h"
#include "ssd.h"

#define EBOLED_DEFAULT_SPI_BUS 0
#define EBOLED_DEFAULT_CD      36
#define EBOLED_DEFAULT_RESET   48

#define swap(a, b) { uint8_t t = a; a = b; b = t; }

namespace upm
{
  const uint8_t COLOR_WHITE     = 0x01;
  const uint8_t COLOR_BLACK     = 0x00;
  const uint8_t COLOR_XOR       = 0x02;
  const uint8_t OLED_WIDTH      = 0x40; // 64 pixels
  const uint8_t VERT_COLUMNS    = 0x20; // half width for hi/lo 16bit writes.
  const uint8_t OLED_HEIGHT     = 0x30; // 48 pixels
  const int     BUFFER_SIZE     = 192;

  /**
   * @library i2clcd
   * @sensor eboled
   * @comname Sparkfun Edison Block OLED Display
   * @altname ssd1306
   * @type display
   * @man sparkfun
   * @web https://www.sparkfun.com/products/13035
   * @con spi
   *
   * @brief API for EBOLED spi controlled OLED display
   *
   * The EBOLED is an ssd1306 with some modifications to work as an
   * Edison Block.  It is a 64x48 pixel OLED display that connects
   * directly to an edison via it's 80-pin connector.  Edison Blocks
   * are stackable modules created by Sparkfun.
   *
   * This block has some buttons on it that can be accessed using
   * standard GPIO -- this driver only concerns itself with the
   * display.
   *
   * @image html eboled.jpg
   * <br><em>OLED Sensor image provided by SparkFun* under
   * <a href=https://creativecommons.org/licenses/by-nc-sa/3.0/>
   * CC BY-NC-SA-3.0</a>.</em>
   *
   * @snippet eboled.cxx Interesting
   */
  class EBOLED : public LCD
  {
    // SSD commands
    typedef enum {
      CMD_SETLOWCOLUMN          = 0x00,
      CMD_EXTERNALVCC           = 0x01,
      CMD_SWITCHCAPVCC          = 0x02,
      CMD_SETHIGHCOLUMN         = 0x10,
      CMD_MEMORYADDRMODE        = 0x20,
      CMD_SETCOLUMNADDRESS      = 0x21,
      CMD_SETPAGEADDRESS        = 0x22,
      CMD_SETSTARTLINE          = 0x40, // 0x40 - 0x7f
      CMD_SETCONTRAST           = 0x81,
      CMD_CHARGEPUMP            = 0x8d,
      CMD_SEGREMAP              = 0xa0,
      CMD_DISPLAYALLONRESUME    = 0xa4,
      CMD_DISPLAYALLON          = 0xa5,
      CMD_NORMALDISPLAY         = 0xa6,
      CMD_INVERTDISPLAY         = 0xa7,
      CMD_SETMULTIPLEX          = 0xa8,
      CMD_DISPLAYOFF            = 0xae,
      CMD_DISPLAYON             = 0xaf,
      CMD_SETPAGESTARTADDR      = 0xb0, // 0xb0-0xb7
      CMD_COMSCANINC            = 0xc0,
      CMD_COMSCANDEC            = 0xc8,
      CMD_SETDISPLAYOFFSET      = 0xd3,
      CMD_SETDISPLAYCLOCKDIV    = 0xd5,
      CMD_SETPRECHARGE          = 0xd9,
      CMD_SETCOMPINS            = 0xda,
      CMD_SETVCOMDESELECT       = 0xdb
    } SSD_CMDS_T;

  public:
    /**
     * EBOLED Constructor.  Note that you will not have any choice as
     * to the pins used, so they are all set to default values.
     *
     * @param spi spi bus to use
     * @param CD Command/Data select pin
     * @param reset reset pin
     * @param address the slave address the lcd is registered on
     */
    EBOLED(int spi=EBOLED_DEFAULT_SPI_BUS, int CD=EBOLED_DEFAULT_CD,
           int reset=EBOLED_DEFAULT_RESET);

    /**
     * EBOLED Destructor
     */
    ~EBOLED();

    /**
     * Draw the buffer to screen.
     *
     * @return result of operation
     */
    mraa::Result refresh();

    /**
     * Write a string to LCD
     *
     * @param msg the std::string to write to display, note only ascii
     * chars are supported
     * @return result of operation
     */
    mraa::Result write(std::string msg);

    /**
     * Set cursor to a coordinate
     *
     * @param y Axis on the vertical scale. This device supports 6 rows.
     * @param x Axis on the horizontal scale This device supports 64 columns
     *
     * @return result of operation
     */
    mraa::Result setCursor (int row, int column);

    /**
      * Sets a text color for a message
      *
      * @param textColor Font color: COLOR_WHITE, COLOR_BLACK or COLOR_XOR
      */
    void setTextColor (uint8_t textColor);

    /**
      * Sets the size of the font
      *
      * @param size Font size
      */
    void setTextSize (uint8_t size);

    /**
      * Wraps a printed message
      *
      * @param wrap True (1) or false (0)
      */
    void setTextWrap (uint8_t wrap);

    /**
      * Write a single character to the screen.
      *
      * @param x Axis on the horizontal scale
      * @param y Axis on the vertical scale
      * @param data Character to write
      * @param color Character color
      * @param bg Character background color
      * @param size Size of the font
      */
    void drawChar (uint8_t x, uint8_t y, uint8_t data, uint8_t color, uint8_t size);

    /**
     * Clear display
     *
     * @return result of operation
     */
    mraa::Result clear();

    void clearScreenBuffer();

    /**
     * Return to coordinate 0,0
     *
     * @return result of operation
     */
    mraa::Result home();

    /**
     * Write a single pixel to the screen buffer.
     * Can do an specific color write or toggle (xor) a pixel.
     *
     * @param x the x position of the pixel
     * @param y the y position of the pixel
     * @param color pixel is COLOR_WHITE, COLOR_BLACK or COLOR_XOR
     */
    void drawPixel (int8_t x, int8_t y, uint8_t color=COLOR_WHITE);

    /**
     * Draw a line to the screen buffer.
     *
     * @param x0 the x position of the beginning of the line
     * @param y0 the y position of the beginning of the line
     * @param x1 the x position of the end of the line
     * @param y1 the y position of the end of the line
     * @param color line is COLOR_WHITE, COLOR_BLACK or COLOR_XOR
     */
    void drawLine (int8_t x0, int8_t y0, int8_t x1, int8_t y1, uint8_t color = COLOR_WHITE);

    /**
     * Draw a horizontal line to the screen buffer.
     *
     * @param x the x position of the beginning of the line
     * @param y the y position of the beginning of the line
     * @param width is the horizontal length of the line
     * @param color line is COLOR_WHITE, COLOR_BLACK or COLOR_XOR
     */
    void drawLineHorizontal (int8_t x, int8_t y, uint8_t width, uint8_t color = COLOR_WHITE);

    /**
     * Draw a vertical line to the screen buffer.
     *
     * @param x the x position of the beginning of the line
     * @param y the y position of the beginning of the line
     * @param width is the vertical length of the line
     * @param color line is COLOR_WHITE, COLOR_BLACK or COLOR_XOR
     */
    void drawLineVertical (int8_t x, int8_t y, uint8_t height, uint8_t color = COLOR_WHITE);

    /**
     * Draw a rectangle to the screen buffer.
     *
     * @param x the left edge
     * @param y the top edge
     * @param width sets the right edge
     * @param height bottom edge
     * @param color outline is COLOR_WHITE, COLOR_BLACK or COLOR_XOR
     */
    void drawRectangle (int8_t x, int8_t y, uint8_t width, uint8_t height, uint8_t color = COLOR_WHITE);

    /**
     * Draw a rectangle with rounded corners to the screen buffer.
     *
     * @param x the left edge
     * @param y the top edge
     * @param width sets the right edge
     * @param height bottom edge
     * @param radius of the rounded corners
     * @param color outline is COLOR_WHITE, COLOR_BLACK or COLOR_XOR
     */
    void drawRoundedRectangle(int8_t x, int8_t y, int8_t width, int8_t height, int16_t radius, uint8_t color);

    /**
     * Draw a filled rectangle to the screen buffer.
     *
     * @param x the left edge
     * @param y the top edge
     * @param width sets the right edge
     * @param height bottom edge
     * @param color fill color is COLOR_WHITE, COLOR_BLACK or COLOR_XOR
     */
    void drawRectangleFilled (int8_t x, int8_t y, uint8_t width, uint8_t height, uint8_t color = COLOR_WHITE);

    /**
     * Draw a triangle to the screen buffer.
     *
     * @param x0 the x coordinate of the first corner
     * @param y0 the y coordinate of the first corner
     * @param x1 the x coordinate of the second corner
     * @param y1 the y coordinate of the second corner
     * @param x2 the x coordinate of the third corner
     * @param y2 the y coordinate of the third corner
     * @param color outline is COLOR_WHITE, COLOR_BLACK or COLOR_XOR
     */
    void drawTriangle (int8_t x0, int8_t y0, int8_t x1, int8_t y1, int8_t x2, int8_t y2, uint8_t color = COLOR_WHITE);

    /**
     * Draw a filled triangle to the screen buffer.
     *
     * @param x0 the x coordinate of the first corner
     * @param y0 the y coordinate of the first corner
     * @param x1 the x coordinate of the second corner
     * @param y1 the y coordinate of the second corner
     * @param x2 the x coordinate of the third corner
     * @param y2 the y coordinate of the third corner
     * @param color fill color is COLOR_WHITE, COLOR_BLACK or COLOR_XOR
     */
    void drawTriangleFilled ( int8_t x0, int8_t y0, int8_t x1, int8_t y1, int8_t x2, int8_t y2, uint8_t color);

    /**
     * Draw a circle to the screen buffer.
     *
     * @param x0 the x coordinate of the circle's center
     * @param y0 the y coordinate of the circle's center
     * @param radius the radius of the circle
     * @param color outline is COLOR_WHITE, COLOR_BLACK or COLOR_XOR
     */
    void drawCircle (int16_t x0, int16_t y0, int16_t radius, uint8_t color = COLOR_WHITE);

    /**
     * Draw a quarter circle arc to the screen buffer.
     *
     * @param x0 the x coordinate of the arc's center
     * @param y0 the y coordinate of the arc's center
     * @param radius the radius of the arc
     * @param cornername denotes which of the 4 quarters to draw - 1,2,4,8
     * @param color outline is COLOR_WHITE, COLOR_BLACK or COLOR_XOR
     */
    void drawRoundCorners( int8_t x0, int8_t y0, int16_t radius, uint8_t cornername, uint8_t color);

    /**
     * Draw a filled circle to the screen buffer.
     *
     * @param x0 the x coordinate of the circle's center
     * @param y0 the y coordinate of the circle's center
     * @param radius the radius of the circle
     * @param color outline is COLOR_WHITE, COLOR_BLACK or COLOR_XOR
     */
    void drawCircleFilled(int8_t x0, int8_t y0, int16_t radius, uint8_t color);

    /**
     * Draw a quarter pie to the screen buffer.
     *
     * @param x0 the x coordinate of the arc's center
     * @param y0 the y coordinate of the arc's center
     * @param radius the radius of the arc
     * @param cornername denotes which of the 4 quarters to draw - 1,2,4,8
     * @param color fill color is COLOR_WHITE, COLOR_BLACK or COLOR_XOR
     */
    void drawRoundedCornersFilled(int8_t x0, int8_t y0, int16_t radius, uint8_t cornername, int16_t delta, uint8_t color);

    /**
     * Fill the screen buffer with specified color.
     *
     * @param color fill color is COLOR_WHITE, COLOR_BLACK or COLOR_XOR
     */
    void fillScreen (uint8_t color=COLOR_WHITE);


  protected:
    mraa::Result command(uint8_t cmd);
    mraa::Result data(uint16_t data);
    mraa::Result writeChar(uint8_t value);
    mraa::Result setAddressingMode(displayAddressingMode mode);

  private:
    mraa::Gpio m_gpioCD;        // command(0)/data(1)
    mraa::Gpio m_gpioRST;       // reset pin

    mraa::Spi m_spi;

    uint8_t m_cursorX;
    uint8_t m_cursorY;
    uint8_t m_textSize;
    uint8_t m_textColor;
    uint8_t m_textWrap;
  };
}