ssd1306.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * ssd1306.h
  3. *
  4. * Created on: Jan 12, 2021
  5. * Author: David Original work by Jose (PTDreamer), 2017
  6. */
  7. #ifndef GRAPHICS_SSD1306_H_
  8. #define GRAPHICS_SSD1306_H_
  9. #include "u8g2.h"
  10. #include "main.h"
  11. #include "widgets.h"
  12. //For checking if SPI DMA is active. Check before drawing the buffer.
  13. typedef enum {
  14. oled_idle=0,
  15. oled_busy=1,
  16. } oledStatus_t;
  17. typedef enum{
  18. error_NMI,
  19. error_HARDFAULT,
  20. error_MEMMANAGE,
  21. error_BUSFAULT,
  22. error_USAGEFAULT,
  23. error_RUNAWAY25,
  24. error_RUNAWAY50,
  25. error_RUNAWAY75,
  26. error_RUNAWAY100,
  27. error_RUNAWAY500,
  28. error_RUNAWAY_UNKNOWN,
  29. }FatalErrors;
  30. #define OledWidth 128
  31. #define OledHeight 64
  32. // buffer needs to be aligned to 32bit(4byte) boundary, as FillBuffer() uses 32bit transfer for increased speed
  33. typedef struct{
  34. __attribute__((aligned(4))) uint8_t buffer[128*8]; // 128x64 1BPP OLED
  35. uint8_t *ptr;
  36. volatile uint8_t status;
  37. volatile uint8_t row;
  38. volatile uint8_t use_sw;
  39. #if defined OLED_SPI && defined OLED_DEVICE
  40. SPI_HandleTypeDef *device;
  41. #elif defined OLED_I2C && defined OLED_DEVICE
  42. I2C_HandleTypeDef *device;
  43. #endif
  44. DMA_HandleTypeDef *fillDMA;
  45. }oled_t;
  46. extern oled_t oled;
  47. enum { fill_soft, fill_dma };
  48. #define Oled_Set_SCL() SW_SCL_GPIO_Port->BSRR = (uint32_t)SW_SCL_Pin
  49. #define Oled_Clear_SCL() SW_SCL_GPIO_Port->BRR = (uint32_t)SW_SCL_Pin
  50. #define Oled_Set_SDA() SW_SDA_GPIO_Port->BSRR = (uint32_t)SW_SDA_Pin
  51. #define Oled_Clear_SDA() SW_SDA_GPIO_Port->BRR = (uint32_t)SW_SDA_Pin
  52. #ifdef USE_CS
  53. #define Oled_Set_CS() OLED_CS_GPIO_Port->BSRR = (uint32_t)OLED_CS_Pin
  54. #define Oled_Clear_CS() OLED_CS_GPIO_Port->BRR = (uint32_t)OLED_CS_Pin
  55. #endif
  56. #ifdef USE_DC
  57. #define Oled_Set_DC() OLED_DC_GPIO_Port->BSRR = (uint32_t)OLED_DC_Pin
  58. #define Oled_Clear_DC() OLED_DC_GPIO_Port->BRR = (uint32_t)OLED_DC_Pin
  59. #endif
  60. #ifdef USE_RST
  61. #define Oled_Set_RES() OLED_RST_GPIO_Port->BSRR = (uint32_t)OLED_RST_Pin
  62. #define Oled_Clear_RES() OLED_RST_GPIO_Port->BRR = (uint32_t)OLED_RST_Pin
  63. #endif
  64. #define BLACK 0
  65. #define WHITE 1
  66. #define XOR 2
  67. #if !defined OLED_SPI && !defined OLED_I2C
  68. #error "No display configured in board.h!"
  69. #elif defined OLED_I2C && !defined OLED_ADDRESS
  70. #error "No display I2C address configured in board.h!"
  71. #endif
  72. #if defined OLED_SPI && defined OLED_DEVICE
  73. void ssd1306_init(SPI_HandleTypeDef *device,DMA_HandleTypeDef *dma);
  74. void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *device);
  75. #elif defined OLED_SPI
  76. void Enable_Soft_SPI(void);
  77. void ssd1306_init(DMA_HandleTypeDef *dma);
  78. void spi_send(uint8_t* bf, uint16_t count);
  79. #endif
  80. #if !defined OLED_DEVICE || (defined OLED_DEVICE && defined I2C_TRY_HW)
  81. void enable_soft_Oled(void);
  82. void disable_soft_Oled(void);
  83. #endif
  84. #if defined OLED_I2C
  85. #define i2cData 0x40
  86. #define i2cCmd 0x00
  87. #if !defined OLED_DEVICE || (defined OLED_DEVICE && defined I2C_TRY_HW)
  88. void i2cStart(void);
  89. void i2cStop(void);
  90. void i2cBegin(uint8_t mode);
  91. void i2cSend(uint8_t* bf, uint16_t count, uint8_t mode);
  92. void i2cDelay(void);
  93. #endif
  94. #if defined OLED_DEVICE
  95. void ssd1306_init(I2C_HandleTypeDef *device,DMA_HandleTypeDef *dma);
  96. void i2c_workaround(void);
  97. void HAL_I2C_MemTxCpltCallback(I2C_HandleTypeDef *device);
  98. #else
  99. void ssd1306_init(DMA_HandleTypeDef *dma);
  100. #endif
  101. #endif
  102. void FatalError(uint8_t type);
  103. void write_data(uint8_t* data, uint16_t count);
  104. void write_cmd(uint8_t cmd);
  105. void pset(uint8_t x, uint8_t y, bool c);
  106. void update_display(void);
  107. void display_abort(void);
  108. void update_display_ErrorHandler(void);
  109. void setContrast(uint8_t value);
  110. void setOledRow(uint8_t row);
  111. uint8_t getContrast();
  112. void FillBuffer(bool color, bool mode);
  113. void putStrAligned(char* str, uint8_t y, AlignType align);
  114. void Reset_onError(void);
  115. #endif /* GRAPHICS_SSD1306_H_ */