DIY Logging Volt/Ampmeter
ssd1306.h
Go to the documentation of this file.
1 /**
2  * This Library was originally written by Olivier Van den Eede (4ilo) in 2016.
3  * Some refactoring was done and SPI support was added by Aleksander Alekseev (afiskon) in 2018.
4  *
5  * https://github.com/afiskon/stm32-ssd1306
6  */
7 
8 #ifndef __SSD1306_H__
9 #define __SSD1306_H__
10 
11 #include <stddef.h>
12 #include <_ansi.h>
13 
14 _BEGIN_STD_C
15 
16 #include "ssd1306_conf.h"
17 
18 #if defined(STM32F0)
19 #include "stm32f0xx_hal.h"
20 #elif defined(STM32F1)
21 #include "stm32f1xx_hal.h"
22 #elif defined(STM32F4)
23 #include "stm32f4xx_hal.h"
24 #include "stm32f4xx_hal_gpio.h"
25 #elif defined(STM32L0)
26 #include "stm32l0xx_hal.h"
27 #elif defined(STM32L4)
28 #include "stm32l4xx_hal.h"
29 #elif defined(STM32F3)
30 #include "stm32f3xx_hal.h"
31 #elif defined(STM32H7)
32 #include "stm32h7xx_hal.h"
33 #elif defined(STM32F7)
34 #include "stm32f7xx_hal.h"
35 #else
36 #error "SSD1306 library was tested only on STM32F0, STM32F1, STM32F3, STM32F4, STM32F7, STM32L0, STM32L4, STM32H7 MCU families. Please modify ssd1306.h if you know what you are doing. Also please send a pull request if it turns out the library works on other MCU's as well!"
37 #endif
38 
39 #include "ssd1306_fonts.h"
40 
41 /* vvv I2C config vvv */
42 
43 #ifndef SSD1306_I2C_PORT
44 #define SSD1306_I2C_PORT hi2c1
45 #endif
46 
47 #ifndef SSD1306_I2C_ADDR
48 #define SSD1306_I2C_ADDR (0x3C << 1)
49 #endif
50 
51 /* ^^^ I2C config ^^^ */
52 
53 /* vvv SPI config vvv */
54 
55 #ifndef SSD1306_SPI_PORT
56 #define SSD1306_SPI_PORT hspi2
57 #endif
58 
59 #ifndef SSD1306_CS_Port
60 #define SSD1306_CS_Port GPIOB
61 #endif
62 #ifndef SSD1306_CS_Pin
63 #define SSD1306_CS_Pin GPIO_PIN_12
64 #endif
65 
66 #ifndef SSD1306_DC_Port
67 #define SSD1306_DC_Port GPIOB
68 #endif
69 #ifndef SSD1306_DC_Pin
70 #define SSD1306_DC_Pin GPIO_PIN_14
71 #endif
72 
73 #ifndef SSD1306_Reset_Port
74 #define SSD1306_Reset_Port GPIOA
75 #endif
76 #ifndef SSD1306_Reset_Pin
77 #define SSD1306_Reset_Pin GPIO_PIN_8
78 #endif
79 
80 /* ^^^ SPI config ^^^ */
81 
82 #if defined(SSD1306_USE_I2C)
84 #elif defined(SSD1306_USE_SPI)
85 extern SPI_HandleTypeDef SSD1306_SPI_PORT;
86 #else
87 #error "You should define SSD1306_USE_SPI or SSD1306_USE_I2C macro!"
88 #endif
89 
90 /* SSD1306 OLED height in pixels */
91 #ifndef SSD1306_HEIGHT
92 #define SSD1306_HEIGHT 64
93 #endif
94 
95 /* SSD1306 width in pixels */
96 #ifndef SSD1306_WIDTH
97 #define SSD1306_WIDTH 128
98 #endif
99 
100 #ifndef SSD1306_BUFFER_SIZE
101 #define SSD1306_BUFFER_SIZE SSD1306_WIDTH * SSD1306_HEIGHT / 8
102 #endif
103 
104 /* Enumeration for screen colors */
105 typedef enum {
106  Black = 0x00, /* Black color, no pixel */
107  White = 0x01 /* Pixel is set. Color depends on OLED */
108 } SSD1306_COLOR;
109 
110 typedef enum {
111  SSD1306_OK = 0x00,
112  SSD1306_ERR = 0x01 /* Generic error. */
114 
115 /* Struct to store transformations */
116 typedef struct {
117  uint16_t CurrentX;
118  uint16_t CurrentY;
119  uint8_t Inverted;
120  uint8_t Initialized;
121  uint8_t DisplayOn;
122 } SSD1306_t;
123 typedef struct {
124  uint8_t x;
125  uint8_t y;
127 
128 /* Procedure definitions */
129 void ssd1306_SelectDisplay(uint8_t display); /* 0 = left = 0x78 1 = right = 0x7A */
130 void ssd1306_Init(void);
131 void ssd1306_Fill(SSD1306_COLOR color);
132 void ssd1306_UpdateScreen(void);
133 void ssd1306_DrawPixel(uint8_t x, uint8_t y, SSD1306_COLOR color);
134 char ssd1306_WriteChar(char ch, FontDef Font, SSD1306_COLOR color);
135 char ssd1306_WriteString(char* str, FontDef Font, SSD1306_COLOR color);
136 void ssd1306_SetCursor(uint8_t x, uint8_t y);
137 void ssd1306_Line(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2, SSD1306_COLOR color);
138 void ssd1306_DrawArc(uint8_t x, uint8_t y, uint8_t radius, uint16_t start_angle, uint16_t sweep, SSD1306_COLOR color);
139 void ssd1306_DrawCircle(uint8_t par_x, uint8_t par_y, uint8_t par_r, SSD1306_COLOR color);
140 void ssd1306_Polyline(const SSD1306_VERTEX *par_vertex, uint16_t par_size, SSD1306_COLOR color);
141 void ssd1306_DrawRectangle(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2, SSD1306_COLOR color);
142 /**
143  * @brief Sets the contrast of the display.
144  * @param[in] value contrast to set.
145  * @note Contrast increases as the value increases.
146  * @note RESET = 7Fh.
147  */
148 void ssd1306_SetContrast(const uint8_t value);
149 /**
150  * @brief Set Display ON/OFF.
151  * @param[in] on 0 for OFF, any for ON.
152  */
153 void ssd1306_SetDisplayOn(const uint8_t on);
154 /**
155  * @brief Reads DisplayOn state.
156  * @return 0: OFF.
157  * 1: ON.
158  */
159 uint8_t ssd1306_GetDisplayOn();
160 
161 /* Low-level procedures */
162 void ssd1306_Reset(void);
163 void ssd1306_WriteCommand(uint8_t byte);
164 void ssd1306_WriteData(uint8_t* buffer, size_t buff_size);
165 SSD1306_Error_t ssd1306_FillBuffer(uint8_t* buf, uint32_t len);
166 
167 _END_STD_C
168 
169 #endif /* __SSD1306_H__ */
ssd1306_SelectDisplay
void ssd1306_SelectDisplay(uint8_t display)
Definition: ssd1306.c:19
ssd1306_DrawPixel
void ssd1306_DrawPixel(uint8_t x, uint8_t y, SSD1306_COLOR color)
Definition: ssd1306.c:239
ssd1306_Line
void ssd1306_Line(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2, SSD1306_COLOR color)
Definition: ssd1306.c:358
ssd1306_Fill
void ssd1306_Fill(SSD1306_COLOR color)
Definition: ssd1306.c:199
ssd1306_fonts.h
SSD1306_COLOR
SSD1306_COLOR
Definition: ssd1306.h:105
stm32f1xx_hal.h
This file contains all the functions prototypes for the HAL module driver.
Black
@ Black
Definition: ssd1306.h:106
ssd1306_DrawRectangle
void ssd1306_DrawRectangle(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2, SSD1306_COLOR color)
Definition: ssd1306.c:513
ssd1306_WriteData
void ssd1306_WriteData(uint8_t *buffer, size_t buff_size)
Definition: ssd1306.c:36
FontDef
Definition: ssd1306_fonts.h:8
ssd1306_Polyline
void ssd1306_Polyline(const SSD1306_VERTEX *par_vertex, uint16_t par_size, SSD1306_COLOR color)
Definition: ssd1306.c:394
ssd1306_WriteString
char ssd1306_WriteString(char *str, FontDef Font, SSD1306_COLOR color)
Definition: ssd1306.c:327
SSD1306_t::CurrentY
uint16_t CurrentY
Definition: ssd1306.h:118
ssd1306_SetCursor
void ssd1306_SetCursor(uint8_t x, uint8_t y)
Definition: ssd1306.c:344
ssd1306_UpdateScreen
void ssd1306_UpdateScreen(void)
Definition: ssd1306.c:220
ssd1306_conf.h
ssd1306_FillBuffer
SSD1306_Error_t ssd1306_FillBuffer(uint8_t *buf, uint32_t len)
Definition: ssd1306.c:83
SSD1306_VERTEX::y
uint8_t y
Definition: ssd1306.h:125
ssd1306_DrawCircle
void ssd1306_DrawCircle(uint8_t par_x, uint8_t par_y, uint8_t par_r, SSD1306_COLOR color)
Definition: ssd1306.c:468
SSD1306_VERTEX
Definition: ssd1306.h:123
SSD1306_t
Definition: ssd1306.h:116
SSD1306_t::Initialized
uint8_t Initialized
Definition: ssd1306.h:120
SSD1306_Error_t
SSD1306_Error_t
Definition: ssd1306.h:110
ssd1306_Init
void ssd1306_Init(void)
Definition: ssd1306.c:94
SSD1306_SPI_PORT
#define SSD1306_SPI_PORT
Definition: ssd1306.h:56
SSD1306_t::Inverted
uint8_t Inverted
Definition: ssd1306.h:119
SSD1306_I2C_PORT
#define SSD1306_I2C_PORT
Definition: ssd1306_conf.h:24
ssd1306_SetContrast
void ssd1306_SetContrast(const uint8_t value)
Sets the contrast of the display.
Definition: ssd1306.c:522
SSD1306_ERR
@ SSD1306_ERR
Definition: ssd1306.h:112
ssd1306_WriteCommand
void ssd1306_WriteCommand(uint8_t byte)
Definition: ssd1306.c:31
ssd1306_DrawArc
void ssd1306_DrawArc(uint8_t x, uint8_t y, uint8_t radius, uint16_t start_angle, uint16_t sweep, SSD1306_COLOR color)
Definition: ssd1306.c:429
ssd1306_Reset
void ssd1306_Reset(void)
Definition: ssd1306.c:15
SSD1306_t::CurrentX
uint16_t CurrentX
Definition: ssd1306.h:117
I2C_HandleTypeDef
Definition: stm32f1xx_hal_i2c.h:188
SSD1306_OK
@ SSD1306_OK
Definition: ssd1306.h:111
ssd1306_WriteChar
char ssd1306_WriteChar(char ch, FontDef Font, SSD1306_COLOR color)
Definition: ssd1306.c:274
SSD1306_t::DisplayOn
uint8_t DisplayOn
Definition: ssd1306.h:121
ssd1306_SetDisplayOn
void ssd1306_SetDisplayOn(const uint8_t on)
Set Display ON/OFF.
Definition: ssd1306.c:528
ssd1306_GetDisplayOn
uint8_t ssd1306_GetDisplayOn()
Reads DisplayOn state.
Definition: ssd1306.c:542
White
@ White
Definition: ssd1306.h:107
SSD1306_VERTEX::x
uint8_t x
Definition: ssd1306.h:124