diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..198dfcf --- /dev/null +++ b/LICENSE @@ -0,0 +1,13 @@ +Copyright 2017 Cesanta Software Limited + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..e014185 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# Arduino Adafruit SSD1306 library for Mongoose OS diff --git a/mjs_fs/api_arduino_ssd1306.js b/mjs_fs/api_arduino_ssd1306.js new file mode 100644 index 0000000..5e43e3c --- /dev/null +++ b/mjs_fs/api_arduino_ssd1306.js @@ -0,0 +1,230 @@ +// Arduino Adafruit_SSD1306 library API. Source C API is defined at: +// [mgos_arduino_ssd1306.h](https://github.com/cesanta/mongoose-os/blob/master/arduino_drivers/Arduino/mgos_arduino_ssd1306.h) + +let Adafruit_SSD1306 = { + _ci2c: ffi('void *mgos_ssd1306_create_i2c(int, int)'), + _cspi: ffi('void *mgos_ssd1306_create_spi(int, int, int, int)'), + _close: ffi('void mgos_ssd1306_close(void *)'), + _begin: ffi('void mgos_ssd1306_begin(void *, int, int, int)'), + _cmd: ffi('void mgos_ssd1306_command(void *, int)'), + _cd: ffi('void mgos_ssd1306_clear_display(void *)'), + _id: ffi('void mgos_ssd1306_invert_display(void *, int)'), + _d: ffi('void mgos_ssd1306_display(void *)'), + _ssr: ffi('void mgos_ssd1306_start_scroll_right(void *, int, int)'), + _ssl: ffi('void mgos_ssd1306_start_scroll_left(void *, int, int)'), + _ssdr: ffi('void mgos_ssd1306_start_scroll_diag_right(void *, int, int)'), + _ssdl: ffi('void mgos_ssd1306_start_scroll_diag_left(void *, int, int)'), + _ss: ffi('void mgos_ssd1306_stop_scroll(void *)'), + _dim: ffi('void mgos_ssd1306_dim(void *, int)'), + _dp: ffi('void mgos_ssd1306_draw_pixel(void *, int, int, int)'), + _dfvl: ffi('void mgos_ssd1306_draw_fast_vline(void *, int, int, int, int)'), + _dfhl: ffi('void mgos_ssd1306_draw_fast_hline(void *, int, int, int, int)'), + _pair: ffi('int mgos_ssd1306_make_xy_pair(int, int)'), + _dc: ffi('void mgos_ssd1306_draw_circle(void *, int, int, int)'), + _dch: ffi('void mgos_ssd1306_draw_circle_helper(void *, int, int, int, int)'), + _fc: ffi('void mgos_ssd1306_fill_circle(void *, int, int, int)'), + _fch: ffi('void mgos_ssd1306_fill_circle_helper(void *, int, int, int, int, int)'), + _dt: ffi('void mgos_ssd1306_draw_triangle(void *, int, int, int, int)'), + _ft: ffi('void mgos_ssd1306_fill_triangle(void *, int, int, int, int)'), + _drr: ffi('void mgos_ssd1306_draw_round_rect(void *, int, int, int, int, int)'), + _frr: ffi('void mgos_ssd1306_fill_round_rect(void *, int, int, int, int, int)'), + _dch: ffi('void mgos_ssd1306_draw_char(void *, int, int, int, int, int)'), + _sc: ffi('void mgos_ssd1306_set_cursor(void *, int, int)'), + _stc: ffi('void mgos_ssd1306_set_text_color(void *, int)'), + _stcb: ffi('void mgos_ssd1306_set_text_color_bg(void *, int, int)'), + _sts: ffi('void mgos_ssd1306_set_text_size(void *, int)'), + _stw: ffi('void mgos_ssd1306_set_text_wrap(void *, int)'), + _w: ffi('int mgos_ssd1306_write(void *, char *, int)'), + _ht: ffi('int mgos_ssd1306_height(void *)'), + _wh: ffi('int mgos_ssd1306_width(void *)'), + _gr: ffi('int mgos_ssd1306_get_rotation(void *)'), + _gcx: ffi('int mgos_ssd1306_get_cursor_x(void *)'), + _gcy: ffi('int mgos_ssd1306_get_cursor_y(void *)'), + + RES_96_16: 0, + RES_128_32: 1, + RES_128_64: 2, + + EXTERNALVCC: 1, + SWITCHCAPVCC: 2, + + BLACK: 0, + WHITE: 1, + INVERSE: 2, + + _proto: { + // Close Adafruit_SSD1306 handle. Return value: none. + close: function() { + return Adafruit_SSD1306._close(this.ssd); + }, + + // Initialize the display. + begin: function(vccst, addr, reset) { + return Adafruit_SSD1306._begin(this.ssd, vccst, addr, reset); + }, + + ssd1306_command: function(cmd) { + return Adafruit_SSD1306._cmd(this.ssd, cmd); + }, + + clearDisplay: function() { + return Adafruit_SSD1306._cd(this.ssd); + }, + + invertDisplay: function(i) { + return Adafruit_SSD1306._id(this.ssd, i); + }, + + display: function() { + return Adafruit_SSD1306._d(this.ssd); + }, + + // Activates a right handed scroll for rows start through stop. + startScrollRight: function(start, stop) { + return Adafruit_SSD1306._ssr(this.ssd, start, stop); + }, + + // Activates a right handed scroll for rows start through stop. + startScrollLeft: function(start, stop) { + return Adafruit_SSD1306._ssl(this.ssd, start, stop); + }, + + // Activates a diagonal scroll for rows start through stop. + startScrollDiagRight: function(start, stop) { + return Adafruit_SSD1306._ssdr(this.ssd, start, stop); + }, + + // Activates a diagonal scroll for rows start through stop. + startScrollDiagLeft: function(start, stop) { + return Adafruit_SSD1306._ssdl(this.ssd, start, stop); + }, + + // Stops scroll. + stopScroll: function() { + return Adafruit_SSD1306._ss(this.ssd); + }, + + // Dims the display. + // dim = 1: display is dimmed; + // dim = 0: display is normal. + dim: function(dim) { + return Adafruit_SSD1306._dim(this.ssd, dim); + }, + + // Sets a single pixel. + drawPixel: function(x, y, color) { + return Adafruit_SSD1306._dp(this.ssd, x, y, color); + }, + + drawFastVLine: function(x, y, h, color) { + return Adafruit_SSD1306._dfvl(this.ssd, x, y, h, color); + }, + + drawFastHLine: function(x, y, w, color) { + return Adafruit_SSD1306._dfhl(this.ssd, x, y, w, color); + }, + + drawCircle: function(x0, y0, r, color) { + return Adafruit_SSD1306._dc(this.ssd, _pair(x0, y0), r, color); + }, + + drawCircleHelper: function(x0, y0, r, cornername, color) { + return Adafruit_SSD1306._dch(this.ssd, _pair(x0, y0), r, cornername, color); + }, + + fillCircle: function(x0, y0, r, color) { + return Adafruit_SSD1306._fc(this.ssd, _pair(x0, y0), r, color); + }, + + fillCircleHelper: function(x0, y0, r, cornername, delta, color) { + return Adafruit_SSD1306._fch(this.ssd, _pair(x0, y0), r, cornername, delta, color); + }, + + drawTriangle: function(x0, y0, x1, y1, x2, y2, color) { + return Adafruit_SSD1306._dt(this.ssd, _pair(x0, y0), _pair(x1, y1), _pair(x2, y2), color); + }, + + fillTriangle: function(x0, y0, x1, y1, x2, y2, color) { + return Adafruit_SSD1306._ft(this.ssd, _pair(x0, y0), _pair(x1, y1), _pair(x2, y2), color); + }, + + drawRoundRect: function(x0, y0, w, h, radius, color) { + return Adafruit_SSD1306._drr(this.ssd, _pair(x0, y0), w, h, radius, color); + }, + + fillRoundRect: function(x0, y0, w, h, radius, color) { + return Adafruit_SSD1306._frr(this.ssd, _pair(x0, y0), w, h, radius, color); + }, + + drawChar: function(x, y, c, color, bg, size) { + return Adafruit_SSD1306._dch(this.ssd, _pair(x, y), c, color, bg, size); + }, + + setCursor: function(x, y) { + return Adafruit_SSD1306._sc(this.ssd, x, y); + }, + + setTextColor: function(color) { + return Adafruit_SSD1306._stc(this.ssd, color); + }, + + setTextColorBg: function(color, bg) { + return Adafruit_SSD1306._stcb(this.ssd, color, bg); + }, + + setTextSize: function(size) { + return Adafruit_SSD1306._sts(this.ssd, size); + }, + + setTextWrap: function(w) { + return Adafruit_SSD1306._stw(this.ssd, w); + }, + + write: function(str) { + return Adafruit_SSD1306._w(this.ssd, str, str.length); + }, + + height: function() { + return Adafruit_SSD1306._ht(this.ssd); + }, + + width: function() { + return Adafruit_SSD1306._wh(this.ssd); + }, + + getRotation: function() { + return Adafruit_SSD1306._gr(this.ssd); + }, + + // get current cursor position (get rotation safe maximum values, using: width() for x, height() for y) + getCursorX: function() { + return Adafruit_SSD1306._gcx(this.ssd); + }, + + getCursorY: function() { + return Adafruit_SSD1306._gcy(this.ssd); + }, + }, + + // Create a SSD1306 object for I2C. + create_i2c: function(rst, res) { + let obj = Object.create(Adafruit_SSD1306._proto); + // Initialize Adafruit_SSD1306 library for I2C. + // Return value: handle opaque pointer. + // We set the reset pin and + // Resolution: 0 - RES_96_16, 1 - RES_128_32, 2 - RES_128_64. + obj.ssd = Adafruit_SSD1306._ci2c(rst, res); + return obj; + }, + + // Create a SSD1306 object for SPI. + create_spi: function(dc, rst, cs, res) { + let obj = Object.create(Adafruit_SSD1306._proto); + // Initialize Adafruit_SSD1306 library for SPI. + // Return value: handle opaque pointer. + // We set DataCommand, ChipSelect, Reset and + // Resolution: 0 - RES_96_16, 1 - RES_128_32, 2 - RES_128_64. + obj.ssd = Adafruit_SSD1306._cspi(dc, rst, cs, res); + return obj; + }, +} diff --git a/mos.yml b/mos.yml new file mode 100644 index 0000000..644df65 --- /dev/null +++ b/mos.yml @@ -0,0 +1,14 @@ +author: mongoose-os +name: arduino-adafruit-ssd1306 +description: Arduino Adafruit SSD1306 library +type: lib +# arch: PLATFORM +version: 1.0 +skeleton_version: 2017-05-18 +sources: + - src +libs: + - origin: https://github.com/mongoose-os-libs/arduino-compat + - origin: https://github.com/mongoose-os-libs/arduino-wire + - origin: https://github.com/mongoose-os-libs/arduino-spi + - origin: https://github.com/mongoose-os-libs/arduino-adafruit-gfx diff --git a/src/.github/ISSUE_TEMPLATE.md b/src/.github/ISSUE_TEMPLATE.md new file mode 100644 index 0000000..f0e2614 --- /dev/null +++ b/src/.github/ISSUE_TEMPLATE.md @@ -0,0 +1,46 @@ +Thank you for opening an issue on an Adafruit Arduino library repository. To +improve the speed of resolution please review the following guidelines and +common troubleshooting steps below before creating the issue: + +- **Do not use GitHub issues for troubleshooting projects and issues.** Instead use + the forums at http://forums.adafruit.com to ask questions and troubleshoot why + something isn't working as expected. In many cases the problem is a common issue + that you will more quickly receive help from the forum community. GitHub issues + are meant for known defects in the code. If you don't know if there is a defect + in the code then start with troubleshooting on the forum first. + +- **If following a tutorial or guide be sure you didn't miss a step.** Carefully + check all of the steps and commands to run have been followed. Consult the + forum if you're unsure or have questions about steps in a guide/tutorial. + +- **For Arduino projects check these very common issues to ensure they don't apply**: + + - For uploading sketches or communicating with the board make sure you're using + a **USB data cable** and **not** a **USB charge-only cable**. It is sometimes + very hard to tell the difference between a data and charge cable! Try using the + cable with other devices or swapping to another cable to confirm it is not + the problem. + + - **Be sure you are supplying adequate power to the board.** Check the specs of + your board and plug in an external power supply. In many cases just + plugging a board into your computer is not enough to power it and other + peripherals. + + - **Double check all soldering joints and connections.** Flakey connections + cause many mysterious problems. See the [guide to excellent soldering](https://learn.adafruit.com/adafruit-guide-excellent-soldering/tools) for examples of good solder joints. + + - **Ensure you are using an official Arduino or Adafruit board.** We can't + guarantee a clone board will have the same functionality and work as expected + with this code and don't support them. + +If you're sure this issue is a defect in the code and checked the steps above +please fill in the following fields to provide enough troubleshooting information. +You may delete the guideline and text above to just leave the following details: + +- Arduino board: **INSERT ARDUINO BOARD NAME/TYPE HERE** + +- Arduino IDE version (found in Arduino -> About Arduino menu): **INSERT ARDUINO + VERSION HERE** + +- List the steps to reproduce the problem below (if possible attach a sketch or + copy the sketch code in too): **LIST REPRO STEPS BELOW** diff --git a/src/.github/PULL_REQUEST_TEMPLATE.md b/src/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 0000000..7b641eb --- /dev/null +++ b/src/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,26 @@ +Thank you for creating a pull request to contribute to Adafruit's GitHub code! +Before you open the request please review the following guidelines and tips to +help it be more easily integrated: + +- **Describe the scope of your change--i.e. what the change does and what parts + of the code were modified.** This will help us understand any risks of integrating + the code. + +- **Describe any known limitations with your change.** For example if the change + doesn't apply to a supported platform of the library please mention it. + +- **Please run any tests or examples that can exercise your modified code.** We + strive to not break users of the code and running tests/examples helps with this + process. + +Thank you again for contributing! We will try to test and integrate the change +as soon as we can, but be aware we have many GitHub repositories to manage and +can't immediately respond to every request. There is no need to bump or check in +on a pull request (it will clutter the discussion of the request). + +Also don't be worried if the request is closed or not integrated--sometimes the +priorities of Adafruit's GitHub code (education, ease of use) might not match the +priorities of the pull request. Don't fret, the open source community thrives on +forks and GitHub makes it easy to keep your changes in a forked repo. + +After reviewing the guidelines above you can delete this text from the pull request. diff --git a/src/Adafruit_SSD1306.cpp b/src/Adafruit_SSD1306.cpp new file mode 100644 index 0000000..032ca43 --- /dev/null +++ b/src/Adafruit_SSD1306.cpp @@ -0,0 +1,770 @@ +/********************************************************************* +This is a library for our Monochrome OLEDs based on SSD1306 drivers + + Pick one up today in the adafruit shop! + ------> http://www.adafruit.com/category/63_98 + +These displays use SPI to communicate, 4 or 5 pins are required to +interface + +Adafruit invests time and resources providing this open source code, +please support Adafruit and open-source hardware by purchasing +products from Adafruit! + +Written by Limor Fried/Ladyada for Adafruit Industries. +BSD license, check license.txt for more information +All text above, and the splash screen below must be included in any redistribution +*********************************************************************/ + +#ifdef __AVR__ + #include +#elif defined(ESP8266) || defined(ESP32) + #include +#else + #define pgm_read_byte(addr) (*(const unsigned char *)(addr)) +#endif + +#if !defined(__ARM_ARCH) && !defined(ENERGIA) && !defined(ESP8266) && !defined(ESP32) && !defined(__arc__) + #include +#endif + +#include + +#include +#include +#include "Adafruit_GFX.h" +#include "Adafruit_SSD1306.h" + +static const uint8_t logo_buffer[] = { +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, +0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x80, 0x80, 0xC0, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xF8, 0xE0, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x80, +0x80, 0x80, 0x00, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 0xFF, +// For 128x32 and 128x64 +0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, +0x80, 0xFF, 0xFF, 0x80, 0x80, 0x00, 0x80, 0x80, 0x00, 0x80, 0x80, 0x80, 0x80, 0x00, 0x80, 0x80, +0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x8C, 0x8E, 0x84, 0x00, 0x00, 0x80, 0xF8, +0xF8, 0xF8, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xE0, 0xE0, 0xC0, 0x80, +0x00, 0xE0, 0xFC, 0xFE, 0xFF, 0xFF, 0xFF, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFE, 0xFF, 0xC7, 0x01, 0x01, +0x01, 0x01, 0x83, 0xFF, 0xFF, 0x00, 0x00, 0x7C, 0xFE, 0xC7, 0x01, 0x01, 0x01, 0x01, 0x83, 0xFF, +0xFF, 0xFF, 0x00, 0x38, 0xFE, 0xC7, 0x83, 0x01, 0x01, 0x01, 0x83, 0xC7, 0xFF, 0xFF, 0x00, 0x00, +0x01, 0xFF, 0xFF, 0x01, 0x01, 0x00, 0xFF, 0xFF, 0x07, 0x01, 0x01, 0x01, 0x00, 0x00, 0x7F, 0xFF, +0x80, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x7F, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x01, 0xFF, +0xFF, 0xFF, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x03, 0x0F, 0x3F, 0x7F, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xE7, 0xC7, 0xC7, 0x8F, +0x8F, 0x9F, 0xBF, 0xFF, 0xFF, 0xC3, 0xC0, 0xF0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0xFC, 0xFC, +0xFC, 0xFC, 0xFC, 0xFC, 0xFC, 0xF8, 0xF8, 0xF0, 0xF0, 0xE0, 0xC0, 0x00, 0x01, 0x03, 0x03, 0x03, +0x03, 0x03, 0x01, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x03, 0x03, 0x01, 0x01, +0x03, 0x01, 0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x03, 0x03, 0x01, 0x01, 0x03, 0x03, 0x00, 0x00, +0x00, 0x03, 0x03, 0x00, 0x00, 0x00, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, +0x03, 0x03, 0x03, 0x03, 0x03, 0x01, 0x00, 0x00, 0x00, 0x01, 0x03, 0x01, 0x00, 0x00, 0x00, 0x03, +0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +// For 128x64 +0x00, 0x00, 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF9, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x3F, 0x1F, 0x0F, +0x87, 0xC7, 0xF7, 0xFF, 0xFF, 0x1F, 0x1F, 0x3D, 0xFC, 0xF8, 0xF8, 0xF8, 0xF8, 0x7C, 0x7D, 0xFF, +0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x3F, 0x0F, 0x07, 0x00, 0x30, 0x30, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0xFE, 0xFE, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0xC0, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x30, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0xC0, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x7F, 0x3F, 0x1F, +0x0F, 0x07, 0x1F, 0x7F, 0xFF, 0xFF, 0xF8, 0xF8, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xF8, 0xE0, +0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFE, 0xFE, 0x00, 0x00, +0x00, 0xFC, 0xFE, 0xFC, 0x0C, 0x06, 0x06, 0x0E, 0xFC, 0xF8, 0x00, 0x00, 0xF0, 0xF8, 0x1C, 0x0E, +0x06, 0x06, 0x06, 0x0C, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0xFE, 0xFE, 0x00, 0x00, 0x00, 0x00, 0xFC, +0xFE, 0xFC, 0x00, 0x18, 0x3C, 0x7E, 0x66, 0xE6, 0xCE, 0x84, 0x00, 0x00, 0x06, 0xFF, 0xFF, 0x06, +0x06, 0xFC, 0xFE, 0xFC, 0x0C, 0x06, 0x06, 0x06, 0x00, 0x00, 0xFE, 0xFE, 0x00, 0x00, 0xC0, 0xF8, +0xFC, 0x4E, 0x46, 0x46, 0x46, 0x4E, 0x7C, 0x78, 0x40, 0x18, 0x3C, 0x76, 0xE6, 0xCE, 0xCC, 0x80, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x01, 0x07, 0x0F, 0x1F, 0x1F, 0x3F, 0x3F, 0x3F, 0x3F, 0x1F, 0x0F, 0x03, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x00, 0x00, +0x00, 0x0F, 0x0F, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x00, 0x00, 0x03, 0x07, 0x0E, 0x0C, +0x18, 0x18, 0x0C, 0x06, 0x0F, 0x0F, 0x0F, 0x00, 0x00, 0x01, 0x0F, 0x0E, 0x0C, 0x18, 0x0C, 0x0F, +0x07, 0x01, 0x00, 0x04, 0x0E, 0x0C, 0x18, 0x0C, 0x0F, 0x07, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x00, +0x00, 0x0F, 0x0F, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x0F, 0x00, 0x00, 0x00, 0x07, +0x07, 0x0C, 0x0C, 0x18, 0x1C, 0x0C, 0x06, 0x06, 0x00, 0x04, 0x0E, 0x0C, 0x18, 0x0C, 0x0F, 0x07, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 +}; + +#define ssd1306_swap(a, b) { int16_t t = a; a = b; b = t; } + +// static +int Adafruit_SSD1306::getW(Adafruit_SSD1306::Resolution res) { + switch (res) { + case RES_128_32: + case RES_128_64: + return 128; + case RES_96_16: + return 96; + } + return 0; +} + +// static +int Adafruit_SSD1306::getH(Adafruit_SSD1306::Resolution res) { + switch (res) { + case RES_128_32: + return 32; + case RES_128_64: + return 64; + case RES_96_16: + return 16; + } + return 0; +} + +// the most basic function, set a single pixel +void Adafruit_SSD1306::drawPixel(int16_t x, int16_t y, uint16_t color) { + if ((x < 0) || (x >= width()) || (y < 0) || (y >= height())) + return; + + // check rotation, move pixel around if necessary + switch (getRotation()) { + case 1: + ssd1306_swap(x, y); + x = WIDTH - x - 1; + break; + case 2: + x = WIDTH - x - 1; + y = HEIGHT - y - 1; + break; + case 3: + ssd1306_swap(x, y); + y = HEIGHT - y - 1; + break; + } + + // x is which column + switch (color) + { + case WHITE: buffer[x+ (y/8)*WIDTH] |= (1 << (y&7)); break; + case BLACK: buffer[x+ (y/8)*WIDTH] &= ~(1 << (y&7)); break; + case INVERSE: buffer[x+ (y/8)*WIDTH] ^= (1 << (y&7)); break; + } + +} + +Adafruit_SSD1306::Adafruit_SSD1306(int8_t SID, int8_t SCLK, int8_t DC, int8_t RST, int8_t CS, + Adafruit_SSD1306::Resolution res) + : Adafruit_GFX(getW(res), getH(res)), res(res) { + cs = CS; + rst = RST; + dc = DC; + sclk = SCLK; + sid = SID; + hwSPI = false; +} + +// constructor for hardware SPI - we indicate DataCommand, ChipSelect, Reset +Adafruit_SSD1306::Adafruit_SSD1306(int8_t DC, int8_t RST, int8_t CS, + Adafruit_SSD1306::Resolution res) + : Adafruit_GFX(getW(res), getH(res)), res(res) { + dc = DC; + rst = RST; + cs = CS; + hwSPI = true; +} + +// initializer for I2C - we only indicate the reset pin! +Adafruit_SSD1306::Adafruit_SSD1306(int8_t reset, + Adafruit_SSD1306::Resolution res) + : Adafruit_GFX(getW(res), getH(res)), res(res) { + sclk = dc = cs = sid = -1; + rst = reset; +} + + +void Adafruit_SSD1306::begin(uint8_t vccstate, uint8_t i2caddr, bool reset) { + _vccstate = vccstate; + _i2caddr = i2caddr; + int buf_size = WIDTH * HEIGHT / 8; + memcpy(buffer, logo_buffer, buf_size); + + // set pin directions + if (sid != -1){ + pinMode(dc, OUTPUT); + pinMode(cs, OUTPUT); +#ifdef HAVE_PORTREG + csport = portOutputRegister(digitalPinToPort(cs)); + cspinmask = digitalPinToBitMask(cs); + dcport = portOutputRegister(digitalPinToPort(dc)); + dcpinmask = digitalPinToBitMask(dc); +#endif + if (!hwSPI){ + // set pins for software-SPI + pinMode(sid, OUTPUT); + pinMode(sclk, OUTPUT); +#ifdef HAVE_PORTREG + clkport = portOutputRegister(digitalPinToPort(sclk)); + clkpinmask = digitalPinToBitMask(sclk); + mosiport = portOutputRegister(digitalPinToPort(sid)); + mosipinmask = digitalPinToBitMask(sid); +#endif + } + if (hwSPI){ + SPI.begin(); +#ifdef SPI_HAS_TRANSACTION + SPI.beginTransaction(SPISettings(8000000, MSBFIRST, SPI_MODE0)); +#else + SPI.setClockDivider (4); +#endif + } + } + else + { + // I2C Init + Wire.begin(); +#ifdef __SAM3X8E__ + // Force 400 KHz I2C, rawr! (Uses pins 20, 21 for SDA, SCL) + TWI1->TWI_CWGR = 0; + TWI1->TWI_CWGR = ((VARIANT_MCK / (2 * 400000)) - 4) * 0x101; +#endif + } + if ((reset) && (rst >= 0)) { + // Setup reset pin direction (used by both SPI and I2C) + pinMode(rst, OUTPUT); + digitalWrite(rst, HIGH); + // VDD (3.3V) goes high at start, lets just chill for a ms + delay(1); + // bring reset low + digitalWrite(rst, LOW); + // wait 10ms + delay(10); + // bring out of reset + digitalWrite(rst, HIGH); + // turn on VCC (9V?) + } + + // Init sequence + ssd1306_command(SSD1306_DISPLAYOFF); // 0xAE + ssd1306_command(SSD1306_SETDISPLAYCLOCKDIV); // 0xD5 + ssd1306_command(0x80); // the suggested ratio 0x80 + + ssd1306_command(SSD1306_SETMULTIPLEX); // 0xA8 + ssd1306_command(HEIGHT - 1); + + ssd1306_command(SSD1306_SETDISPLAYOFFSET); // 0xD3 + ssd1306_command(0x0); // no offset + ssd1306_command(SSD1306_SETSTARTLINE | 0x0); // line #0 + ssd1306_command(SSD1306_CHARGEPUMP); // 0x8D + if (vccstate == SSD1306_EXTERNALVCC) + { ssd1306_command(0x10); } + else + { ssd1306_command(0x14); } + ssd1306_command(SSD1306_MEMORYMODE); // 0x20 + ssd1306_command(0x00); // 0x0 act like ks0108 + ssd1306_command(SSD1306_SEGREMAP | 0x1); + ssd1306_command(SSD1306_COMSCANDEC); + + switch (res) { + case RES_128_32: + ssd1306_command(SSD1306_SETCOMPINS); // 0xDA + ssd1306_command(0x02); + ssd1306_command(SSD1306_SETCONTRAST); // 0x81 + ssd1306_command(0x8F); + break; + + case RES_128_64: + ssd1306_command(SSD1306_SETCOMPINS); // 0xDA + ssd1306_command(0x12); + ssd1306_command(SSD1306_SETCONTRAST); // 0x81 + if (vccstate == SSD1306_EXTERNALVCC) + { ssd1306_command(0x9F); } + else + { ssd1306_command(0xCF); } + break; + + case RES_96_16: + ssd1306_command(SSD1306_SETCOMPINS); // 0xDA + ssd1306_command(0x2); //ada x12 + ssd1306_command(SSD1306_SETCONTRAST); // 0x81 + if (vccstate == SSD1306_EXTERNALVCC) + { ssd1306_command(0x10); } + else + { ssd1306_command(0xAF); } + break; + } + + ssd1306_command(SSD1306_SETPRECHARGE); // 0xd9 + if (vccstate == SSD1306_EXTERNALVCC) + { ssd1306_command(0x22); } + else + { ssd1306_command(0xF1); } + ssd1306_command(SSD1306_SETVCOMDETECT); // 0xDB + ssd1306_command(0x40); + ssd1306_command(SSD1306_DISPLAYALLON_RESUME); // 0xA4 + ssd1306_command(SSD1306_NORMALDISPLAY); // 0xA6 + + ssd1306_command(SSD1306_DEACTIVATE_SCROLL); + + ssd1306_command(SSD1306_DISPLAYON);//--turn on oled panel +} + + +void Adafruit_SSD1306::invertDisplay(uint8_t i) { + if (i) { + ssd1306_command(SSD1306_INVERTDISPLAY); + } else { + ssd1306_command(SSD1306_NORMALDISPLAY); + } +} + +void Adafruit_SSD1306::ssd1306_command(uint8_t c) { + if (sid != -1) + { + // SPI +#ifdef HAVE_PORTREG + *csport |= cspinmask; + *dcport &= ~dcpinmask; + *csport &= ~cspinmask; +#else + digitalWrite(cs, HIGH); + digitalWrite(dc, LOW); + digitalWrite(cs, LOW); +#endif + fastSPIwrite(c); +#ifdef HAVE_PORTREG + *csport |= cspinmask; +#else + digitalWrite(cs, HIGH); +#endif + } + else + { + // I2C + uint8_t control = 0x00; // Co = 0, D/C = 0 + Wire.beginTransmission(_i2caddr); + Wire.write(control); + Wire.write(c); + Wire.endTransmission(); + } +} + +// startscrollright +// Activate a right handed scroll for rows start through stop +// Hint, the display is 16 rows tall. To scroll the whole display, run: +// display.scrollright(0x00, 0x0F) +void Adafruit_SSD1306::startscrollright(uint8_t start, uint8_t stop){ + ssd1306_command(SSD1306_RIGHT_HORIZONTAL_SCROLL); + ssd1306_command(0X00); + ssd1306_command(start); + ssd1306_command(0X00); + ssd1306_command(stop); + ssd1306_command(0X00); + ssd1306_command(0XFF); + ssd1306_command(SSD1306_ACTIVATE_SCROLL); +} + +// startscrollleft +// Activate a right handed scroll for rows start through stop +// Hint, the display is 16 rows tall. To scroll the whole display, run: +// display.scrollright(0x00, 0x0F) +void Adafruit_SSD1306::startscrollleft(uint8_t start, uint8_t stop){ + ssd1306_command(SSD1306_LEFT_HORIZONTAL_SCROLL); + ssd1306_command(0X00); + ssd1306_command(start); + ssd1306_command(0X00); + ssd1306_command(stop); + ssd1306_command(0X00); + ssd1306_command(0XFF); + ssd1306_command(SSD1306_ACTIVATE_SCROLL); +} + +// startscrolldiagright +// Activate a diagonal scroll for rows start through stop +// Hint, the display is 16 rows tall. To scroll the whole display, run: +// display.scrollright(0x00, 0x0F) +void Adafruit_SSD1306::startscrolldiagright(uint8_t start, uint8_t stop){ + ssd1306_command(SSD1306_SET_VERTICAL_SCROLL_AREA); + ssd1306_command(0X00); + ssd1306_command(HEIGHT); + ssd1306_command(SSD1306_VERTICAL_AND_RIGHT_HORIZONTAL_SCROLL); + ssd1306_command(0X00); + ssd1306_command(start); + ssd1306_command(0X00); + ssd1306_command(stop); + ssd1306_command(0X01); + ssd1306_command(SSD1306_ACTIVATE_SCROLL); +} + +// startscrolldiagleft +// Activate a diagonal scroll for rows start through stop +// Hint, the display is 16 rows tall. To scroll the whole display, run: +// display.scrollright(0x00, 0x0F) +void Adafruit_SSD1306::startscrolldiagleft(uint8_t start, uint8_t stop){ + ssd1306_command(SSD1306_SET_VERTICAL_SCROLL_AREA); + ssd1306_command(0X00); + ssd1306_command(HEIGHT); + ssd1306_command(SSD1306_VERTICAL_AND_LEFT_HORIZONTAL_SCROLL); + ssd1306_command(0X00); + ssd1306_command(start); + ssd1306_command(0X00); + ssd1306_command(stop); + ssd1306_command(0X01); + ssd1306_command(SSD1306_ACTIVATE_SCROLL); +} + +void Adafruit_SSD1306::stopscroll(void){ + ssd1306_command(SSD1306_DEACTIVATE_SCROLL); +} + +// Dim the display +// dim = true: display is dimmed +// dim = false: display is normal +void Adafruit_SSD1306::dim(boolean dim) { + uint8_t contrast; + + if (dim) { + contrast = 0; // Dimmed display + } else { + if (_vccstate == SSD1306_EXTERNALVCC) { + contrast = 0x9F; + } else { + contrast = 0xCF; + } + } + // the range of contrast to too small to be really useful + // it is useful to dim the display + ssd1306_command(SSD1306_SETCONTRAST); + ssd1306_command(contrast); +} + +void Adafruit_SSD1306::display(void) { + ssd1306_command(SSD1306_COLUMNADDR); + ssd1306_command(0); // Column start address (0 = reset) + ssd1306_command(WIDTH-1); // Column end address (127 = reset) + + ssd1306_command(SSD1306_PAGEADDR); + ssd1306_command(0); // Page start address (0 = reset) + switch (HEIGHT) { + case 64: + ssd1306_command(7); // Page end address + break; + case 32: + ssd1306_command(3); // Page end address + break; + case 16: + ssd1306_command(1); // Page end address + break; + } + + if (sid != -1) + { + // SPI +#ifdef HAVE_PORTREG + *csport |= cspinmask; + *dcport |= dcpinmask; + *csport &= ~cspinmask; +#else + digitalWrite(cs, HIGH); + digitalWrite(dc, HIGH); + digitalWrite(cs, LOW); +#endif + + fastSPIwrite(buffer, WIDTH*HEIGHT/8); +#ifdef HAVE_PORTREG + *csport |= cspinmask; +#else + digitalWrite(cs, HIGH); +#endif + } + else + { + // save I2C bitrate +#ifdef TWBR + uint8_t twbrbackup = TWBR; + TWBR = 12; // upgrade to 400KHz! +#endif + + //Serial.println(TWBR, DEC); + //Serial.println(TWSR & 0x3, DEC); + + // I2C + for (uint16_t i=0; i<(WIDTH*HEIGHT/8); i++) { + // send a bunch of data in one xmission + Wire.beginTransmission(_i2caddr); + WIRE_WRITE(0x40); + for (uint8_t x=0; x<16; x++) { + WIRE_WRITE(buffer[i]); + i++; + } + i--; + Wire.endTransmission(); + } +#ifdef TWBR + TWBR = twbrbackup; +#endif + } +} + +// clear everything +void Adafruit_SSD1306::clearDisplay(void) { + memset(buffer, 0, (WIDTH*HEIGHT/8)); +} + + +inline void Adafruit_SSD1306::fastSPIwrite(uint8_t d) { + + if(hwSPI) { + (void)SPI.transfer(d); + } else { + for(uint8_t bit = 0x80; bit; bit >>= 1) { +#ifdef HAVE_PORTREG + *clkport &= ~clkpinmask; + if(d & bit) *mosiport |= mosipinmask; + else *mosiport &= ~mosipinmask; + *clkport |= clkpinmask; +#else + digitalWrite(sclk, LOW); + if(d & bit) digitalWrite(sid, HIGH); + else digitalWrite(sid, LOW); + digitalWrite(sclk, HIGH); +#endif + } + } +} + +void Adafruit_SSD1306::fastSPIwrite(const uint8_t *bytes, size_t len) { + if (hwSPI) { + SPI.writeBytes(bytes, len); + } else { + while (len-- > 0) { + fastSPIwrite(*bytes++); + } + } +} + +void Adafruit_SSD1306::drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color) { + boolean bSwap = false; + switch(rotation) { + case 0: + // 0 degree rotation, do nothing + break; + case 1: + // 90 degree rotation, swap x & y for rotation, then invert x + bSwap = true; + ssd1306_swap(x, y); + x = WIDTH - x - 1; + break; + case 2: + // 180 degree rotation, invert x and y - then shift y around for height. + x = WIDTH - x - 1; + y = HEIGHT - y - 1; + x -= (w-1); + break; + case 3: + // 270 degree rotation, swap x & y for rotation, then invert y and adjust y for w (not to become h) + bSwap = true; + ssd1306_swap(x, y); + y = HEIGHT - y - 1; + y -= (w-1); + break; + } + + if(bSwap) { + drawFastVLineInternal(x, y, w, color); + } else { + drawFastHLineInternal(x, y, w, color); + } +} + +void Adafruit_SSD1306::drawFastHLineInternal(int16_t x, int16_t y, int16_t w, uint16_t color) { + // Do bounds/limit checks + if(y < 0 || y >= HEIGHT) { return; } + + // make sure we don't try to draw below 0 + if(x < 0) { + w += x; + x = 0; + } + + // make sure we don't go off the edge of the display + if( (x + w) > WIDTH) { + w = (WIDTH - x); + } + + // if our width is now negative, punt + if(w <= 0) { return; } + + // set up the pointer for movement through the buffer + register uint8_t *pBuf = buffer; + // adjust the buffer pointer for the current row + pBuf += ((y/8) * WIDTH); + // and offset x columns in + pBuf += x; + + register uint8_t mask = 1 << (y&7); + + switch (color) + { + case WHITE: while(w--) { *pBuf++ |= mask; }; break; + case BLACK: mask = ~mask; while(w--) { *pBuf++ &= mask; }; break; + case INVERSE: while(w--) { *pBuf++ ^= mask; }; break; + } +} + +void Adafruit_SSD1306::drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color) { + bool bSwap = false; + switch(rotation) { + case 0: + break; + case 1: + // 90 degree rotation, swap x & y for rotation, then invert x and adjust x for h (now to become w) + bSwap = true; + ssd1306_swap(x, y); + x = WIDTH - x - 1; + x -= (h-1); + break; + case 2: + // 180 degree rotation, invert x and y - then shift y around for height. + x = WIDTH - x - 1; + y = HEIGHT - y - 1; + y -= (h-1); + break; + case 3: + // 270 degree rotation, swap x & y for rotation, then invert y + bSwap = true; + ssd1306_swap(x, y); + y = HEIGHT - y - 1; + break; + } + + if(bSwap) { + drawFastHLineInternal(x, y, h, color); + } else { + drawFastVLineInternal(x, y, h, color); + } +} + + +void Adafruit_SSD1306::drawFastVLineInternal(int16_t x, int16_t __y, int16_t __h, uint16_t color) { + + // do nothing if we're off the left or right side of the screen + if(x < 0 || x >= WIDTH) { return; } + + // make sure we don't try to draw below 0 + if(__y < 0) { + // __y is negative, this will subtract enough from __h to account for __y being 0 + __h += __y; + __y = 0; + + } + + // make sure we don't go past the height of the display + if( (__y + __h) > HEIGHT) { + __h = (HEIGHT - __y); + } + + // if our height is now negative, punt + if(__h <= 0) { + return; + } + + // this display doesn't need ints for coordinates, use local byte registers for faster juggling + register uint8_t y = __y; + register uint8_t h = __h; + + + // set up the pointer for fast movement through the buffer + register uint8_t *pBuf = buffer; + // adjust the buffer pointer for the current row + pBuf += ((y/8) * WIDTH); + // and offset x columns in + pBuf += x; + + // do the first partial byte, if necessary - this requires some masking + register uint8_t mod = (y&7); + if(mod) { + // mask off the high n bits we want to set + mod = 8-mod; + + // note - lookup table results in a nearly 10% performance improvement in fill* functions + // register uint8_t mask = ~(0xFF >> (mod)); + static uint8_t premask[8] = {0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE }; + register uint8_t mask = premask[mod]; + + // adjust the mask if we're not going to reach the end of this byte + if( h < mod) { + mask &= (0XFF >> (mod-h)); + } + + switch (color) + { + case WHITE: *pBuf |= mask; break; + case BLACK: *pBuf &= ~mask; break; + case INVERSE: *pBuf ^= mask; break; + } + + // fast exit if we're done here! + if(h= 8) { + if (color == INVERSE) { // separate copy of the code so we don't impact performance of the black/white write version with an extra comparison per loop + do { + *pBuf=~(*pBuf); + + // adjust the buffer forward 8 rows worth of data + pBuf += WIDTH; + + // adjust h & y (there's got to be a faster way for me to do this, but this should still help a fair bit for now) + h -= 8; + } while(h >= 8); + } + else { + // store a local value to work with + register uint8_t val = (color == WHITE) ? 255 : 0; + + do { + // write our value in + *pBuf = val; + + // adjust the buffer forward 8 rows worth of data + pBuf += WIDTH; + + // adjust h & y (there's got to be a faster way for me to do this, but this should still help a fair bit for now) + h -= 8; + } while(h >= 8); + } + } + + // now do the final partial byte, if necessary + if(h) { + mod = h & 7; + // this time we want to mask the low bits of the byte, vs the high bits we did above + // register uint8_t mask = (1 << mod) - 1; + // note - lookup table results in a nearly 10% performance improvement in fill* functions + static uint8_t postmask[8] = {0x00, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F }; + register uint8_t mask = postmask[mod]; + switch (color) + { + case WHITE: *pBuf |= mask; break; + case BLACK: *pBuf &= ~mask; break; + case INVERSE: *pBuf ^= mask; break; + } + } +} diff --git a/src/Adafruit_SSD1306.h b/src/Adafruit_SSD1306.h new file mode 100644 index 0000000..3840488 --- /dev/null +++ b/src/Adafruit_SSD1306.h @@ -0,0 +1,189 @@ +/********************************************************************* +This is a library for our Monochrome OLEDs based on SSD1306 drivers + + Pick one up today in the adafruit shop! + ------> http://www.adafruit.com/category/63_98 + +These displays use SPI to communicate, 4 or 5 pins are required to +interface + +Adafruit invests time and resources providing this open source code, +please support Adafruit and open-source hardware by purchasing +products from Adafruit! + +Written by Limor Fried/Ladyada for Adafruit Industries. +BSD license, check license.txt for more information +All text above, and the splash screen must be included in any redistribution +*********************************************************************/ +#ifndef _Adafruit_SSD1306_H_ +#define _Adafruit_SSD1306_H_ + +#if ARDUINO >= 100 + #include "Arduino.h" + #define WIRE_WRITE Wire.write +#else + #include "WProgram.h" + #define WIRE_WRITE Wire.send +#endif + +#if defined(__SAM3X8E__) + typedef volatile RwReg PortReg; + typedef uint32_t PortMask; + #define HAVE_PORTREG +#elif defined(ARDUINO_ARCH_SAMD) +// not supported +#elif defined(ESP8266) || defined(ESP32) || defined(ARDUINO_STM32_FEATHER) || defined(__arc__) + typedef volatile uint32_t PortReg; + typedef uint32_t PortMask; +#elif defined(__AVR__) + typedef volatile uint8_t PortReg; + typedef uint8_t PortMask; + #define HAVE_PORTREG +#else + // chances are its 32 bit so assume that + typedef volatile uint32_t PortReg; + typedef uint32_t PortMask; +#endif + +#include +#include + +#define BLACK 0 +#define WHITE 1 +#define INVERSE 2 + +#define SSD1306_I2C_ADDRESS 0x3C // 011110+SA0+RW - 0x3C or 0x3D +// Address for 128x32 is 0x3C +// Address for 128x64 is 0x3D (default) or 0x3C (if SA0 is grounded) + +/*========================================================================= + SSD1306 Displays + ----------------------------------------------------------------------- + The driver is used in multiple displays (128x64, 128x32, etc.). + Select the appropriate display below to create an appropriately + sized framebuffer, etc. + + SSD1306_128_64 128x64 pixel display + + SSD1306_128_32 128x32 pixel display + + SSD1306_96_16 + + -----------------------------------------------------------------------*/ +// #define SSD1306_128_64 + #define SSD1306_128_32 +// #define SSD1306_96_16 +/*=========================================================================*/ + +#if defined SSD1306_128_64 && defined SSD1306_128_32 + #error "Only one SSD1306 display can be specified at once in SSD1306.h" +#endif +#if !defined SSD1306_128_64 && !defined SSD1306_128_32 && !defined SSD1306_96_16 + #error "At least one SSD1306 display must be specified in SSD1306.h" +#endif + +#if defined SSD1306_128_64 + #define SSD1306_DEFAULT_RES Adafruit_SSD1306::RES_128_64 +#elif defined SSD1306_128_32 + #define SSD1306_DEFAULT_RES Adafruit_SSD1306::RES_128_32 +#elif defined SSD1306_96_16 + #define SSD1306_DEFAULT_RES Adafruit_SSD1306::RES_96_16 +#endif + +#define SSD1306_SETCONTRAST 0x81 +#define SSD1306_DISPLAYALLON_RESUME 0xA4 +#define SSD1306_DISPLAYALLON 0xA5 +#define SSD1306_NORMALDISPLAY 0xA6 +#define SSD1306_INVERTDISPLAY 0xA7 +#define SSD1306_DISPLAYOFF 0xAE +#define SSD1306_DISPLAYON 0xAF + +#define SSD1306_SETDISPLAYOFFSET 0xD3 +#define SSD1306_SETCOMPINS 0xDA + +#define SSD1306_SETVCOMDETECT 0xDB + +#define SSD1306_SETDISPLAYCLOCKDIV 0xD5 +#define SSD1306_SETPRECHARGE 0xD9 + +#define SSD1306_SETMULTIPLEX 0xA8 + +#define SSD1306_SETLOWCOLUMN 0x00 +#define SSD1306_SETHIGHCOLUMN 0x10 + +#define SSD1306_SETSTARTLINE 0x40 + +#define SSD1306_MEMORYMODE 0x20 +#define SSD1306_COLUMNADDR 0x21 +#define SSD1306_PAGEADDR 0x22 + +#define SSD1306_COMSCANINC 0xC0 +#define SSD1306_COMSCANDEC 0xC8 + +#define SSD1306_SEGREMAP 0xA0 + +#define SSD1306_CHARGEPUMP 0x8D + +#define SSD1306_EXTERNALVCC 0x1 +#define SSD1306_SWITCHCAPVCC 0x2 + +// Scrolling #defines +#define SSD1306_ACTIVATE_SCROLL 0x2F +#define SSD1306_DEACTIVATE_SCROLL 0x2E +#define SSD1306_SET_VERTICAL_SCROLL_AREA 0xA3 +#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 + +class Adafruit_SSD1306 : public Adafruit_GFX { + public: + enum Resolution { RES_96_16, RES_128_32, RES_128_64 }; + + Adafruit_SSD1306(int8_t SID, int8_t SCLK, int8_t DC, int8_t RST, int8_t CS, Resolution res = SSD1306_DEFAULT_RES); + Adafruit_SSD1306(int8_t DC, int8_t RST, int8_t CS, Resolution res = SSD1306_DEFAULT_RES); + Adafruit_SSD1306(int8_t RST = -1, Resolution res = SSD1306_DEFAULT_RES); + + void begin(uint8_t switchvcc = SSD1306_SWITCHCAPVCC, uint8_t i2caddr = SSD1306_I2C_ADDRESS, bool reset = true); + void ssd1306_command(uint8_t c); + + void clearDisplay(void); + void invertDisplay(uint8_t i); + void display(); + + void startscrollright(uint8_t start, uint8_t stop); + void startscrollleft(uint8_t start, uint8_t stop); + + void startscrolldiagright(uint8_t start, uint8_t stop); + void startscrolldiagleft(uint8_t start, uint8_t stop); + void stopscroll(void); + + void dim(boolean dim); + + void drawPixel(int16_t x, int16_t y, uint16_t color); + + virtual void drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color); + virtual void drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color); + + private: + void fastSPIwrite(uint8_t c); + void fastSPIwrite(const uint8_t *bytes, size_t len); + static int getW(Resolution res); + static int getH(Resolution res); + + Resolution res; + int8_t _i2caddr, _vccstate, sid, sclk, dc, rst, cs; + uint8_t buffer[128 * 64 / 8]; + + boolean hwSPI; +#ifdef HAVE_PORTREG + PortReg *mosiport, *clkport, *csport, *dcport; + PortMask mosipinmask, clkpinmask, cspinmask, dcpinmask; +#endif + + inline void drawFastVLineInternal(int16_t x, int16_t y, int16_t h, uint16_t color) __attribute__((always_inline)); + inline void drawFastHLineInternal(int16_t x, int16_t y, int16_t w, uint16_t color) __attribute__((always_inline)); + +}; + +#endif /* _Adafruit_SSD1306_H_ */ diff --git a/src/README.md b/src/README.md new file mode 100644 index 0000000..feb8ded --- /dev/null +++ b/src/README.md @@ -0,0 +1,36 @@ +# Cesanta note + +Imported from https://github.com/adafruit/Adafruit_SSD1306/commit/ddfec78fa15f0ff8dfc8a76524077ba6bb5fc6f3 + +# Adafruit_SSD1306 + + +## Compatibility + +MCU | Tested Works | Doesn't Work | Not Tested | Notes +------------------ | :----------: | :----------: | :---------: | ----- +Atmega328 @ 16MHz | X | | | +Atmega328 @ 12MHz | X | | | +Atmega32u4 @ 16MHz | X | | | +Atmega32u4 @ 8MHz | X | | | +ESP8266 | X | | | change OLED_RESET to different pin if using default I2C pins D4/D5. +Atmega2560 @ 16MHz | X | | | +ATSAM3X8E | X | | | +ATSAM21D | X | | | +ATtiny85 @ 16MHz | | X | | +ATtiny85 @ 8MHz | | X | | +Intel Curie @ 32MHz | | | X | +STM32F2 | | | X | + + * ATmega328 @ 16MHz : Arduino UNO, Adafruit Pro Trinket 5V, Adafruit Metro 328, Adafruit Metro Mini + * ATmega328 @ 12MHz : Adafruit Pro Trinket 3V + * ATmega32u4 @ 16MHz : Arduino Leonardo, Arduino Micro, Arduino Yun, Teensy 2.0 + * ATmega32u4 @ 8MHz : Adafruit Flora, Bluefruit Micro + * ESP8266 : Adafruit Huzzah + * ATmega2560 @ 16MHz : Arduino Mega + * ATSAM3X8E : Arduino Due + * ATSAM21D : Arduino Zero, M0 Pro + * ATtiny85 @ 16MHz : Adafruit Trinket 5V + * ATtiny85 @ 8MHz : Adafruit Gemma, Arduino Gemma, Adafruit Trinket 3V + + diff --git a/src/README.txt b/src/README.txt new file mode 100644 index 0000000..420cc15 --- /dev/null +++ b/src/README.txt @@ -0,0 +1,24 @@ +This is a library for our Monochrome OLEDs based on SSD1306 drivers + + Pick one up today in the adafruit shop! + ------> http://www.adafruit.com/category/63_98 + +These displays use SPI to communicate, 4 or 5 pins are required to +interface + +Adafruit invests time and resources providing this open source code, +please support Adafruit and open-source hardware by purchasing +products from Adafruit! + +Written by Limor Fried/Ladyada for Adafruit Industries. +Scrolling code contributed by Michael Gregg +BSD license, check license.txt for more information +All text above must be included in any redistribution + +To download. click the DOWNLOADS button in the top right corner, rename the uncompressed folder Adafruit_SSD1306. Check that the Adafruit_SSD1306 folder contains Adafruit_SSD1306.cpp and Adafruit_SSD1306.h + +Place the Adafruit_SSD1306 library folder your /libraries/ folder. You may need to create the libraries subfolder if its your first library. Restart the IDE. + +You will also have to download the Adafruit GFX Graphics core which does all the circles, text, rectangles, etc. You can get it from +https://github.com/adafruit/Adafruit-GFX-Library +and download/install that library as well \ No newline at end of file diff --git a/src/examples/ssd1306_128x32_i2c/ssd1306_128x32_i2c.ino b/src/examples/ssd1306_128x32_i2c/ssd1306_128x32_i2c.ino new file mode 100644 index 0000000..b3b8bfa --- /dev/null +++ b/src/examples/ssd1306_128x32_i2c/ssd1306_128x32_i2c.ino @@ -0,0 +1,375 @@ +/********************************************************************* +This is an example for our Monochrome OLEDs based on SSD1306 drivers + + Pick one up today in the adafruit shop! + ------> http://www.adafruit.com/category/63_98 + +This example is for a 128x32 size display using I2C to communicate +3 pins are required to interface (2 I2C and one reset) + +Adafruit invests time and resources providing this open source code, +please support Adafruit and open-source hardware by purchasing +products from Adafruit! + +Written by Limor Fried/Ladyada for Adafruit Industries. +BSD license, check license.txt for more information +All text above, and the splash screen must be included in any redistribution +*********************************************************************/ + +#include +#include +#include +#include + +#define OLED_RESET 4 +Adafruit_SSD1306 display(OLED_RESET); + +#define NUMFLAKES 10 +#define XPOS 0 +#define YPOS 1 +#define DELTAY 2 + + +#define LOGO16_GLCD_HEIGHT 16 +#define LOGO16_GLCD_WIDTH 16 +static const unsigned char PROGMEM logo16_glcd_bmp[] = +{ B00000000, B11000000, + B00000001, B11000000, + B00000001, B11000000, + B00000011, B11100000, + B11110011, B11100000, + B11111110, B11111000, + B01111110, B11111111, + B00110011, B10011111, + B00011111, B11111100, + B00001101, B01110000, + B00011011, B10100000, + B00111111, B11100000, + B00111111, B11110000, + B01111100, B11110000, + B01110000, B01110000, + B00000000, B00110000 }; + +#if (SSD1306_LCDHEIGHT != 32) +#error("Height incorrect, please fix Adafruit_SSD1306.h!"); +#endif + +void setup() { + Serial.begin(9600); + + // by default, we'll generate the high voltage from the 3.3v line internally! (neat!) + display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x32) + // init done + + // Show image buffer on the display hardware. + // Since the buffer is intialized with an Adafruit splashscreen + // internally, this will display the splashscreen. + display.display(); + delay(2000); + + // Clear the buffer. + display.clearDisplay(); + + // draw a single pixel + display.drawPixel(10, 10, WHITE); + // Show the display buffer on the hardware. + // NOTE: You _must_ call display after making any drawing commands + // to make them visible on the display hardware! + display.display(); + delay(2000); + display.clearDisplay(); + + // draw many lines + testdrawline(); + display.display(); + delay(2000); + display.clearDisplay(); + + // draw rectangles + testdrawrect(); + display.display(); + delay(2000); + display.clearDisplay(); + + // draw multiple rectangles + testfillrect(); + display.display(); + delay(2000); + display.clearDisplay(); + + // draw mulitple circles + testdrawcircle(); + display.display(); + delay(2000); + display.clearDisplay(); + + // draw a white circle, 10 pixel radius + display.fillCircle(display.width()/2, display.height()/2, 10, WHITE); + display.display(); + delay(2000); + display.clearDisplay(); + + testdrawroundrect(); + delay(2000); + display.clearDisplay(); + + testfillroundrect(); + delay(2000); + display.clearDisplay(); + + testdrawtriangle(); + delay(2000); + display.clearDisplay(); + + testfilltriangle(); + delay(2000); + display.clearDisplay(); + + // draw the first ~12 characters in the font + testdrawchar(); + display.display(); + delay(2000); + display.clearDisplay(); + + // draw scrolling text + testscrolltext(); + delay(2000); + display.clearDisplay(); + + // text display tests + display.setTextSize(1); + display.setTextColor(WHITE); + display.setCursor(0,0); + display.println("Hello, world!"); + display.setTextColor(BLACK, WHITE); // 'inverted' text + display.println(3.141592); + display.setTextSize(2); + display.setTextColor(WHITE); + display.print("0x"); display.println(0xDEADBEEF, HEX); + display.display(); + delay(2000); + display.clearDisplay(); + + // miniature bitmap display + display.drawBitmap(30, 16, logo16_glcd_bmp, 16, 16, 1); + display.display(); + delay(1); + + // invert the display + display.invertDisplay(true); + delay(1000); + display.invertDisplay(false); + delay(1000); + display.clearDisplay(); + + // draw a bitmap icon and 'animate' movement + testdrawbitmap(logo16_glcd_bmp, LOGO16_GLCD_HEIGHT, LOGO16_GLCD_WIDTH); +} + + +void loop() { + +} + + +void testdrawbitmap(const uint8_t *bitmap, uint8_t w, uint8_t h) { + uint8_t icons[NUMFLAKES][3]; + + // initialize + for (uint8_t f=0; f< NUMFLAKES; f++) { + icons[f][XPOS] = random(display.width()); + icons[f][YPOS] = 0; + icons[f][DELTAY] = random(5) + 1; + + Serial.print("x: "); + Serial.print(icons[f][XPOS], DEC); + Serial.print(" y: "); + Serial.print(icons[f][YPOS], DEC); + Serial.print(" dy: "); + Serial.println(icons[f][DELTAY], DEC); + } + + while (1) { + // draw each icon + for (uint8_t f=0; f< NUMFLAKES; f++) { + display.drawBitmap(icons[f][XPOS], icons[f][YPOS], bitmap, w, h, WHITE); + } + display.display(); + delay(200); + + // then erase it + move it + for (uint8_t f=0; f< NUMFLAKES; f++) { + display.drawBitmap(icons[f][XPOS], icons[f][YPOS], bitmap, w, h, BLACK); + // move it + icons[f][YPOS] += icons[f][DELTAY]; + // if its gone, reinit + if (icons[f][YPOS] > display.height()) { + icons[f][XPOS] = random(display.width()); + icons[f][YPOS] = 0; + icons[f][DELTAY] = random(5) + 1; + } + } + } +} + + +void testdrawchar(void) { + display.setTextSize(1); + display.setTextColor(WHITE); + display.setCursor(0,0); + + for (uint8_t i=0; i < 168; i++) { + if (i == '\n') continue; + display.write(i); + if ((i > 0) && (i % 21 == 0)) + display.println(); + } + display.display(); + delay(1); +} + +void testdrawcircle(void) { + for (int16_t i=0; i0; i-=5) { + display.fillTriangle(display.width()/2, display.height()/2-i, + display.width()/2-i, display.height()/2+i, + display.width()/2+i, display.height()/2+i, WHITE); + if (color == WHITE) color = BLACK; + else color = WHITE; + display.display(); + delay(1); + } +} + +void testdrawroundrect(void) { + for (int16_t i=0; i=0; i-=4) { + display.drawLine(0, display.height()-1, display.width()-1, i, WHITE); + display.display(); + delay(1); + } + delay(250); + + display.clearDisplay(); + for (int16_t i=display.width()-1; i>=0; i-=4) { + display.drawLine(display.width()-1, display.height()-1, i, 0, WHITE); + display.display(); + delay(1); + } + for (int16_t i=display.height()-1; i>=0; i-=4) { + display.drawLine(display.width()-1, display.height()-1, 0, i, WHITE); + display.display(); + delay(1); + } + delay(250); + + display.clearDisplay(); + for (int16_t i=0; i http://www.adafruit.com/category/63_98 + +This example is for a 128x32 size display using SPI to communicate +4 or 5 pins are required to interface + +Adafruit invests time and resources providing this open source code, +please support Adafruit and open-source hardware by purchasing +products from Adafruit! + +Written by Limor Fried/Ladyada for Adafruit Industries. +BSD license, check license.txt for more information +All text above, and the splash screen must be included in any redistribution +*********************************************************************/ + +#include +#include +#include +#include + +// If using software SPI (the default case): +#define OLED_MOSI 9 +#define OLED_CLK 10 +#define OLED_DC 11 +#define OLED_CS 12 +#define OLED_RESET 13 +Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS); + +/* Uncomment this block to use hardware SPI +#define OLED_DC 6 +#define OLED_CS 7 +#define OLED_RESET 8 +Adafruit_SSD1306 display(OLED_DC, OLED_RESET, OLED_CS); +*/ + +#define NUMFLAKES 10 +#define XPOS 0 +#define YPOS 1 +#define DELTAY 2 + +#define LOGO16_GLCD_HEIGHT 16 +#define LOGO16_GLCD_WIDTH 16 +static const unsigned char PROGMEM logo16_glcd_bmp[] = +{ B00000000, B11000000, + B00000001, B11000000, + B00000001, B11000000, + B00000011, B11100000, + B11110011, B11100000, + B11111110, B11111000, + B01111110, B11111111, + B00110011, B10011111, + B00011111, B11111100, + B00001101, B01110000, + B00011011, B10100000, + B00111111, B11100000, + B00111111, B11110000, + B01111100, B11110000, + B01110000, B01110000, + B00000000, B00110000 }; + +#if (SSD1306_LCDHEIGHT != 32) +#error("Height incorrect, please fix Adafruit_SSD1306.h!"); +#endif + +void setup() { + Serial.begin(9600); + + // by default, we'll generate the high voltage from the 3.3v line internally! (neat!) + display.begin(SSD1306_SWITCHCAPVCC); + // init done + + // Show image buffer on the display hardware. + // Since the buffer is intialized with an Adafruit splashscreen + // internally, this will display the splashscreen. + display.display(); + delay(2000); + + // Clear the buffer. + display.clearDisplay(); + + // draw a single pixel + display.drawPixel(10, 10, WHITE); + // Show the display buffer on the hardware. + // NOTE: You _must_ call display after making any drawing commands + // to make them visible on the display hardware! + display.display(); + delay(2000); + display.clearDisplay(); + + // draw many lines + testdrawline(); + display.display(); + delay(2000); + display.clearDisplay(); + + // draw rectangles + testdrawrect(); + display.display(); + delay(2000); + display.clearDisplay(); + + // draw multiple rectangles + testfillrect(); + display.display(); + delay(2000); + display.clearDisplay(); + + // draw mulitple circles + testdrawcircle(); + display.display(); + delay(2000); + display.clearDisplay(); + + // draw a white circle, 10 pixel radius + display.fillCircle(display.width()/2, display.height()/2, 10, WHITE); + display.display(); + delay(2000); + display.clearDisplay(); + + testdrawroundrect(); + delay(2000); + display.clearDisplay(); + + testfillroundrect(); + delay(2000); + display.clearDisplay(); + + testdrawtriangle(); + delay(2000); + display.clearDisplay(); + + testfilltriangle(); + delay(2000); + display.clearDisplay(); + + // draw the first ~12 characters in the font + testdrawchar(); + display.display(); + delay(2000); + display.clearDisplay(); + + // draw scrolling text + testscrolltext(); + delay(2000); + display.clearDisplay(); + + // text display tests + display.setTextSize(1); + display.setTextColor(WHITE); + display.setCursor(0,0); + display.println("Hello, world!"); + display.setTextColor(BLACK, WHITE); // 'inverted' text + display.println(3.141592); + display.setTextSize(2); + display.setTextColor(WHITE); + display.print("0x"); display.println(0xDEADBEEF, HEX); + display.display(); + delay(2000); + display.clearDisplay(); + + // miniature bitmap display + display.drawBitmap(30, 16, logo16_glcd_bmp, 16, 16, 1); + display.display(); + + // invert the display + display.invertDisplay(true); + delay(1000); + display.invertDisplay(false); + delay(1000); + display.clearDisplay(); + + // draw a bitmap icon and 'animate' movement + testdrawbitmap(logo16_glcd_bmp, LOGO16_GLCD_HEIGHT, LOGO16_GLCD_WIDTH); +} + + +void loop() { + +} + + +void testdrawbitmap(const uint8_t *bitmap, uint8_t w, uint8_t h) { + uint8_t icons[NUMFLAKES][3]; + + // initialize + for (uint8_t f=0; f< NUMFLAKES; f++) { + icons[f][XPOS] = random(display.width()); + icons[f][YPOS] = 0; + icons[f][DELTAY] = random(5) + 1; + + Serial.print("x: "); + Serial.print(icons[f][XPOS], DEC); + Serial.print(" y: "); + Serial.print(icons[f][YPOS], DEC); + Serial.print(" dy: "); + Serial.println(icons[f][DELTAY], DEC); + } + + while (1) { + // draw each icon + for (uint8_t f=0; f< NUMFLAKES; f++) { + display.drawBitmap(icons[f][XPOS], icons[f][YPOS], bitmap, w, h, WHITE); + } + display.display(); + delay(200); + + // then erase it + move it + for (uint8_t f=0; f< NUMFLAKES; f++) { + display.drawBitmap(icons[f][XPOS], icons[f][YPOS], bitmap, w, h, BLACK); + // move it + icons[f][YPOS] += icons[f][DELTAY]; + // if its gone, reinit + if (icons[f][YPOS] > display.height()) { + icons[f][XPOS] = random(display.width()); + icons[f][YPOS] = 0; + icons[f][DELTAY] = random(5) + 1; + } + } + } +} + + +void testdrawchar(void) { + display.setTextSize(1); + display.setTextColor(WHITE); + display.setCursor(0,0); + + for (uint8_t i=0; i < 168; i++) { + if (i == '\n') continue; + display.write(i); + if ((i > 0) && (i % 21 == 0)) + display.println(); + } + display.display(); +} + +void testdrawcircle(void) { + for (int16_t i=0; i0; i-=5) { + display.fillTriangle(display.width()/2, display.height()/2-i, + display.width()/2-i, display.height()/2+i, + display.width()/2+i, display.height()/2+i, WHITE); + if (color == WHITE) color = BLACK; + else color = WHITE; + display.display(); + } +} + +void testdrawroundrect(void) { + for (int16_t i=0; i=0; i-=4) { + display.drawLine(0, display.height()-1, display.width()-1, i, WHITE); + display.display(); + } + delay(250); + + display.clearDisplay(); + for (int16_t i=display.width()-1; i>=0; i-=4) { + display.drawLine(display.width()-1, display.height()-1, i, 0, WHITE); + display.display(); + } + for (int16_t i=display.height()-1; i>=0; i-=4) { + display.drawLine(display.width()-1, display.height()-1, 0, i, WHITE); + display.display(); + } + delay(250); + + display.clearDisplay(); + for (int16_t i=0; i http://www.adafruit.com/category/63_98 + +This example is for a 128x64 size display using I2C to communicate +3 pins are required to interface (2 I2C and one reset) + +Adafruit invests time and resources providing this open source code, +please support Adafruit and open-source hardware by purchasing +products from Adafruit! + +Written by Limor Fried/Ladyada for Adafruit Industries. +BSD license, check license.txt for more information +All text above, and the splash screen must be included in any redistribution +*********************************************************************/ + +#include +#include +#include +#include + +#define OLED_RESET 4 +Adafruit_SSD1306 display(OLED_RESET); + +#define NUMFLAKES 10 +#define XPOS 0 +#define YPOS 1 +#define DELTAY 2 + + +#define LOGO16_GLCD_HEIGHT 16 +#define LOGO16_GLCD_WIDTH 16 +static const unsigned char PROGMEM logo16_glcd_bmp[] = +{ B00000000, B11000000, + B00000001, B11000000, + B00000001, B11000000, + B00000011, B11100000, + B11110011, B11100000, + B11111110, B11111000, + B01111110, B11111111, + B00110011, B10011111, + B00011111, B11111100, + B00001101, B01110000, + B00011011, B10100000, + B00111111, B11100000, + B00111111, B11110000, + B01111100, B11110000, + B01110000, B01110000, + B00000000, B00110000 }; + +#if (SSD1306_LCDHEIGHT != 64) +#error("Height incorrect, please fix Adafruit_SSD1306.h!"); +#endif + +void setup() { + Serial.begin(9600); + + // by default, we'll generate the high voltage from the 3.3v line internally! (neat!) + display.begin(SSD1306_SWITCHCAPVCC, 0x3D); // initialize with the I2C addr 0x3D (for the 128x64) + // init done + + // Show image buffer on the display hardware. + // Since the buffer is intialized with an Adafruit splashscreen + // internally, this will display the splashscreen. + display.display(); + delay(2000); + + // Clear the buffer. + display.clearDisplay(); + + // draw a single pixel + display.drawPixel(10, 10, WHITE); + // Show the display buffer on the hardware. + // NOTE: You _must_ call display after making any drawing commands + // to make them visible on the display hardware! + display.display(); + delay(2000); + display.clearDisplay(); + + // draw many lines + testdrawline(); + display.display(); + delay(2000); + display.clearDisplay(); + + // draw rectangles + testdrawrect(); + display.display(); + delay(2000); + display.clearDisplay(); + + // draw multiple rectangles + testfillrect(); + display.display(); + delay(2000); + display.clearDisplay(); + + // draw mulitple circles + testdrawcircle(); + display.display(); + delay(2000); + display.clearDisplay(); + + // draw a white circle, 10 pixel radius + display.fillCircle(display.width()/2, display.height()/2, 10, WHITE); + display.display(); + delay(2000); + display.clearDisplay(); + + testdrawroundrect(); + delay(2000); + display.clearDisplay(); + + testfillroundrect(); + delay(2000); + display.clearDisplay(); + + testdrawtriangle(); + delay(2000); + display.clearDisplay(); + + testfilltriangle(); + delay(2000); + display.clearDisplay(); + + // draw the first ~12 characters in the font + testdrawchar(); + display.display(); + delay(2000); + display.clearDisplay(); + + // draw scrolling text + testscrolltext(); + delay(2000); + display.clearDisplay(); + + // text display tests + display.setTextSize(1); + display.setTextColor(WHITE); + display.setCursor(0,0); + display.println("Hello, world!"); + display.setTextColor(BLACK, WHITE); // 'inverted' text + display.println(3.141592); + display.setTextSize(2); + display.setTextColor(WHITE); + display.print("0x"); display.println(0xDEADBEEF, HEX); + display.display(); + delay(2000); + display.clearDisplay(); + + // miniature bitmap display + display.drawBitmap(30, 16, logo16_glcd_bmp, 16, 16, 1); + display.display(); + delay(1); + + // invert the display + display.invertDisplay(true); + delay(1000); + display.invertDisplay(false); + delay(1000); + display.clearDisplay(); + + // draw a bitmap icon and 'animate' movement + testdrawbitmap(logo16_glcd_bmp, LOGO16_GLCD_HEIGHT, LOGO16_GLCD_WIDTH); +} + + +void loop() { + +} + + +void testdrawbitmap(const uint8_t *bitmap, uint8_t w, uint8_t h) { + uint8_t icons[NUMFLAKES][3]; + + // initialize + for (uint8_t f=0; f< NUMFLAKES; f++) { + icons[f][XPOS] = random(display.width()); + icons[f][YPOS] = 0; + icons[f][DELTAY] = random(5) + 1; + + Serial.print("x: "); + Serial.print(icons[f][XPOS], DEC); + Serial.print(" y: "); + Serial.print(icons[f][YPOS], DEC); + Serial.print(" dy: "); + Serial.println(icons[f][DELTAY], DEC); + } + + while (1) { + // draw each icon + for (uint8_t f=0; f< NUMFLAKES; f++) { + display.drawBitmap(icons[f][XPOS], icons[f][YPOS], bitmap, w, h, WHITE); + } + display.display(); + delay(200); + + // then erase it + move it + for (uint8_t f=0; f< NUMFLAKES; f++) { + display.drawBitmap(icons[f][XPOS], icons[f][YPOS], bitmap, w, h, BLACK); + // move it + icons[f][YPOS] += icons[f][DELTAY]; + // if its gone, reinit + if (icons[f][YPOS] > display.height()) { + icons[f][XPOS] = random(display.width()); + icons[f][YPOS] = 0; + icons[f][DELTAY] = random(5) + 1; + } + } + } +} + + +void testdrawchar(void) { + display.setTextSize(1); + display.setTextColor(WHITE); + display.setCursor(0,0); + + for (uint8_t i=0; i < 168; i++) { + if (i == '\n') continue; + display.write(i); + if ((i > 0) && (i % 21 == 0)) + display.println(); + } + display.display(); + delay(1); +} + +void testdrawcircle(void) { + for (int16_t i=0; i0; i-=5) { + display.fillTriangle(display.width()/2, display.height()/2-i, + display.width()/2-i, display.height()/2+i, + display.width()/2+i, display.height()/2+i, WHITE); + if (color == WHITE) color = BLACK; + else color = WHITE; + display.display(); + delay(1); + } +} + +void testdrawroundrect(void) { + for (int16_t i=0; i=0; i-=4) { + display.drawLine(0, display.height()-1, display.width()-1, i, WHITE); + display.display(); + delay(1); + } + delay(250); + + display.clearDisplay(); + for (int16_t i=display.width()-1; i>=0; i-=4) { + display.drawLine(display.width()-1, display.height()-1, i, 0, WHITE); + display.display(); + delay(1); + } + for (int16_t i=display.height()-1; i>=0; i-=4) { + display.drawLine(display.width()-1, display.height()-1, 0, i, WHITE); + display.display(); + delay(1); + } + delay(250); + + display.clearDisplay(); + for (int16_t i=0; i http://www.adafruit.com/category/63_98 + +This example is for a 128x64 size display using SPI to communicate +4 or 5 pins are required to interface + +Adafruit invests time and resources providing this open source code, +please support Adafruit and open-source hardware by purchasing +products from Adafruit! + +Written by Limor Fried/Ladyada for Adafruit Industries. +BSD license, check license.txt for more information +All text above, and the splash screen must be included in any redistribution +*********************************************************************/ + +#include +#include +#include +#include + +// If using software SPI (the default case): +#define OLED_MOSI 9 +#define OLED_CLK 10 +#define OLED_DC 11 +#define OLED_CS 12 +#define OLED_RESET 13 +Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS); + +/* Uncomment this block to use hardware SPI +#define OLED_DC 6 +#define OLED_CS 7 +#define OLED_RESET 8 +Adafruit_SSD1306 display(OLED_DC, OLED_RESET, OLED_CS); +*/ + +#define NUMFLAKES 10 +#define XPOS 0 +#define YPOS 1 +#define DELTAY 2 + +#define LOGO16_GLCD_HEIGHT 16 +#define LOGO16_GLCD_WIDTH 16 +static const unsigned char PROGMEM logo16_glcd_bmp[] = +{ B00000000, B11000000, + B00000001, B11000000, + B00000001, B11000000, + B00000011, B11100000, + B11110011, B11100000, + B11111110, B11111000, + B01111110, B11111111, + B00110011, B10011111, + B00011111, B11111100, + B00001101, B01110000, + B00011011, B10100000, + B00111111, B11100000, + B00111111, B11110000, + B01111100, B11110000, + B01110000, B01110000, + B00000000, B00110000 }; + +#if (SSD1306_LCDHEIGHT != 64) +#error("Height incorrect, please fix Adafruit_SSD1306.h!"); +#endif + +void setup() { + Serial.begin(9600); + + // by default, we'll generate the high voltage from the 3.3v line internally! (neat!) + display.begin(SSD1306_SWITCHCAPVCC); + // init done + + // Show image buffer on the display hardware. + // Since the buffer is intialized with an Adafruit splashscreen + // internally, this will display the splashscreen. + display.display(); + delay(2000); + + // Clear the buffer. + display.clearDisplay(); + + // draw a single pixel + display.drawPixel(10, 10, WHITE); + // Show the display buffer on the hardware. + // NOTE: You _must_ call display after making any drawing commands + // to make them visible on the display hardware! + display.display(); + delay(2000); + display.clearDisplay(); + + // draw many lines + testdrawline(); + display.display(); + delay(2000); + display.clearDisplay(); + + // draw rectangles + testdrawrect(); + display.display(); + delay(2000); + display.clearDisplay(); + + // draw multiple rectangles + testfillrect(); + display.display(); + delay(2000); + display.clearDisplay(); + + // draw mulitple circles + testdrawcircle(); + display.display(); + delay(2000); + display.clearDisplay(); + + // draw a white circle, 10 pixel radius + display.fillCircle(display.width()/2, display.height()/2, 10, WHITE); + display.display(); + delay(2000); + display.clearDisplay(); + + testdrawroundrect(); + delay(2000); + display.clearDisplay(); + + testfillroundrect(); + delay(2000); + display.clearDisplay(); + + testdrawtriangle(); + delay(2000); + display.clearDisplay(); + + testfilltriangle(); + delay(2000); + display.clearDisplay(); + + // draw the first ~12 characters in the font + testdrawchar(); + display.display(); + delay(2000); + display.clearDisplay(); + + // draw scrolling text + testscrolltext(); + delay(2000); + display.clearDisplay(); + + // text display tests + display.setTextSize(1); + display.setTextColor(WHITE); + display.setCursor(0,0); + display.println("Hello, world!"); + display.setTextColor(BLACK, WHITE); // 'inverted' text + display.println(3.141592); + display.setTextSize(2); + display.setTextColor(WHITE); + display.print("0x"); display.println(0xDEADBEEF, HEX); + display.display(); + delay(2000); + display.clearDisplay(); + + // miniature bitmap display + display.drawBitmap(30, 16, logo16_glcd_bmp, 16, 16, 1); + display.display(); + + // invert the display + display.invertDisplay(true); + delay(1000); + display.invertDisplay(false); + delay(1000); + display.clearDisplay(); + + // draw a bitmap icon and 'animate' movement + testdrawbitmap(logo16_glcd_bmp, LOGO16_GLCD_HEIGHT, LOGO16_GLCD_WIDTH); +} + + +void loop() { + +} + + +void testdrawbitmap(const uint8_t *bitmap, uint8_t w, uint8_t h) { + uint8_t icons[NUMFLAKES][3]; + + // initialize + for (uint8_t f=0; f< NUMFLAKES; f++) { + icons[f][XPOS] = random(display.width()); + icons[f][YPOS] = 0; + icons[f][DELTAY] = random(5) + 1; + + Serial.print("x: "); + Serial.print(icons[f][XPOS], DEC); + Serial.print(" y: "); + Serial.print(icons[f][YPOS], DEC); + Serial.print(" dy: "); + Serial.println(icons[f][DELTAY], DEC); + } + + while (1) { + // draw each icon + for (uint8_t f=0; f< NUMFLAKES; f++) { + display.drawBitmap(icons[f][XPOS], icons[f][YPOS], bitmap, w, h, WHITE); + } + display.display(); + delay(200); + + // then erase it + move it + for (uint8_t f=0; f< NUMFLAKES; f++) { + display.drawBitmap(icons[f][XPOS], icons[f][YPOS], bitmap, w, h, BLACK); + // move it + icons[f][YPOS] += icons[f][DELTAY]; + // if its gone, reinit + if (icons[f][YPOS] > display.height()) { + icons[f][XPOS] = random(display.width()); + icons[f][YPOS] = 0; + icons[f][DELTAY] = random(5) + 1; + } + } + } +} + + +void testdrawchar(void) { + display.setTextSize(1); + display.setTextColor(WHITE); + display.setCursor(0,0); + + for (uint8_t i=0; i < 168; i++) { + if (i == '\n') continue; + display.write(i); + if ((i > 0) && (i % 21 == 0)) + display.println(); + } + display.display(); +} + +void testdrawcircle(void) { + for (int16_t i=0; i0; i-=5) { + display.fillTriangle(display.width()/2, display.height()/2-i, + display.width()/2-i, display.height()/2+i, + display.width()/2+i, display.height()/2+i, WHITE); + if (color == WHITE) color = BLACK; + else color = WHITE; + display.display(); + } +} + +void testdrawroundrect(void) { + for (int16_t i=0; i=0; i-=4) { + display.drawLine(0, display.height()-1, display.width()-1, i, WHITE); + display.display(); + } + delay(250); + + display.clearDisplay(); + for (int16_t i=display.width()-1; i>=0; i-=4) { + display.drawLine(display.width()-1, display.height()-1, i, 0, WHITE); + display.display(); + } + for (int16_t i=display.height()-1; i>=0; i-=4) { + display.drawLine(display.width()-1, display.height()-1, 0, i, WHITE); + display.display(); + } + delay(250); + + display.clearDisplay(); + for (int16_t i=0; i +sentence=SSD1306 oled driver library for 'monochrome' 128x64 and 128x32 OLEDs! +paragraph=SSD1306 oled driver library for 'monochrome' 128x64 and 128x32 OLEDs! +category=Display +url=https://github.com/adafruit/Adafruit_SSD1306 +architectures=* diff --git a/src/license.txt b/src/license.txt new file mode 100644 index 0000000..f6a0f22 --- /dev/null +++ b/src/license.txt @@ -0,0 +1,26 @@ +Software License Agreement (BSD License) + +Copyright (c) 2012, Adafruit Industries +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: +1. Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +3. Neither the name of the copyright holders nor the +names of its contributors may be used to endorse or promote products +derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY +EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/src/mgos_arduino_adafruit_ssd1306_c.c b/src/mgos_arduino_adafruit_ssd1306_c.c new file mode 100644 index 0000000..9fca625 --- /dev/null +++ b/src/mgos_arduino_adafruit_ssd1306_c.c @@ -0,0 +1,5 @@ +#include + +bool mgos_arduino_adafruit_ssd1306_init(void) { + return true; +} diff --git a/src/mgos_arduino_ssd1306.cpp b/src/mgos_arduino_ssd1306.cpp new file mode 100644 index 0000000..932f82e --- /dev/null +++ b/src/mgos_arduino_ssd1306.cpp @@ -0,0 +1,219 @@ +/* + * Copyright (c) 2014-2017 Cesanta Software Limited + * All rights reserved + * + * Arduino Adafruit_SSD1306 library API wrapper + */ + +#include "mgos_arduino_ssd1306.h" + +static Adafruit_SSD1306::Resolution mgos_ssd1306_get_res(int res) { + Adafruit_SSD1306::Resolution r; + switch (res) { + case 0: r = Adafruit_SSD1306::RES_96_16; break; + case 1: r = Adafruit_SSD1306::RES_128_32; break; + case 2: r = Adafruit_SSD1306::RES_128_64; break; + default: r = SSD1306_DEFAULT_RES; + } + return r; +} + +Adafruit_SSD1306 *mgos_ssd1306_create_i2c(int rst, int res) { + return new Adafruit_SSD1306(rst, mgos_ssd1306_get_res(res)); +} + +Adafruit_SSD1306 *mgos_ssd1306_create_spi(int dc, int rst, int cs, int res) { + return new Adafruit_SSD1306(dc, rst, cs, mgos_ssd1306_get_res(res)); +} + +void mgos_ssd1306_close(Adafruit_SSD1306 *ssd) { + if (ssd != nullptr) { + delete ssd; + ssd = nullptr; + } +} + +void mgos_ssd1306_begin(Adafruit_SSD1306 *ssd, int vccstate, int i2caddr, bool reset) { + if (ssd == nullptr) return; + ssd->begin(vccstate, i2caddr, reset); +} + +void mgos_ssd1306_command(Adafruit_SSD1306 *ssd, int c) { + if (ssd == nullptr) return; + ssd->ssd1306_command(c); +} + +void mgos_ssd1306_clear_display(Adafruit_SSD1306 *ssd) { + if (ssd == nullptr) return; + ssd->clearDisplay(); +} + +void mgos_ssd1306_invert_display(Adafruit_SSD1306 *ssd, int i) { + if (ssd == nullptr) return; + ssd->invertDisplay(i); +} + +void mgos_ssd1306_display(Adafruit_SSD1306 *ssd) { + if (ssd == nullptr) return; + ssd->display(); +} + +void mgos_ssd1306_start_scroll_right(Adafruit_SSD1306 *ssd, int start, int stop) { + if (ssd == nullptr) return; + ssd->startscrollright(start, stop); +} + +void mgos_ssd1306_start_scroll_left(Adafruit_SSD1306 *ssd, int start, int stop) { + if (ssd == nullptr) return; + ssd->startscrollleft(start, stop); +} + +void mgos_ssd1306_start_scroll_diag_right(Adafruit_SSD1306 *ssd, int start, int stop) { + if (ssd == nullptr) return; + ssd->startscrolldiagright(start, stop); +} + +void mgos_ssd1306_start_scroll_diag_left(Adafruit_SSD1306 *ssd, int start, int stop) { + if (ssd == nullptr) return; + ssd->startscrolldiagleft(start, stop); +} + +void mgos_ssd1306_stop_scroll(Adafruit_SSD1306 *ssd) { + if (ssd == nullptr) return; + ssd->stopscroll(); +} + +void mgos_ssd1306_dim(Adafruit_SSD1306 *ssd, int dim) { + if (ssd == nullptr) return; + ssd->dim(dim); +} + +void mgos_ssd1306_draw_pixel(Adafruit_SSD1306 *ssd, int x, int y, int color) { + if (ssd == nullptr) return; + ssd->drawPixel(x, y, color); +} + +void mgos_ssd1306_draw_fast_vline(Adafruit_SSD1306 *ssd, int x, int y, int h, int color) { + if (ssd == nullptr) return; + ssd->drawFastVLine(x, y, h, color); +} + +void mgos_ssd1306_draw_fast_hline(Adafruit_SSD1306 *ssd, int x, int y, int w, int color) { + if (ssd == nullptr) return; + ssd->drawFastHLine(x, y, w, color); +} + +//Adafruit_GFX +int mgos_ssd1306_make_xy_pair(int x, int y) { + return ((int)x << 16) + y; +} + +void mgos_ssd1306_draw_circle(Adafruit_SSD1306 *ssd, + int x0y0, int r, int color) { + if (ssd == nullptr) return; + ssd->drawCircle((x0y0 >> 16), x0y0, r, color); +} + +void mgos_ssd1306_draw_circle_helper(Adafruit_SSD1306 *ssd, + int x0y0, int r, int cornername, int color) { + if (ssd == nullptr) return; + ssd->drawCircleHelper((x0y0 >> 16), x0y0, r, cornername, color); +} + +void mgos_ssd1306_fill_circle(Adafruit_SSD1306 *ssd, + int x0y0, int r, int color) { + if (ssd == nullptr) return; + ssd->fillCircle((x0y0 >> 16), x0y0, r, color); +} + +void mgos_ssd1306_fill_circle_helper(Adafruit_SSD1306 *ssd, + int x0y0, int r, int cornername, int delta, int color) { + if (ssd == nullptr) return; + ssd->fillCircleHelper((x0y0 >> 16), x0y0, r, cornername, delta, color); +} + +void mgos_ssd1306_draw_triangle(Adafruit_SSD1306 *ssd, + int x0y0, int x1y1, int x2y2, int color) { + if (ssd == nullptr) return; + ssd->drawTriangle((x0y0 >> 16), x0y0, (x1y1 >> 16), x1y1, (x2y2 >> 16), x2y2, color); +} + +void mgos_ssd1306_fill_triangle(Adafruit_SSD1306 *ssd, + int x0y0, int x1y1, int x2y2, int color) { + if (ssd == nullptr) return; + ssd->fillTriangle((x0y0 >> 16), x0y0, (x1y1 >> 16), x1y1, (x2y2 >> 16), x2y2, color); +} + +void mgos_ssd1306_draw_round_rect(Adafruit_SSD1306 *ssd, + int x0y0, int w, int h, int radius, int color) { + if (ssd == nullptr) return; + ssd-> drawRoundRect((x0y0 >> 16), x0y0, w, h, radius, color); +} + +void mgos_ssd1306_fill_round_rect(Adafruit_SSD1306 *ssd, + int x0y0, int w, int h, int radius, int color) { + if (ssd == nullptr) return; + ssd->fillRoundRect((x0y0 >> 16), x0y0, w, h, radius, color); +} + +void mgos_ssd1306_draw_char(Adafruit_SSD1306 *ssd, + int xy, int c, int color, int bg, int size) { + if (ssd == nullptr) return; + ssd->drawChar((xy >> 16), xy, c, color, bg, size); +} + +void mgos_ssd1306_set_cursor(Adafruit_SSD1306 *ssd, int x, int y) { + if (ssd == nullptr) return; + ssd->setCursor(x, y); +} + +void mgos_ssd1306_set_text_color(Adafruit_SSD1306 *ssd, int c) { + if (ssd == nullptr) return; + ssd->setTextColor(c); +} + +void mgos_ssd1306_set_text_color_bg(Adafruit_SSD1306 *ssd, int c, int bg) { + if (ssd == nullptr) return; + ssd->setTextColor(c, bg); +} + +void mgos_ssd1306_set_text_size(Adafruit_SSD1306 *ssd, int s) { + if (ssd == nullptr) return; + ssd->setTextSize(s); +} + +void mgos_ssd1306_set_text_wrap(Adafruit_SSD1306 *ssd, boolean w) { + if (ssd == nullptr) return; + ssd->setTextWrap(w); +} + +int mgos_ssd1306_write(Adafruit_SSD1306 *ssd, const char *buffer, int size) { + if (ssd == nullptr) return 0; + return ssd->Print::write(buffer, size); +} + +int mgos_ssd1306_height(Adafruit_SSD1306 *ssd) { + if (ssd == nullptr) return 0; + return ssd->height(); +} + +int mgos_ssd1306_width(Adafruit_SSD1306 *ssd) { + if (ssd == nullptr) return 0; + return ssd->width(); +} + +int mgos_ssd1306_get_rotation(Adafruit_SSD1306 *ssd) { + if (ssd == nullptr) return 0; + return ssd->getRotation(); +} + +// get current cursor position (get rotation safe maximum values, using: width() for x, height() for y) +int mgos_ssd1306_get_cursor_x(Adafruit_SSD1306 *ssd) { + if (ssd == nullptr) return 0; + return ssd->getCursorX(); +} + +int mgos_ssd1306_get_cursor_y(Adafruit_SSD1306 *ssd) { + if (ssd == nullptr) return 0; + return ssd->getCursorY(); +} diff --git a/src/mgos_arduino_ssd1306.h b/src/mgos_arduino_ssd1306.h new file mode 100644 index 0000000..3a78169 --- /dev/null +++ b/src/mgos_arduino_ssd1306.h @@ -0,0 +1,107 @@ +/* + * Copyright (c) 2014-2017 Cesanta Software Limited + * All rights reserved + * + * Arduino Adafruit_SSD1306 library API wrapper + */ + +#include "Adafruit_SSD1306.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + +// Initialize Adafruit_SSD1306 library for I2C. +// Return value: handle opaque pointer. +// We set the reset pin and +// Resolution: 0 - RES_96_16, 1 - RES_128_32, 2 - RES_128_64. +Adafruit_SSD1306 *mgos_ssd1306_create_i2c(int rst, int res); +// Initialize Adafruit_SSD1306 library for SPI. +// Return value: handle opaque pointer. +// We set DataCommand, ChipSelect, Reset and +// Resolution: 0 - RES_96_16, 1 - RES_128_32, 2 - RES_128_64. +Adafruit_SSD1306 *mgos_ssd1306_create_spi(int dc, int rst, int cs, int res); +// Close Adafruit_SSD1306 handle. Return value: none. +void mgos_ssd1306_close(Adafruit_SSD1306 *ssd); +// Initialize the display. +void mgos_ssd1306_begin(Adafruit_SSD1306 *ssd, int vccstate, int i2caddr, bool reset); + +void mgos_ssd1306_command(Adafruit_SSD1306 *ssd, int c); + +void mgos_ssd1306_clear_display(Adafruit_SSD1306 *ssd); +void mgos_ssd1306_invert_display(Adafruit_SSD1306 *ssd, int i); +void mgos_ssd1306_display(Adafruit_SSD1306 *ssd); + +// Activates a right handed scroll for rows start through stop. +void mgos_ssd1306_start_scroll_right(Adafruit_SSD1306 *ssd, int start, int stop); +// Activates a right handed scroll for rows start through stop. +void mgos_ssd1306_start_scroll_left(Adafruit_SSD1306 *ssd, int start, int stop); +// Activates a diagonal scroll for rows start through stop. +void mgos_ssd1306_start_scroll_diag_right(Adafruit_SSD1306 *ssd, int start, int stop); +// Activates a diagonal scroll for rows start through stop. +void mgos_ssd1306_start_scroll_diag_left(Adafruit_SSD1306 *ssd, int start, int stop); +// Stops scroll. +void mgos_ssd1306_stop_scroll(Adafruit_SSD1306 *ssd); + +// Dims the display. +// dim = 1: display is dimmed; +// dim = 0: display is normal. +void mgos_ssd1306_dim(Adafruit_SSD1306 *ssd, int dim); + +// Sets a single pixel. +void mgos_ssd1306_draw_pixel(Adafruit_SSD1306 *ssd, int x, int y, int color); +void mgos_ssd1306_draw_fast_vline(Adafruit_SSD1306 *ssd, int x, int y, int h, int color); +void mgos_ssd1306_draw_fast_hline(Adafruit_SSD1306 *ssd, int x, int y, int w, int color); + +//Adafruit_GFX +int mgos_ssd1306_make_xy_pair(int x, int y); + +void mgos_ssd1306_draw_circle(Adafruit_SSD1306 *ssd, + int x0y0, int r, int color); + +void mgos_ssd1306_draw_circle_helper(Adafruit_SSD1306 *ssd, + int x0y0, int r, int cornername, int color); + +void mgos_ssd1306_fill_circle(Adafruit_SSD1306 *ssd, + int x0y0, int r, int color); + +void mgos_ssd1306_fill_circle_helper(Adafruit_SSD1306 *ssd, + int x0y0, int r, int cornername, int delta, int color); + +void mgos_ssd1306_draw_triangle(Adafruit_SSD1306 *ssd, + int x0y0, int x1y1, int x2y2, int color); + +void mgos_ssd1306_fill_triangle(Adafruit_SSD1306 *ssd, + int x0y0, int x1y1, int x2y2, int color); + +void mgos_ssd1306_draw_round_rect(Adafruit_SSD1306 *ssd, + int x0y0, int w, int h, int radius, int color); + +void mgos_ssd1306_fill_round_rect(Adafruit_SSD1306 *ssd, + int x0y0, int w, int h, int radius, int color); + +void mgos_ssd1306_draw_char(Adafruit_SSD1306 *ssd, + int xy, int c, int color, int bg, int size); + +void mgos_ssd1306_set_cursor(Adafruit_SSD1306 *ssd, int x, int y); + +void mgos_ssd1306_set_text_color(Adafruit_SSD1306 *ssd, int c); +void mgos_ssd1306_set_text_color_bg(Adafruit_SSD1306 *ssd, int c, int bg); +void mgos_ssd1306_set_text_size(Adafruit_SSD1306 *ssd, int s); +void mgos_ssd1306_set_text_wrap(Adafruit_SSD1306 *ssd, boolean w); + +int mgos_ssd1306_write(Adafruit_SSD1306 *ssd, const char *buffer, int size); + +int mgos_ssd1306_height(Adafruit_SSD1306 *ssd); +int mgos_ssd1306_width(Adafruit_SSD1306 *ssd); + +int mgos_ssd1306_get_rotation(Adafruit_SSD1306 *ssd); + +// get current cursor position (get rotation safe maximum values, using: width() for x, height() for y) +int mgos_ssd1306_get_cursor_x(Adafruit_SSD1306 *ssd); +int mgos_ssd1306_get_cursor_y(Adafruit_SSD1306 *ssd); + +#ifdef __cplusplus +} +#endif /* __cplusplus */