diff --git a/main.c b/main.c --- a/main.c +++ b/main.c @@ -75,13 +75,13 @@ int main(void) // Init OLED over SPI ssd1306_Init(); - ssd1306_block_write(); + ssd1306_clearscreen(); // Startup screen ssd1306_DrawString("therm v0.1", 1, 40); ssd1306_DrawString("protofusion.org/therm", 3, 0); delay(1500); - ssd1306_block_write(); + ssd1306_clearscreen(); // Main loop while(1) @@ -466,7 +466,7 @@ void machine() if(last_state != state) { // Clear screen on state change - ssd1306_block_write(); + ssd1306_clearscreen(); } } diff --git a/ssd1306.c b/ssd1306.c --- a/ssd1306.c +++ b/ssd1306.c @@ -1,54 +1,8 @@ -/******************************************************************************* -* File Name : ssd1306.c -* Author : lxyppc -* Version : V1.0 -* Date : 10-01-21 -* Description : ssd1306 operations -* the SSH1101A is compatible with ssd1306 -*******************************************************************************/ - -/* Includes ------------------------------------------------------------------*/ #include "stm32l100c_discovery.h" #include "bsp.h" #include "ssd1306.h" -/* Private typedef -----------------------------------------------------------*/ -/* Private define ------------------------------------------------------------*/ -#define SSD1306_PAGE_NUMBER 8 -#define SSD1306_COLUMN_NUMBER 128 -#define SSD1306_COLUMN_MARGIN_START 2 -#define SSD1306_COLUMN_MARGIN_END 2 -#define SSD1306_X_PIXEL 128 -#define SSD1306_Y_PIXEL 32 - -/* Private macro -------------------------------------------------------------*/ -#define ssd1306_Buffer (_SSD1306_Buffer + SSD1306_COLUMN_MARGIN_START) - -/* Private variables ---------------------------------------------------------*/ -static uint8_t _SSD1306_Buffer[SSD1306_COLUMN_NUMBER*SSD1306_PAGE_NUMBER + SSD1306_COLUMN_MARGIN_START + SSD1306_COLUMN_MARGIN_END] = {0}; -static uint8_t pageIndex = 0; -static uint8_t iS_SSD_On = 0; -static uint8_t pre_on = 0; -static uint8_t curContrast = 0xCC; -static uint8_t lastContrast = 0xCC; - -/* Private function prototypes -----------------------------------------------*/ -void WriteCommand(unsigned char command); -void WriteData(unsigned char data); -void OnPageTransferDone(void); -unsigned long ssd1306_OFF(void); -unsigned long ssd1306_ON(void); -unsigned char* ssd1306_GetBuffer() -{ - return ssd1306_Buffer; -} -/******************************************************************************* -* Function Name : WriteCommand -* Description : Write command to the ssd1306 -* Input : None -* Output : None -* Return : None -*******************************************************************************/ +// Write command to OLED void WriteCommand(unsigned char command) { SSD_A0_Low(); @@ -56,13 +10,7 @@ void WriteCommand(unsigned char command) SPI_Wait(); } -/******************************************************************************* -* Function Name : WriteData -* Description : Write data to the ssd1306 -* Input : None -* Output : None -* Return : None -*******************************************************************************/ +// Write data to OLED void WriteData(unsigned char data) { SSD_A0_High(); @@ -70,13 +18,7 @@ void WriteData(unsigned char data) SPI_Wait(); } -/******************************************************************************* -* Function Name : ssd1306_Init -* Description : Initialize the ssd1306 -* Input : None -* Output : None -* Return : None -*******************************************************************************/ +// Initialize OLED void ssd1306_Init(void) { @@ -114,395 +56,6 @@ void ssd1306_Init(void) WriteCommand(0xAF); // display on } -void ssd1306_block_write(void) -{ - // Set col start addr to 0 - WriteCommand(0x21); - WriteCommand(0x00); - // Set col end addr to width - 1 - WriteCommand(127); - - // Set page addr - WriteCommand(0x22); - WriteCommand(0x00); // start page addr - WriteCommand(4); // end page addr (height / 8) - - // Write data - uint32_t i = 0; - for(i=0;i<512;i++) - WriteData(ssd1306_Buffer[i]); -} - - -/******************************************************************************* -* Function Name : ssd1306_TurnOff -* Description : Turn off the ssd1306 controller -* Input : None -* Output : None -* Return : None -*******************************************************************************/ -unsigned long ssd1306_TurnOff(void) -{ - pre_on = 0; - return iS_SSD_On; -} - -/******************************************************************************* -* Function Name : ssd1306_TurnOn -* Description : Turn off the ssd1306 controller -* Input : None -* Output : None -* Return : None -*******************************************************************************/ -unsigned long ssd1306_TurnOn(void) -{ - pre_on = 1; - return iS_SSD_On; -} - -/******************************************************************************* -* Function Name : ssd1306_SetContrast -* Description : Set the ssd1306 contrast -* Input : None -* Output : None -* Return : None -*******************************************************************************/ -unsigned char ssd1306_SetContrast(unsigned char contrast) -{ - curContrast = contrast; - return lastContrast; -} - -/******************************************************************************* -* Function Name : ssd1306_GetContrast -* Description : Get the ssd1306 contrast -* Input : None -* Output : None -* Return : None -*******************************************************************************/ -unsigned char ssd1306_GetContrast() -{ - return lastContrast; -} - -/******************************************************************************* -* Function Name : ssd1306_OFF -* Description : Turn off the ssd1306 controller -* Input : None -* Output : None -* Return : None -*******************************************************************************/ -unsigned long ssd1306_OFF(void) -{ - if(iS_SSD_On){ -#ifdef DEBUG_UI - uint32_t i = 0; - for(i=0;iCCR &= ((uint32_t)0xFFFFFFFE); - DMA1_Channel5->CNDTR = ssd1306_COLUMN_NUMBER+SSD1306_COLUMN_MARGIN_START + SSD1306_COLUMN_MARGIN_END; - DMA1_Channel5->CMAR = (uint32_t)(ssd1306_Buffer+SSD1306_COLUMN_NUMBER*pageIndex - SSD1306_COLUMN_MARGIN_START); - DMA_SSD_1306->CCR |= ((uint32_t)0x00000001); - pageIndex++; -#else - SSD_A0_High(); - DMA_SSD_1306->CCR &= ((uint32_t)0xFFFFFFFE); - DMA_SSD_1306->CNDTR = SSD1306_COLUMN_NUMBER*SSD1306_PAGE_NUMBER;//+SSD1306_COLUMN_MARGIN_START + SSD1306_COLUMN_MARGIN_END; - DMA_SSD_1306->CMAR = (uint32_t)(ssd1306_Buffer);//+SSD1306_COLUMN_NUMBER*pageIndex); - //DMA_Cmd(DMA_SSD_1306, ENABLE); - DMA_SSD_1306->CCR |= ((uint32_t)0x00000001); -// pageIndex++; - pageIndex = SSD1306_PAGE_NUMBER; -#endif -} - -/******************************************************************************* -* Function Name : DMA1_Channel5_IRQHandler -* Description : This function handles DMA1 Channel 5 interrupt request. -* Input : None -* Output : None -* Return : None -*******************************************************************************/ -void DMA_Handler_SSD_1306(void) //DMA1_Channel5_IRQHandler(void) -{ - if(DMA_GetITStatus(DMA1_IT_TC5)){ - DMA_ClearITPendingBit(DMA1_IT_GL5); - OnPageTransferDone(); - } -} - -/******************************************************************************* -* Function Name : StartPageTransfer -* Description : Start a new page transfer -* Input : None -* Output : None -* Return : None -*******************************************************************************/ -void StartPageTransfer(void) -{ - pageIndex = 0; - if(pre_on){ - if(iS_SSD_On == 0){ - pre_on = ssd1306_ON(); - } - }else{ - if(iS_SSD_On){ - pre_on = ssd1306_OFF(); - } - } - if( curContrast != lastContrast ){ - lastContrast = curContrast; - WriteCommand(0x81); /* Set Contrast Control */ - WriteCommand(lastContrast); /* 0 ~ 255 0x1f*/ - } - - if(iS_SSD_On){ - OnPageTransferDone(); - } -} - -/******************************************************************************* - Display related functions - ******************************************************************************/ -/******************************************************************************* -* Function Name : ssd1306_DrawBlock -* Description : Draw a block with specify data to ssd1306 -* Input : None -* Output : None -* Return : None -*******************************************************************************/ -unsigned long ssd1306_DrawBlock( - Pos_t x, - Pos_t y, - Pos_t cx, - Pos_t cy, - const unsigned char* data) -{ - if(x >= SSD1306_X_PIXEL)return 0; - if(y >= SSD1306_Y_PIXEL)return 0; - if(x+cx > SSD1306_X_PIXEL ) cx = SSD1306_X_PIXEL - x ; - if(y+cy > SSD1306_Y_PIXEL ) cy = SSD1306_Y_PIXEL - y ; - unsigned char* pStart = ssd1306_Buffer + (y/8)*SSD1306_COLUMN_NUMBER + x; - unsigned char offset1 = y%8; - unsigned char offset2 = (cy+y)%8; - if(data){ - unsigned char mask1 = (1<>=8; - } - if(offset2){ - tmp |= ((((unsigned short)data[j*cx])<>=8; -// if(j == (cy/8)-1){ -// // Last line -// *(pStart + (j+1)*ssd1306_COLUMN_NUMBER + i + x) &= ~mask; -// *(pStart + (j+1)*ssd1306_COLUMN_NUMBER + i + x) |= tmp; -// } -// } -// } -// } - - return 0; -} - -/******************************************************************************* -* Function Name : ssd1306_DrawPoint -* Description : Draw a point with specify data to ssd1306 -* Input : None -* Output : None -* Return : None -*******************************************************************************/ -unsigned long ssd1306_DrawPoint( - Pos_t x, - Pos_t y, - Color_t color) -{ - if(x >= SSD1306_X_PIXEL)return 0; - if(y >= SSD1306_Y_PIXEL)return 0; - unsigned char* pStart = ssd1306_Buffer + (y/8)*SSD1306_COLUMN_NUMBER + x; - unsigned char mask = 1<<(y%8); - if(color){ - *pStart |= mask; - }else{ - *pStart &= ~mask; - } - return 0; -} - -/******************************************************************************* -* Function Name : ssd1306_ReadPoint -* Description : Read a point from ssd1306 -* Input : None -* Output : None -* Return : None -*******************************************************************************/ -unsigned long ssd1306_ReadPoint( - Pos_t x, - Pos_t y) -{ - unsigned char* pStart = ssd1306_Buffer + (y/8)*SSD1306_COLUMN_NUMBER + x; - unsigned char mask = 1<<(y%8); - return *pStart&mask ? 1 : 0; -} - -/******************************************************************************* -* Function Name : ssd1306_ClearScreen -* Description : Clear the screen contents -* Input : None -* Output : None -* Return : None -*******************************************************************************/ -void ssd1306_FillScreen(Color_t color) -{ - unsigned char mask = color ? 0xFF : 0; - unsigned long i = 0; - for(i=0;i