You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

300 lines
8.0 KiB

#include "app_oled.h"
#include "app_gpio.h"
#include "app_log.h"
#include "sw_timer.h"
#include "ssd1306.h"
#define OLED_POWER_PORT PORT_0
#define OLED_POWER_PIN PIN_4
#define OLED_POWER_ON Gpio_Output_Clear(OLED_POWER_PORT, OLED_POWER_PIN);
#define OLED_POWER_OFF Gpio_Output_Set(OLED_POWER_PORT, OLED_POWER_PIN);
#define OLED_POWER_T Gpio_Output_Toggle(OLED_POWER_PORT, OLED_POWER_PIN);
#define OLED_POWER_OFF_WAIT_TIME 500
#define OLED_POWER_ON_WAIT_TIME 100
#define OLED_ERROR_PRINT_INTERVAL 1000
#define OLED_SHOW_LOG_WAIT_TIME 1000
#define OLED_SHOW_LOG_INVERSE_WAIT_TIME 1000
#define OLED_UPDATE_TEMP_INTERVAL 100
static OLED_STEP oled_step;
static OLED_STEP oled_pre_step;
static uint32_t oled_tickCount;
static uint32_t oled_shift_index;
uint16_t oled_draw_before_temp;
static uint16_t oled_draw_temp;
static bool isDrawUpdate;
static void Oled_Process(void);
static void Oled_Next_Step(OLED_STEP nextStep);
bool Oled_Initialization(void)
{
oled_step = OLED_STEP_INIT;
oled_pre_step = OLED_STEP_INIT;
//Gpio_Set_Port_Output(OLED_POWER_PORT, OLED_POWER_PIN, GPIO_PAD_PULL_UP);
//OLED_POWER_OFF;
SW_Timer_Callback_Register(SW_TIMER_RUN_CONTINUE, 1, Oled_Process);
3 months ago
return true;
}
void Oled_Test(void)
{
OLED_POWER_T;
}
static void Oled_Process(void)
{
switch(oled_step)
{
case OLED_STEP_INIT:
Gpio_Set_Port_Output(OLED_POWER_PORT, OLED_POWER_PIN, GPIO_PAD_PULL_UP);
OLED_POWER_OFF;
oled_tickCount = millis();
Oled_Next_Step(OLED_STEP_POWER_OFF_WAIT);
break;
case OLED_STEP_POWER_OFF_WAIT:
if((millis() - oled_tickCount) >= OLED_POWER_OFF_WAIT_TIME)
{
Oled_Next_Step(OLED_STEP_POWER_ON);
}
break;
case OLED_STEP_POWER_ON:
OLED_POWER_ON;
oled_tickCount = millis();
Oled_Next_Step(OLED_STEP_POWER_ON_WAIT);
break;
case OLED_STEP_POWER_ON_WAIT:
if((millis() - oled_tickCount) >= OLED_POWER_ON_WAIT_TIME)
{
Oled_Next_Step(OLED_STEP_SET_INIT);
}
break;
case OLED_STEP_SET_INIT:
if(SSD1306_Init(SSD1306_ADDR) != SSD1306_SUCCESS)
{
Oled_Next_Step(OLED_STEP_ERROR);
}
else
{
Oled_Next_Step(OLED_STEP_NORMAL_SCREEN_SET);
}
break;
case OLED_STEP_NORMAL_SCREEN_SET:
SSD1306_NormalScreen (SSD1306_ADDR);
SSD1306_ClearScreen ();
Oled_Next_Step(OLED_STEP_DRAW_LOG);
break;
case OLED_STEP_DRAW_LOG:
SSD1306_DrawBackGround_Log();
SSD1306_UpdateScreen (SSD1306_ADDR);
oled_tickCount = millis();
Oled_Next_Step(OLED_STEP_WAIT_LOG);
break;
case OLED_STEP_WAIT_LOG:
if((millis() - oled_tickCount) >= OLED_SHOW_LOG_WAIT_TIME)
{
Oled_Next_Step(OLED_STEP_LOG_INVERSE);
}
break;
case OLED_STEP_LOG_INVERSE:
SSD1306_InverseScreen (SSD1306_ADDR);
oled_tickCount = millis();
Oled_Next_Step(OLED_STEP_WAIT_LOG_INVERSE);
break;
case OLED_STEP_WAIT_LOG_INVERSE:
if((millis() - oled_tickCount) >= OLED_SHOW_LOG_INVERSE_WAIT_TIME)
{
Oled_Next_Step(OLED_STEP_LOG_NORMAL);
}
break;
case OLED_STEP_LOG_NORMAL:
SSD1306_NormalScreen (SSD1306_ADDR);
oled_shift_index = 0;
Oled_Next_Step(OLED_STEP_LOG_LEFT_SCROL);
break;
case OLED_STEP_LOG_LEFT_SCROL:
SSD1306_DrawBackGround_LeftShift(1);
if(oled_shift_index % 2 == 0)
{
SSD1306_UpdateScreen (SSD1306_ADDR);
}
oled_shift_index++;
if(oled_shift_index >= 128)
{
Oled_Next_Step(OLED_STEP_DRAW_BACKGROUND);
}
break;
case OLED_STEP_DRAW_BACKGROUND:
SSD1306_DrawBackGround();
SSD1306_Draw_Temperature(oled_draw_temp);
SSD1306_UpdateScreen (SSD1306_ADDR);
oled_tickCount = millis();
oled_draw_temp = 0;
isDrawUpdate = true;
Oled_Next_Step(OLED_STEP_DRAW_TEMP);
break;
case OLED_STEP_DRAW_TEMP:
if(isDrawUpdate == true)
{
isDrawUpdate = false;
SSD1306_Draw_Temperature(oled_draw_temp);
SSD1306_UpdateScreen (SSD1306_ADDR);
}
#if 0
if((millis() - oled_tickCount) >= OLED_UPDATE_TEMP_INTERVAL)
{
oled_tickCount = millis();
SSD1306_Draw_Temperature(oled_draw_temp);
SSD1306_UpdateScreen (SSD1306_ADDR);
}
#endif
break;
case OLED_STEP_ERROR:
if((millis() - oled_tickCount) >= OLED_ERROR_PRINT_INTERVAL)
{
oled_tickCount = millis();
dbg_printf(LOG_LEVEL_DEBUG, "Oled Step Error %d\r\n", oled_pre_step);
}
break;
}
}
static void Oled_Next_Step(OLED_STEP nextStep)
{
oled_pre_step = oled_step;
oled_step = nextStep;
}
bool Oled_SetTemperature(uint16_t Temperature)
{
if(oled_step == OLED_STEP_DRAW_TEMP)
{
if(oled_draw_temp != Temperature)
{
isDrawUpdate = true;
oled_draw_temp = Temperature;
}
}
3 months ago
return true;
}
#if 0
if(SSD1306_Init(SSD1306_ADDR) != SSD1306_SUCCESS)
{
printf("ssd1306 init fail\r\n");
}
else
{
uint8_t i;
SSD1306_NormalScreen (SSD1306_ADDR);
SSD1306_ClearScreen ();
SSD1306_DrawBackGround_Log();
SSD1306_UpdateScreen (SSD1306_ADDR);
Delay_ms(1500);
SSD1306_InverseScreen (SSD1306_ADDR);
Delay_ms(1500);
SSD1306_NormalScreen (SSD1306_ADDR);
for(i = 0 ; i < 128 ; i++)
{
SSD1306_DrawBackGround_LeftShift(1);
if(i % 2 == 0)
{
SSD1306_UpdateScreen (SSD1306_ADDR);
}
//Delay_ms(10);
}
SSD1306_DrawBackGround();
SSD1306_UpdateScreen (SSD1306_ADDR);
#if 0
SSD1306_NormalScreen (SSD1306_ADDR);
SSD1306_ClearScreen (); // clear screen
SSD1306_DrawLine (0, MAX_X, 4, 4); // draw line
SSD1306_SetPosition (7, 1); // set position
SSD1306_DrawString ("SSD1306 OLED DRIVER"); // draw string
SSD1306_DrawLine (0, MAX_X, 18, 18); // draw line
SSD1306_SetPosition (40, 3); // set position
SSD1306_DrawString ("MATIASUS"); // draw string
//SSD1306_SetPosition (53, 5); // set position
//SSD1306_DrawString ("2021"); // draw string
SSD1306_UpdateScreen (SSD1306_ADDR); // update
Delay_ms (1000);
SSD1306_InverseScreen (SSD1306_ADDR);
Delay_ms (1000);
SSD1306_NormalScreen (SSD1306_ADDR);
#if 0
Delay_ms (1000);
SSD1306_InverseScreen (SSD1306_ADDR);
Delay_ms (1000);
SSD1306_NormalScreen (SSD1306_ADDR);
Delay_ms (1000);
SSD1306_InverseScreen (SSD1306_ADDR);
Delay_ms (1000);
SSD1306_NormalScreen (SSD1306_ADDR);
#endif
#endif
}
#endif