This repository has been archived on 2018-05-28. You can view files and clone it, but cannot push or open issues or pull requests.
BBB-Simple-ACS/client/lib/ssd1306.hpp

208 lines
9.7 KiB
C++

/*
* MIT License
Copyright (c) 2017 DeeplyEmbedded
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.
* SSD1306_OLED.h
*
* Created on : Sep 21, 2017
* Author : Vinay Divakar
* Website : www.deeplyembedded.org
*/
#pragma once
#include "spi_device.hpp"
#include "gpio.hpp"
#define DISPLAY_BUFF_SIZE (SSD1306_LCDWIDTH*SSD1306_LCDHEIGHT/8)
/* COLOR MACROS */
#define WHITE 1
#define BLACK 0
#define INVERSE 2
/* Number output format */
#define DEC 10
#define HEX 16
#define OCT 8
#define BIN 2
#define DEFAULT 0
#define SSD1306_LCDWIDTH 128
#define SSD1306_LCDHEIGHT 64
/* SSD1306 Commands */
#define SSD1306_DISPLAY_OFF 0xAE
#define SSD1306_SET_DISP_CLK 0xD5
#define SSD1306_SET_MULTIPLEX 0xA8
#define SSD1306_SET_DISP_OFFSET 0xD3
#define SSD1306_SET_DISP_START_LINE 0x40
#define SSD1306_CONFIG_CHARGE_PUMP 0x8D
#define SSD1306_SET_MEM_ADDR_MODE 0x20
#define SSD1306_SEG_REMAP (0xA0 | 0x01)
#define SSD1306_SET_COMSCANDEC 0xC8
#define SSD1306_SET_COMPINS 0xDA
#define SSD1306_SET_CONTRAST 0x81
#define SSD1306_SET_PRECHARGE 0xD9
#define SSD1306_SET_VCOMDETECT 0xDB
#define SSD1306_DISPLAYALLON_RESUME 0xA4
#define SSD1306_NORMAL_DISPLAY 0xA6
#define SSD1306_DISPLAYON 0xAF
#define SSD1306_SET_COL_ADDR 0x21
#define SSD1306_PAGEADDR 0x22
#define SSD1306_INVERT_DISPLAY 0x01
#define SSD1306_NORMALIZE_DISPLAY 0x00
/* SDD1306 Scroll Commands */
#define SSD1306_SET_VERTICAL_SCROLL_AREA 0xA3
#define SSD1306_ACTIVATE_SCROLL 0x2F
#define SSD1306_DEACTIVATE_SCROLL 0x2E
#define SSD1306_RIGHT_HORIZONTAL_SCROLL 0x26
#define SSD1306_LEFT_HORIZONTAL_SCROLL 0x27
#define SSD1306_VERTICAL_AND_RIGHT_HORIZONTAL_SCROLL 0x29
#define SSD1306_VERTICAL_AND_LEFT_HORIZONTAL_SCROLL 0x2A
#define SSD1306_INVERTDISPLAY 0xA7
/* SSD1306 Configuration Commands */
#define SSD1306_DISPCLK_DIV 0x80
#define SSD1306_MULT_64 0x3F
#define SSD1306_DISP_OFFSET_VAL 0x00
#define SSD1306_COL_START_ADDR 0x00
#define SSD1306_COL_END_ADDR (SSD1306_LCDWIDTH - 1)
#define SSD1306_PG_START_ADDR 0x00
#define SSD1306_PG_END_ADDR 7
#define SSD1306_CHARGE_PUMP_EN 0x14
#define SSD1306_CONFIG_COM_PINS 0x12
#define SSD1306_CONTRAST_VAL 0xCF
#define SSD1306_PRECHARGE_VAL 0xF1
#define SSD1306_VCOMH_VAL 0x40
#define SSD1306_MULT_DAT (SSD1306_LCDHEIGHT - 1)
#define SSD1306_HOR_MM 0x00
#define TO_CONST_UCHAR_STR(str) (reinterpret_cast<const unsigned char*>(str))
typedef struct { // Data stored PER GLYPH
unsigned short bitmap_offset; // Pointer into GFXfont->bitmap
unsigned char width, height; // Bitmap dimensions in pixels
unsigned char x_advance; // Distance to advance cursor (x axis)
char x_offset, y_offset; // Dist from cursor pos to UL corner
} gf_xglyph_t, *gf_xglyph_ptr;
typedef struct { // Data stored for FONT AS A WHOLE:
unsigned char *bitmap; // Glyph bitmaps, concatenated
gf_xglyph_ptr glyph; // Glyph array
unsigned char first, last; // ASCII extents
unsigned char y_advance; // Newline distance (y axis)
} gf_xfont_t, *gf_xfont_ptr;
class ssd1306
{
gf_xfont_ptr gfx_font_ = nullptr;
unsigned char chunk_[16] = { 0 };
unsigned char screen_[DISPLAY_BUFF_SIZE] = { 0 };
const static unsigned char font[1280];
unsigned char rotation_ = 0;
unsigned char textsize_ = 0;
spi_device spi_;
gpio reset_;
gpio dc_;
short width_ = SSD1306_LCDWIDTH;
short height_ = SSD1306_LCDHEIGHT;
short cursor_x_ = 0, cursor_y_ = 0, textcolor_ = 0, textbgcolor_ = 0;
bool cp437_ = false, wrap_ = true;
void transfer();
void draw_fast_v_line(short x, short y, short h, short color);
void write_fast_v_line(short x, short y, short h, short color);
void draw_fast_h_line(short x, short y, short w, short color);
void write_fast_h_line(short x, short y, short w, short color);
short print(const unsigned char *buffer, short size);
int cmd(const unsigned char byte)
{
if (dc_.stream_write(gpio::low))
return -1;
return spi_.write(byte);
}
int data()
{
return dc_.stream_write(gpio::high);
}
public:
explicit ssd1306(unsigned bus, unsigned device, unsigned reset, unsigned dc);
~ssd1306();
void clear_display();
void display_init_seq();
void display();
void init_col_pg_addrs(unsigned char col_start_addr, unsigned char col_end_addr,
unsigned char pg_start_addr, unsigned char pg_end_addr);
void set_rotation(unsigned char x);
void start_scroll_right(unsigned char start, unsigned char stop);
void start_scroll_left(unsigned char start, unsigned char stop);
void start_scroll_diag_right(unsigned char start, unsigned char stop);
void start_scroll_diag_left(unsigned char start, unsigned char stop);
void stop_scroll();
void set_cursor(short x, short y);
short get_cursor_x() const;
short get_cursor_y() const;
unsigned char get_rotation() const;
void invert_display(unsigned char i);
signed char draw_pixel(short x, short y, short color);
void write_line(short x0, short y0, short x1, short y1, short color);
void draw_circle_helper( short x0, short y0, short r, unsigned char cornername, short color);
void draw_line(short x0, short y0, short x1, short y1, short color);
void draw_rect(short x, short y, short w, short h, short color);
void fill_rect(short x, short y, short w, short h, short color);
void draw_circle(short x0, short y0, short r, short color);
void fill_circle_helper(short x0, short y0, short r, unsigned char cornername, short delta, short color);
void fill_circle(short x0, short y0, short r, short color);
void draw_triangle(short x0, short y0, short x1, short y1, short x2, short y2, short color);
void fill_triangle(short x0, short y0, short x1, short y1, short x2, short y2, short color);
void draw_round_rect(short x, short y, short w, short h, short r, short color);
void fill_round_rect(short x, short y, short w, short h, short r, short color);
void draw_bitmap(short x, short y, const unsigned char* bitmap, short w, short h, short color);
short oled_write(unsigned char c);
void set_text_size(unsigned char s);
void set_text_color(short c);
void set_text_wrap(bool w);
void draw_char(short x, short y, unsigned char c, short color, short bg, unsigned char size);
short print_str(const char *strPtr);
short println();
short print_strln(const char *strPtr);
short print_number(unsigned long n, unsigned char base);
short print_number_ul(unsigned long n, int base);
short print_number_ul_ln(unsigned long num, int base);
short print_number_ui(unsigned int n, int base);
short print_number_ui_ln(unsigned int n, int base);
short print_number_uc(unsigned char b, int base);
short print_number_uc_ln(unsigned char b, int base);
short print_number_l(long n, int base);
short print_number_l_ln(long num, int base);
short print_number_i(int n, int base);
short print_number_i_ln(int n, int base);
short print_float(double number, unsigned char digits);
short print_float_ln(double num, int digits);
};