ssd1306.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. #ifndef __SSD1306_H__
  8. #define __SSD1306_H__
  9. #include <stddef.h>
  10. #include <_ansi.h>
  11. _BEGIN_STD_C
  12. #include "../ssd1306/ssd1306_conf.h"
  13. #if defined(STM32F0)
  14. #include "stm32f0xx_hal.h"
  15. #elif defined(STM32F1)
  16. #include "stm32f1xx_hal.h"
  17. #elif defined(STM32F4)
  18. #include "stm32f4xx_hal.h"
  19. #include "stm32f4xx_hal_gpio.h"
  20. #elif defined(STM32L0)
  21. #include "stm32l0xx_hal.h"
  22. #elif defined(STM32L1)
  23. #include "stm32l1xx_hal.h"
  24. #elif defined(STM32L4)
  25. #include "stm32l4xx_hal.h"
  26. #elif defined(STM32F3)
  27. #include "stm32f3xx_hal.h"
  28. #elif defined(STM32H7)
  29. #include "stm32h7xx_hal.h"
  30. #elif defined(STM32F7)
  31. #include "stm32f7xx_hal.h"
  32. #elif defined(STM32G0)
  33. #include "stm32g0xx_hal.h"
  34. #else
  35. #error "SSD1306 library was tested only on STM32F0, STM32F1, STM32F3, STM32F4, STM32F7, STM32L0, STM32L1, STM32L4, STM32H7, STM32G0 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!"
  36. #endif
  37. #include "../ssd1306/ssd1306_fonts.h"
  38. /* vvv I2C config vvv */
  39. #ifndef SSD1306_I2C_PORT
  40. #define SSD1306_I2C_PORT hi2c1
  41. #endif
  42. #ifndef SSD1306_I2C_ADDR
  43. #define SSD1306_I2C_ADDR (0x3C << 1)
  44. #endif
  45. /* ^^^ I2C config ^^^ */
  46. /* vvv SPI config vvv */
  47. #ifndef SSD1306_SPI_PORT
  48. #define SSD1306_SPI_PORT hspi2
  49. #endif
  50. #ifndef SSD1306_CS_Port
  51. #define SSD1306_CS_Port GPIOB
  52. #endif
  53. #ifndef SSD1306_CS_Pin
  54. #define SSD1306_CS_Pin GPIO_PIN_12
  55. #endif
  56. #ifndef SSD1306_DC_Port
  57. #define SSD1306_DC_Port GPIOB
  58. #endif
  59. #ifndef SSD1306_DC_Pin
  60. #define SSD1306_DC_Pin GPIO_PIN_14
  61. #endif
  62. #ifndef SSD1306_Reset_Port
  63. #define SSD1306_Reset_Port GPIOA
  64. #endif
  65. #ifndef SSD1306_Reset_Pin
  66. #define SSD1306_Reset_Pin GPIO_PIN_8
  67. #endif
  68. /* ^^^ SPI config ^^^ */
  69. #if defined(SSD1306_USE_I2C)
  70. extern I2C_HandleTypeDef SSD1306_I2C_PORT;
  71. #elif defined(SSD1306_USE_SPI)
  72. extern SPI_HandleTypeDef SSD1306_SPI_PORT;
  73. #else
  74. #error "You should define SSD1306_USE_SPI or SSD1306_USE_I2C macro!"
  75. #endif
  76. // SSD1306 OLED height in pixels
  77. #ifndef SSD1306_HEIGHT
  78. #define SSD1306_HEIGHT 32
  79. #endif
  80. // SSD1306 width in pixels
  81. #ifndef SSD1306_WIDTH
  82. #define SSD1306_WIDTH 128
  83. #endif
  84. #ifndef SSD1306_BUFFER_SIZE
  85. #define SSD1306_BUFFER_SIZE SSD1306_WIDTH * SSD1306_HEIGHT / 8
  86. #endif
  87. // Enumeration for screen colors
  88. typedef enum {
  89. Black = 0x00, // Black color, no pixel
  90. White = 0x01 // Pixel is set. Color depends on OLED
  91. } SSD1306_COLOR;
  92. typedef enum {
  93. SSD1306_OK = 0x00,
  94. SSD1306_ERR = 0x01 // Generic error.
  95. } SSD1306_Error_t;
  96. // Struct to store transformations
  97. typedef struct {
  98. uint16_t CurrentX;
  99. uint16_t CurrentY;
  100. uint8_t Inverted;
  101. uint8_t Initialized;
  102. uint8_t DisplayOn;
  103. } SSD1306_t;
  104. typedef struct {
  105. uint8_t x;
  106. uint8_t y;
  107. } SSD1306_VERTEX;
  108. // Procedure definitions
  109. void ssd1306_Init(void);
  110. void ssd1306_Fill(SSD1306_COLOR color);
  111. void ssd1306_UpdateScreen(void);
  112. void ssd1306_DrawPixel(uint8_t x, uint8_t y, SSD1306_COLOR color);
  113. char ssd1306_WriteChar(char ch, FontDef Font, SSD1306_COLOR color);
  114. char ssd1306_WriteString(char* str, FontDef Font, SSD1306_COLOR color);
  115. void ssd1306_SetCursor(uint8_t x, uint8_t y);
  116. void ssd1306_Line(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2, SSD1306_COLOR color);
  117. void ssd1306_DrawArc(uint8_t x, uint8_t y, uint8_t radius, uint16_t start_angle, uint16_t sweep, SSD1306_COLOR color);
  118. void ssd1306_DrawCircle(uint8_t par_x, uint8_t par_y, uint8_t par_r, SSD1306_COLOR color);
  119. void ssd1306_Polyline(const SSD1306_VERTEX *par_vertex, uint16_t par_size, SSD1306_COLOR color);
  120. void ssd1306_DrawRectangle(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2, SSD1306_COLOR color);
  121. /**
  122. * @brief Sets the contrast of the display.
  123. * @param[in] value contrast to set.
  124. * @note Contrast increases as the value increases.
  125. * @note RESET = 7Fh.
  126. */
  127. void ssd1306_SetContrast(const uint8_t value);
  128. /**
  129. * @brief Set Display ON/OFF.
  130. * @param[in] on 0 for OFF, any for ON.
  131. */
  132. void ssd1306_SetDisplayOn(const uint8_t on);
  133. /**
  134. * @brief Reads DisplayOn state.
  135. * @return 0: OFF.
  136. * 1: ON.
  137. */
  138. uint8_t ssd1306_GetDisplayOn();
  139. // Low-level procedures
  140. void ssd1306_Reset(void);
  141. void ssd1306_WriteCommand(uint8_t byte);
  142. void ssd1306_WriteData(uint8_t* buffer, size_t buff_size);
  143. SSD1306_Error_t ssd1306_FillBuffer(uint8_t* buf, uint32_t len);
  144. _END_STD_C
  145. #endif // __SSD1306_H__