SSD1306/lib/ssd1306.c

592 lines
25 KiB
C
Raw Normal View History

2022-12-07 09:53:48 -05:00
/**
* --------------------------------------------------------------------------------------+
2020-10-13 04:45:09 -04:00
* @desc SSD1306 OLED Driver
2022-12-07 09:53:48 -05:00
* --------------------------------------------------------------------------------------+
2020-10-13 04:45:09 -04:00
* Copyright (C) 2020 Marian Hrinko.
* Written by Marian Hrinko (mato.hrinko@gmail.com)
*
* @author Marian Hrinko
2022-12-08 16:35:51 -05:00
* @date 06.10.2020
2022-12-07 14:27:09 -05:00
* @update 06.12.2022
2022-11-22 14:38:14 -05:00
* @file ssd1306.c
2022-11-21 08:31:26 -05:00
* @version 3.0
2022-11-24 16:40:17 -05:00
* @tested AVR Atmega328p
2020-10-13 04:48:15 -04:00
*
* @depend font.h, twi.h
2022-12-07 09:53:48 -05:00
* --------------------------------------------------------------------------------------+
2022-11-22 14:38:14 -05:00
* @descr Version 1.0 -> applicable for 1 display
* Version 2.0 -> rebuild to 'cacheMemLcd' array
2022-12-09 18:54:34 -05:00
* Version 3.0 -> simplified alphanumeric version
2022-12-07 09:53:48 -05:00
* --------------------------------------------------------------------------------------+
2022-11-22 14:38:14 -05:00
* @usage Basic Setup for OLED Display
2020-10-13 04:45:09 -04:00
*/
2022-12-08 16:35:51 -05:00
// @includes
2020-10-13 04:45:09 -04:00
#include "ssd1306.h"
// +---------------------------+
// | Set MUX Ratio |
// +---------------------------+
// | 0xA8, 0x3F |
// +---------------------------+
// |
// +---------------------------+
// | Set Display Offset |
// +---------------------------+
// | 0xD3, 0x00 |
// +---------------------------+
// |
// +---------------------------+
// | Set Display Start Line |
// +---------------------------+
// | 0x40 |
// +---------------------------+
// |
// +---------------------------+
// | Set Segment Remap |
// +---------------------------+
// | 0xA0 / 0xA1 |
// +---------------------------+
// |
// +---------------------------+
// | Set COM Output Scan |
// | Direction |
// +---------------------------+
// | 0xC0 / 0xC8 |
// +---------------------------+
// |
// +---------------------------+
// | Set COM Pins hardware |
// | configuration |
// +---------------------------+
// | 0xDA, 0x02 |
// +---------------------------+
// |
// +---------------------------+
// | Set Contrast Control |
// +---------------------------+
// | 0x81, 0x7F |
// +---------------------------+
// |
// +---------------------------+
// | Disable Entire Display On |
// +---------------------------+
// | 0xA4 |
// +---------------------------+
// |
// +---------------------------+
// | Set Normal Display |
// +---------------------------+
// | 0xA6 |
// +---------------------------+
// |
// +---------------------------+
// | Set Osc Frequency |
// +---------------------------+
// | 0xD5, 0x80 |
// +---------------------------+
// |
// +---------------------------+
// | Enable charge pump |
// | regulator |
// +---------------------------+
// | 0x8D, 0x14 |
// +---------------------------+
// |
// +---------------------------+
// | Display On |
// +---------------------------+
// | 0xAF |
// +---------------------------+
2022-11-21 08:31:26 -05:00
// @array Init command
2020-10-13 04:45:09 -04:00
const uint8_t INIT_SSD1306[] PROGMEM = {
2022-11-21 08:31:26 -05:00
18, // number of initializers
0, SSD1306_DISPLAY_OFF, // 0xAE = Set Display OFF
2022-12-09 18:15:17 -05:00
1, SSD1306_SET_MUX_RATIO, 63, // 0xA8 - 64MUX for 128 x 64 version
2022-12-05 16:46:24 -05:00
// - 32MUX for 128 x 32 version
2022-11-21 08:31:26 -05:00
1, SSD1306_MEMORY_ADDR_MODE, 0x00, // 0x20 = Set Memory Addressing Mode
// 0x00 - Horizontal Addressing Mode
// 0x01 - Vertical Addressing Mode
// 0x02 - Page Addressing Mode (RESET)
2022-11-25 03:05:32 -05:00
2, SSD1306_SET_COLUMN_ADDR, START_COLUMN_ADDR, END_COLUMN_ADDR, // 0x21 = Set Column Address, 0 - 127
2, SSD1306_SET_PAGE_ADDR, START_PAGE_ADDR, END_PAGE_ADDR, // 0x22 = Set Page Address, 0 - 7
2022-11-21 08:31:26 -05:00
0, SSD1306_SET_START_LINE, // 0x40
1, SSD1306_DISPLAY_OFFSET, 0x00, // 0xD3
0, SSD1306_SEG_REMAP_OP, // 0xA0 / remap 0xA1
0, SSD1306_COM_SCAN_DIR_OP, // 0xC0 / remap 0xC8
2022-12-09 18:15:17 -05:00
1, SSD1306_COM_PIN_CONF, 0x12, // 0xDA, 0x12 - Disable COM Left/Right remap, Alternative COM pin configuration
2022-12-05 16:46:24 -05:00
// 0x12 - for 128 x 64 version
// 0x02 - for 128 x 32 version
2022-11-25 03:05:32 -05:00
1, SSD1306_SET_CONTRAST, 0x7F, // 0x81, 0x7F - reset value (max 0xFF)
2022-11-21 08:31:26 -05:00
0, SSD1306_DIS_ENT_DISP_ON, // 0xA4
0, SSD1306_DIS_NORMAL, // 0xA6
2022-11-25 03:05:32 -05:00
1, SSD1306_SET_OSC_FREQ, 0x80, // 0xD5, 0x80 => D=1; DCLK = Fosc / D <=> DCLK = Fosc
1, SSD1306_SET_PRECHARGE, 0xc2, // 0xD9, higher value less blinking
// 0xC2, 1st phase = 2 DCLK, 2nd phase = 13 DCLK
2022-11-21 08:31:26 -05:00
1, SSD1306_VCOM_DESELECT, 0x20, // Set V COMH Deselect, reset value 0x22 = 0,77xUcc
2022-11-25 03:05:32 -05:00
1, SSD1306_SET_CHAR_REG, 0x14, // 0x8D, Enable charge pump during display on
2022-11-21 08:31:26 -05:00
0, SSD1306_DISPLAY_ON // 0xAF = Set Display ON
2020-10-13 04:45:09 -04:00
};
2022-11-24 16:40:17 -05:00
unsigned short int _indexCol = START_COLUMN_ADDR; // @var global - cache index column
unsigned short int _indexPage = START_PAGE_ADDR; // @var global - cache index page
2020-10-13 04:45:09 -04:00
/**
2022-12-09 18:15:17 -05:00
* @desc SSD1306 Send Start and SLAW request
2020-10-13 04:45:09 -04:00
*
2022-12-09 18:15:17 -05:00
* @param uint8_t
2020-10-13 04:45:09 -04:00
*
2022-11-22 14:38:14 -05:00
* @return uint8_t
2020-10-13 04:45:09 -04:00
*/
2022-12-09 18:15:17 -05:00
uint8_t SSD1306_Send_StartAndSLAW (uint8_t address)
2022-12-07 09:53:48 -05:00
{
2022-11-21 08:31:26 -05:00
uint8_t status = INIT_STATUS; // TWI init status 0xFF
2022-12-09 18:15:17 -05:00
// TWI START
2022-11-21 08:31:26 -05:00
// -------------------------------------------------------------------------------------
2022-12-09 18:15:17 -05:00
status = TWI_MT_Start (); // start
2022-11-21 08:31:26 -05:00
if (SSD1306_SUCCESS != status) { // check status
return status; // error
2020-10-13 04:45:09 -04:00
}
2022-12-09 18:15:17 -05:00
// TWI SLAW
2022-11-21 08:31:26 -05:00
// -------------------------------------------------------------------------------------
2022-12-09 18:15:17 -05:00
status = TWI_MT_Send_SLAW (address); // start & SLAW
if (SSD1306_SUCCESS != status) { // check status
return status; // error
2022-11-21 08:31:26 -05:00
}
2020-10-13 04:45:09 -04:00
2022-11-21 08:31:26 -05:00
return SSD1306_SUCCESS; // success
2020-10-13 04:45:09 -04:00
}
/**
2022-12-09 18:15:17 -05:00
* @desc SSD1306 Send command
2020-10-13 04:45:09 -04:00
*
2022-12-09 18:15:17 -05:00
* @param uint8_t command
2020-10-13 04:45:09 -04:00
*
2022-11-22 14:38:14 -05:00
* @return uint8_t
2020-10-13 04:45:09 -04:00
*/
2022-12-09 18:15:17 -05:00
uint8_t SSD1306_Send_Command (uint8_t command)
2020-10-13 04:45:09 -04:00
{
2022-11-21 08:31:26 -05:00
uint8_t status = INIT_STATUS; // TWI init status 0xFF
2020-10-13 04:45:09 -04:00
2022-12-09 18:15:17 -05:00
// TWI send control byte
2022-11-21 08:31:26 -05:00
// -------------------------------------------------------------------------------------
2022-12-09 18:15:17 -05:00
status = TWI_MT_Send_Data (SSD1306_COMMAND); // send control byte
2022-11-21 08:31:26 -05:00
if (SSD1306_SUCCESS != status) { // check status
return status; // error
2020-10-13 04:45:09 -04:00
}
2022-12-09 18:15:17 -05:00
// TWI send command
2022-11-21 08:31:26 -05:00
// -------------------------------------------------------------------------------------
2022-12-09 18:15:17 -05:00
status = TWI_MT_Send_Data (command); // send command
2022-11-21 08:31:26 -05:00
if (SSD1306_SUCCESS != status) { // check status
return status; // error
2020-10-13 04:45:09 -04:00
}
2022-12-07 09:53:48 -05:00
2022-11-21 08:31:26 -05:00
return SSD1306_SUCCESS; // success
2020-10-13 04:45:09 -04:00
}
/**
2022-12-09 18:15:17 -05:00
* @desc SSD1306 Init
2020-10-13 04:45:09 -04:00
*
2022-12-09 18:15:17 -05:00
* @param void
2020-10-13 04:45:09 -04:00
*
2022-11-22 14:38:14 -05:00
* @return uint8_t
2020-10-13 04:45:09 -04:00
*/
2022-12-09 18:15:17 -05:00
uint8_t SSD1306_Init (void)
2020-10-13 04:45:09 -04:00
{
2022-12-09 18:15:17 -05:00
const uint8_t *commands = INIT_SSD1306; // variables
uint8_t no_of_commands = pgm_read_byte (commands++); // number of commands
uint8_t no_of_arguments; // number od arguments
uint8_t command; // command
2022-11-21 08:31:26 -05:00
uint8_t status = INIT_STATUS; // TWI init status 0xFF
2020-10-13 04:45:09 -04:00
2022-12-09 18:15:17 -05:00
// TWI INIT
2022-12-07 09:53:48 -05:00
// -------------------------------------------------------------------------------------
2022-12-09 18:15:17 -05:00
TWI_Init ();
// TWI START & SLAW
// -------------------------------------------------------------------------------------
status = SSD1306_Send_StartAndSLAW (SSD1306_ADDR); // start & SLAW
2022-11-21 08:31:26 -05:00
if (SSD1306_SUCCESS != status) { // check status
return status; // error
2020-10-13 04:45:09 -04:00
}
2022-12-09 18:15:17 -05:00
// SEND COMMAND & ARGUMENTS
2022-11-21 08:31:26 -05:00
// -------------------------------------------------------------------------------------
2022-12-09 18:15:17 -05:00
while (no_of_commands) { // commands loop
no_of_arguments = pgm_read_byte (commands++); // number of arguments
command = pgm_read_byte (commands++); // command
// Send commands
// -----------------------------------------------------------------------------------
status = SSD1306_Send_Command (command); // send command
if (SSD1306_SUCCESS != status) { // check status
return status; // error
}
// Send arguments
// -----------------------------------------------------------------------------------
while (no_of_arguments--) {
status = SSD1306_Send_Command (pgm_read_byte(commands++)); // send argument
if (SSD1306_SUCCESS != status) { // check status
return status; // error
}
}
no_of_commands--; // next command
2020-10-13 04:45:09 -04:00
}
2022-12-09 18:15:17 -05:00
// TWI STOP
// -------------------------------------------------------------------------------------
TWI_Stop ();
2020-10-13 04:45:09 -04:00
2022-11-21 08:31:26 -05:00
return SSD1306_SUCCESS; // success
2020-10-13 04:45:09 -04:00
}
2020-10-27 08:05:53 -04:00
/**
2022-11-22 14:38:14 -05:00
* @desc SSD1306 Normal colors
2020-10-27 08:05:53 -04:00
*
2022-11-24 16:40:17 -05:00
* @param void
2020-10-27 08:05:53 -04:00
*
2022-11-22 14:38:14 -05:00
* @return uint8_t
2020-10-27 08:05:53 -04:00
*/
2022-11-24 16:40:17 -05:00
uint8_t SSD1306_NormalScreen (void)
2020-10-27 08:05:53 -04:00
{
2022-11-21 08:31:26 -05:00
uint8_t status = INIT_STATUS; // TWI init status 0xFF
2020-10-27 08:05:53 -04:00
2022-12-07 09:53:48 -05:00
// TWI START & SLAW
2022-11-21 08:31:26 -05:00
// -------------------------------------------------------------------------------------
2022-11-24 16:40:17 -05:00
status = SSD1306_Send_StartAndSLAW (SSD1306_ADDR); // start & SLAW
2022-11-21 08:31:26 -05:00
if (SSD1306_SUCCESS != status) { // check status
return status; // error
2020-10-27 08:05:53 -04:00
}
2022-11-21 08:31:26 -05:00
// TWI send command
// -------------------------------------------------------------------------------------
status = SSD1306_Send_Command (SSD1306_DIS_NORMAL); // command 0xA6
if (SSD1306_SUCCESS != status) { // check status
return status; // error
2020-10-27 08:05:53 -04:00
}
2022-12-07 09:53:48 -05:00
// TWI STOP
// -------------------------------------------------------------------------------------
TWI_Stop ();
2020-10-27 08:05:53 -04:00
2022-11-21 08:31:26 -05:00
return SSD1306_SUCCESS; // success
2020-10-27 08:05:53 -04:00
}
/**
2022-11-22 14:38:14 -05:00
* @desc SSD1306 Inverse colors
2020-10-27 08:05:53 -04:00
*
2022-11-24 16:40:17 -05:00
* @param void
2020-10-27 08:05:53 -04:00
*
2022-11-22 14:38:14 -05:00
* @return uint8_t
2020-10-27 08:05:53 -04:00
*/
2022-11-24 16:40:17 -05:00
uint8_t SSD1306_InverseScreen (void)
2020-10-27 08:05:53 -04:00
{
2022-11-22 15:49:35 -05:00
uint8_t status = INIT_STATUS; // TWI init status 0xFF
2020-10-27 08:05:53 -04:00
2022-12-07 09:53:48 -05:00
// TWI START & SLAW
2022-11-21 08:31:26 -05:00
// -------------------------------------------------------------------------------------
2022-11-24 16:40:17 -05:00
status = SSD1306_Send_StartAndSLAW (SSD1306_ADDR); // start & SLAW
2022-11-21 08:31:26 -05:00
if (SSD1306_SUCCESS != status) { // check status
return status; // error
2020-10-27 08:05:53 -04:00
}
2022-11-21 08:31:26 -05:00
// TWI send command
// -------------------------------------------------------------------------------------
status = SSD1306_Send_Command (SSD1306_DIS_INVERSE); // command 0xA7
if (SSD1306_SUCCESS != status) { // check status
return status; // error
2020-10-27 08:05:53 -04:00
}
2022-12-07 09:53:48 -05:00
// TWI STOP
// -------------------------------------------------------------------------------------
TWI_Stop ();
2020-10-27 08:05:53 -04:00
2022-11-21 08:31:26 -05:00
return SSD1306_SUCCESS; // success
2020-10-27 08:05:53 -04:00
}
/**
2022-11-24 16:40:17 -05:00
* @desc SSD1306 Clear screen
2020-10-27 08:05:53 -04:00
*
2022-11-24 16:40:17 -05:00
* @param void
2020-10-27 08:05:53 -04:00
*
2022-11-22 14:38:14 -05:00
* @return uint8_t
2020-10-27 08:05:53 -04:00
*/
2022-11-24 16:40:17 -05:00
uint8_t SSD1306_ClearScreen (void)
2020-10-27 08:05:53 -04:00
{
2022-11-21 08:31:26 -05:00
uint8_t status = INIT_STATUS; // TWI init status 0xFF
uint16_t i = 0; // counter
2022-11-24 16:40:17 -05:00
2022-12-07 09:53:48 -05:00
// TWI START & SLAW
2022-11-21 08:31:26 -05:00
// -------------------------------------------------------------------------------------
2022-11-24 16:40:17 -05:00
status = SSD1306_Send_StartAndSLAW (SSD1306_ADDR); // start & SLAW
2022-11-21 08:31:26 -05:00
if (SSD1306_SUCCESS != status) { // check status
return status; // error
2020-10-13 04:45:09 -04:00
}
2022-11-21 08:31:26 -05:00
// TWI control byte data stream
// -------------------------------------------------------------------------------------
status = TWI_MT_Send_Data (SSD1306_DATA_STREAM); // send data 0x40
if (SSD1306_SUCCESS != status) { // check status
return status; // error
2020-10-13 04:45:09 -04:00
}
2022-11-24 16:40:17 -05:00
// send clear byte to memory lcd
2022-11-21 08:31:26 -05:00
// -------------------------------------------------------------------------------------
2022-11-22 14:38:14 -05:00
while (i < CACHE_SIZE_MEM) {
2022-11-24 16:40:17 -05:00
status = TWI_MT_Send_Data (CLEAR_COLOR); // send data 0x00
2022-11-21 08:31:26 -05:00
if (SSD1306_SUCCESS != status) { // check status
return status; // error
2020-10-13 04:45:09 -04:00
}
2022-11-24 16:40:17 -05:00
i++; // update counter
2020-10-13 04:45:09 -04:00
}
2022-12-07 09:53:48 -05:00
// TWI STOP
2022-11-21 08:31:26 -05:00
// -------------------------------------------------------------------------------------
2022-11-22 14:38:14 -05:00
TWI_Stop ();
2020-10-13 04:45:09 -04:00
2022-11-21 08:31:26 -05:00
return SSD1306_SUCCESS; // success
2020-10-13 04:45:09 -04:00
}
/**
2022-11-22 14:38:14 -05:00
* @desc SSD1306 Set position
2020-10-13 04:45:09 -04:00
*
2022-11-21 08:31:26 -05:00
* @param uint8_t column -> 0 ... 127
2022-12-08 16:35:51 -05:00
* @param uint8_t column -> 0 ... 127
2022-12-09 18:15:17 -05:00
* @param uint8_t page -> 0 ... 3 or 7
* @param uint8_t page -> 0 ... 3 or 7
2020-10-13 04:45:09 -04:00
*
* @return void
*/
2022-12-08 16:35:51 -05:00
uint8_t SSD1306_SetWindow (uint8_t x1, uint8_t x2, uint8_t y1, uint8_t y2)
2020-10-13 04:45:09 -04:00
{
2022-11-24 16:40:17 -05:00
uint8_t status = INIT_STATUS; // TWI init status 0xFF
2022-12-07 09:53:48 -05:00
// TWI START & SLAW
2022-11-24 16:40:17 -05:00
// -------------------------------------------------------------------------------------
status = SSD1306_Send_StartAndSLAW (SSD1306_ADDR); // start & SLAW
if (SSD1306_SUCCESS != status) { // check status
return status; // error
}
// COLUMN
2022-12-07 09:53:48 -05:00
// -------------------------------------------------------------------------------------
2022-11-24 16:40:17 -05:00
status = SSD1306_Send_Command (SSD1306_SET_COLUMN_ADDR); // 0x21
if (SSD1306_SUCCESS != status) { // check status
return status; // error
}
2022-12-08 16:35:51 -05:00
status = SSD1306_Send_Command (x1); // start COLUMN
2022-11-24 16:40:17 -05:00
if (SSD1306_SUCCESS != status) { // check status
return status; // error
}
2022-12-08 16:35:51 -05:00
status = SSD1306_Send_Command (x2); // end COLUMN
2022-11-24 16:40:17 -05:00
if (SSD1306_SUCCESS != status) { // check status
return status; // error
}
2022-12-08 16:35:51 -05:00
_indexCol = x1; // update column index
2022-11-24 16:40:17 -05:00
// PAGE
// -------------------------------------------------------------------------------------
status = SSD1306_Send_Command (SSD1306_SET_PAGE_ADDR); // 0x22
if (SSD1306_SUCCESS != status) { // check status
return status; // error
}
2022-12-08 16:35:51 -05:00
status = SSD1306_Send_Command (y1); // start PAGE
2022-11-24 16:40:17 -05:00
if (SSD1306_SUCCESS != status) { // check status
return status; // error
}
2022-12-08 16:35:51 -05:00
status = SSD1306_Send_Command (y2); // end PAGE
2022-11-24 16:40:17 -05:00
if (SSD1306_SUCCESS != status) { // check status
return status; // error
}
2022-12-08 16:35:51 -05:00
_indexPage = y1; // update column index
2022-12-07 09:53:48 -05:00
// TWI STOP
2022-11-24 16:40:17 -05:00
// -------------------------------------------------------------------------------------
TWI_Stop ();
return SSD1306_SUCCESS; // success
2020-10-27 08:05:53 -04:00
}
2022-12-08 16:35:51 -05:00
/**
* @desc SSD1306 Set position
*
* @param uint8_t column -> 0 ... 127
* @param uint8_t page -> 0 ... 7
*
* @return void
*/
uint8_t SSD1306_SetPosition (uint8_t x, uint8_t y)
{
uint8_t status = INIT_STATUS; // TWI init status 0xFF
uint8_t x_end = END_COLUMN_ADDR;
uint8_t y_end = END_PAGE_ADDR;
status = SSD1306_SetWindow (x, x_end, y, y_end); // end COLUMN
if (SSD1306_SUCCESS != status) { // check status
return status; // error
}
return SSD1306_SUCCESS; // success
}
2020-10-27 08:05:53 -04:00
/**
2022-11-24 16:40:17 -05:00
* @desc SSD1306 Update text poisition - this ensure that character will not be divided
* at the end of row, the whole character will be depicted on the new row
2020-10-27 08:05:53 -04:00
*
2022-12-08 10:12:12 -05:00
* @param uint8_t column
* @param uint8_t page
2020-10-27 08:05:53 -04:00
*
2022-11-22 14:38:14 -05:00
* @return uint8_t
2020-10-27 08:05:53 -04:00
*/
2022-12-08 16:35:51 -05:00
uint8_t SSD1306_UpdatePosition (uint8_t x, uint8_t p)
2020-10-27 08:05:53 -04:00
{
2022-11-24 16:40:17 -05:00
uint8_t status = INIT_STATUS; // TWI init status 0xFF
2020-10-13 04:45:09 -04:00
// check position
2022-11-24 16:40:17 -05:00
// -------------------------------------------------------------------------------------
if (x > END_COLUMN_ADDR) {
// last page not reached
// -----------------------------------------------------------------------------------
2022-12-08 10:12:12 -05:00
if (p < END_PAGE_ADDR) {
2022-11-24 16:40:17 -05:00
_indexCol = 0; // update column
_indexPage = _indexPage + 1; // update page
status = SSD1306_SetPosition (_indexCol, _indexPage); // update position
if (SSD1306_SUCCESS != status) { // check status
return status; // error
}
}
// last page reached
// -----------------------------------------------------------------------------------
2022-12-08 10:12:12 -05:00
if (p >= END_PAGE_ADDR) {
2022-12-07 09:53:48 -05:00
return SSD1306_ERROR; // return out of range
2022-11-22 14:38:14 -05:00
}
2020-10-27 08:05:53 -04:00
}
2022-12-07 09:53:48 -05:00
2022-11-21 08:31:26 -05:00
return SSD1306_SUCCESS; // success
2020-10-27 08:05:53 -04:00
}
2022-12-08 10:12:12 -05:00
/**
* @desc SSD1306 Draw character
*
* @param char character
* @param enum font
*
* @return uint8_t
*/
uint8_t SSD1306_DrawChar (char ch, enum E_Font font)
{
uint8_t byte;
uint8_t status = INIT_STATUS; // TWI init status 0xFF
2022-12-09 18:15:17 -05:00
uint8_t i = 0; // counter
2022-12-08 16:35:51 -05:00
uint8_t next_x;
uint8_t next_p;
uint8_t mask = 0x00;
2022-12-08 10:12:12 -05:00
// NORMAL FONT
// -------------------------------------------------------------------------------------
2022-12-08 16:35:51 -05:00
if ((font & 0x0f) == NORMAL) {
2022-12-09 18:15:17 -05:00
if (font & 0xf0) { // underline?
mask = 0x80; // set underline mask
2022-12-08 16:35:51 -05:00
}
2022-12-09 18:15:17 -05:00
next_x = _indexCol + CHARS_COLS_LENGTH; // next column
next_p = _indexPage; // next page
2022-12-08 16:35:51 -05:00
// UPDATE / CHECK TEXT POSITION
// -----------------------------------------------------------------------------------
status = SSD1306_UpdatePosition (next_x, next_p);
if (SSD1306_SUCCESS != status) { // check status
return status; // error
}
// TWI START & SLAW
// -----------------------------------------------------------------------------------
status = SSD1306_Send_StartAndSLAW (SSD1306_ADDR); // start & SLAW
if (SSD1306_SUCCESS != status) { // check status
return status; // error
}
// TWI control byte data stream
// -----------------------------------------------------------------------------------
status = TWI_MT_Send_Data (SSD1306_DATA_STREAM); // send data 0x40
if (SSD1306_SUCCESS != status) { // check status
return status; // error
}
2022-12-08 10:12:12 -05:00
while (i < CHARS_COLS_LENGTH) {
2022-12-09 18:15:17 -05:00
byte = pgm_read_byte (&FONTS[ch-32][i++]);
2022-12-08 16:35:51 -05:00
status = TWI_MT_Send_Data (byte | mask); // send data col
2022-12-08 10:12:12 -05:00
if (SSD1306_SUCCESS != status) { // check status
return status; // error
}
_indexCol++; // update global col
}
// BOLD FONT
// -------------------------------------------------------------------------------------
2022-12-08 16:35:51 -05:00
} else if ((font & 0x0f) == BOLD) {
2022-12-09 18:15:17 -05:00
if (font & 0xf0) { // underline?
mask = 0x80; // set underline mask
2022-12-08 10:12:12 -05:00
}
2022-12-08 16:35:51 -05:00
2022-12-09 18:15:17 -05:00
next_x = _indexCol + (CHARS_COLS_LENGTH << 1); // next column
next_p = _indexPage; // next page
2022-12-08 16:35:51 -05:00
// UPDATE / CHECK TEXT POSITION
// -----------------------------------------------------------------------------------
status = SSD1306_UpdatePosition (next_x, next_p);
if (SSD1306_SUCCESS != status) { // check status
return status; // error
}
// TWI START & SLAW
// -----------------------------------------------------------------------------------
status = SSD1306_Send_StartAndSLAW (SSD1306_ADDR); // start & SLAW
if (SSD1306_SUCCESS != status) { // check status
return status; // error
}
// TWI control byte data stream
// -----------------------------------------------------------------------------------
status = TWI_MT_Send_Data (SSD1306_DATA_STREAM); // send data 0x40
if (SSD1306_SUCCESS != status) { // check status
return status; // error
}
2022-12-08 10:12:12 -05:00
while (i < CHARS_COLS_LENGTH) {
2022-12-09 18:15:17 -05:00
byte = pgm_read_byte (&FONTS[ch-32][i++]);
status = TWI_MT_Send_Data (byte | mask); // send data 1st col
if (SSD1306_SUCCESS != status) { // check status
return status; // error
}
status = TWI_MT_Send_Data (byte | mask); // send data 2nd col
2022-12-08 10:12:12 -05:00
if (SSD1306_SUCCESS != status) { // check status
return status; // error
}
2022-12-08 16:35:51 -05:00
_indexCol = _indexCol + 2; // update global col
2022-12-08 10:12:12 -05:00
}
2022-12-08 16:35:51 -05:00
}
2022-12-08 10:12:12 -05:00
// send empty column to memory lcd
// -------------------------------------------------------------------------------------
2022-12-08 16:35:51 -05:00
status = TWI_MT_Send_Data (mask); // ONE empty column
2022-12-08 10:12:12 -05:00
if (SSD1306_SUCCESS != status) { // check status
return status; // error
}
_indexCol++; // update global col
// TWI STOP
// -------------------------------------------------------------------------------------
TWI_Stop ();
return SSD1306_SUCCESS; // success
}
2020-10-13 04:45:09 -04:00
/**
* @desc SSD1306 Draw String
*
2022-11-22 14:38:14 -05:00
* @param char * string
2020-10-13 04:45:09 -04:00
*
2022-11-22 14:51:50 -05:00
* @return uint8_t
2020-10-13 04:45:09 -04:00
*/
2022-12-08 16:35:51 -05:00
uint8_t SSD1306_DrawString (char *str, enum E_Font font)
2020-10-13 04:45:09 -04:00
{
2022-11-24 16:40:17 -05:00
uint8_t i = 0; // char counter
// send characters of string
2022-12-07 09:53:48 -05:00
// -------------------------------------------------------------------------------------
2020-10-13 04:45:09 -04:00
while (str[i] != '\0') {
2022-12-08 16:35:51 -05:00
SSD1306_DrawChar (str[i++], font); // send char
2020-10-13 04:45:09 -04:00
}
2022-11-24 16:40:17 -05:00
return SSD1306_SUCCESS; // success
2020-10-13 04:45:09 -04:00
}