Commit 014195fc authored by Charles's avatar Charles
Browse files

Move BSP

parent 514ae726
Subproject commit 0e90cc824faf8b0a014e01473da422f9514802da
Subproject commit 66ff2446ccb7ab9beece58bfac843782050b30a7
#include "oled.h"
#include "bsp_oled.h"
oled_button_val_t oled_button_val(void){
int val = oled_button_get_volt();
if (abs(BUTTON_DOWN - val) <= BUTTON_RANGE)
return B_DOWN;
if (abs(BUTTON_LEFT - val) <= BUTTON_RANGE)
return B_LEFT;
if (abs(BUTTON_RIGHT - val) <= BUTTON_RANGE)
return B_RIGHT;
if (abs(BUTTON_UP - val) <= BUTTON_RANGE)
return B_UP;
if (abs(BUTTON_PRESSED - val) <= BUTTON_RANGE)
return B_PRESSED;
if (abs(BUTTON_STATIC - val) <= BUTTON_RANGE)
return B_STATIC;
else
return B_ERROR;
}
void oled_print_button(uint8_t row, uint8_t col){
oled_clear(Pen_Clear);
oled_button_val_t pos= oled_button_val();
switch(pos){
case(B_DOWN):
oled_printf(row, col, "DOWN");
break;
case(B_LEFT):
oled_printf(row, col, "LEFT");
break;
case(B_RIGHT):
oled_printf(row, col, "RIGHT");
break;
case(B_UP):
oled_printf(row, col, "UP");
break;
case(B_PRESSED):
oled_printf(row, col, "PRESSED");
break;
case(B_STATIC):
oled_printf(row, col, "STATIC");
break;
case(B_ERROR):
oled_printf(row, col, "ERROR");
break;
}
oled_refresh_gram();
}
#ifndef __OLED__H
#define __OLED__H
#include "bsp_oled.h"
#include <stdlib.h>
#define BUTTON_PRESSED 0
#define BUTTON_LEFT 847
#define BUTTON_RIGHT 1730
#define BUTTON_UP 2457
#define BUTTON_DOWN 3280
#define BUTTON_STATIC 4090
#define BUTTON_RANGE 128
typedef enum {
B_DOWN,
B_LEFT,
B_RIGHT,
B_UP,
B_PRESSED,
B_STATIC,
B_ERROR,
} oled_button_val_t;
//Fonctions avec le boutton
oled_button_val_t oled_button_val(void);
void oled_print_button(uint8_t row, uint8_t col);
#endif
/**
***************************************(C) COPYRIGHT 2018 DJI***************************************
* @file oled.c
* @brief this file contains sd card basic operating function
* @note
* @Version V1.0.0
* @Date Jan-28-2018
***************************************(C) COPYRIGHT 2018 DJI***************************************
*/
#include "bsp_oled.h"
#include "bsp_oledfont.h"
#include "math.h"
#include "adc.h"
#include <stdio.h>
#include <stdarg.h>
/**
* OLED flash Addr:
* [0]0 1 2 3 ... 127
* [1]0 1 2 3 ... 127
* [2]0 1 2 3 ... 127
* [3]0 1 2 3 ... 127
* [4]0 1 2 3 ... 127
* [5]0 1 2 3 ... 127
* [6]0 1 2 3 ... 127
* [7]0 1 2 3 ... 127
**/
static uint8_t OLED_GRAM[128][8];
//short delay uesd in spi transmmit
void delay_ms(uint16_t delaytimes)
{
uint16_t i;
for (i = 0; i < delaytimes; i++ )
{
int a = 10000; //delay based on mian clock, 168Mhz
while (a-- );
}
}
/**
* @brief write data/command to OLED
* @param dat: the data ready to write
* @param cmd: 0x00,command 0x01,data
* @retval
*/
void oled_write_byte(uint8_t dat, uint8_t cmd)
{
if (cmd != 0)
OLED_CMD_Set();
else
OLED_CMD_Clr();
HAL_SPI_Transmit(&hspi1, &dat, 1, 10);
}
/**
* @brief set OLED cursor position
* @param x: the X-axis of cursor
* @param y: the Y-axis of cursor
* @retval
*/
static void oled_set_pos(uint8_t x, uint8_t y)
{
x += 2;
oled_write_byte((0xb0 + y), OLED_CMD); //set page address y
oled_write_byte(((x&0xf0)>>4)|0x10, OLED_CMD); //set column high address
oled_write_byte((x&0xf0), OLED_CMD); //set column low address
}
/**
* @brief turn on OLED display
* @param None
* @param None
* @retval
*/
void oled_display_on(void)
{
oled_write_byte(0x8d, OLED_CMD);
oled_write_byte(0x14, OLED_CMD);
oled_write_byte(0xaf, OLED_CMD);
}
/**
* @brief turn off OLED display
* @param None
* @param None
* @retval
*/
void oled_display_off(void)
{
oled_write_byte(0x8d, OLED_CMD);
oled_write_byte(0x10, OLED_CMD);
oled_write_byte(0xae, OLED_CMD);
}
/**
* @brief refresh the RAM of OLED
* @param None
* @param None
* @retval
*/
void oled_refresh_gram(void)
{
uint8_t i, n;
for (i = 0; i < 8; i++)
{
oled_set_pos(0, i);
for (n = 0; n < 128; n++)
{
oled_write_byte(OLED_GRAM[n][i], OLED_DATA);
}
}
}
/**
* @brief clear the screen
* @param None
* @param None
* @retval
*/
void oled_clear(Pen_Typedef pen)
{
uint8_t i, n;
for (i = 0; i < 8; i++)
{
for (n = 0; n <= Max_Column; n++)
{
if (pen == Pen_Write)
OLED_GRAM[n][i] = 0xff;
else if (pen == Pen_Clear)
OLED_GRAM[n][i] = 0x00;
else
OLED_GRAM[n][i] = 0xff - OLED_GRAM[n][i];
}
}
}
/**
* @brief draw a point at (x, y)
* @param x: the X-axis of cursor
* @param y: the Y-axis of cursor
* @param pen: Pen_Clear, Pen_Write, Pen_Inversion @Pen_Typedef
* @retval None
*/
void oled_drawpoint(int8_t x, int8_t y, Pen_Typedef pen)
{
uint8_t page = 0, row = 0;
/* check the corrdinate */
if ((x < 0) || (x > (X_WIDTH - 1)) || (y < 0) || (y > (Y_WIDTH - 1)))
return;
page = y / 8;
row = y % 8;
if (pen == Pen_Write)
OLED_GRAM[x][page] |= 1 << row;
else if (pen == Pen_Inversion)
OLED_GRAM[x][page] ^= 1 << row;
else
OLED_GRAM[x][page] &= ~(1 << row);
}
/**
* @brief draw a line from (x1, y1) to (x2, y2)
* @param x1, y1: the start point of line
* @param x2, y2: the end of line
* @param pen: Pen_Clear, Pen_Write, Pen_Inversion @Pen_Typedef
* @retval None
*/
void oled_drawline(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2, Pen_Typedef pen)
{
uint8_t col = 0, row = 0;
uint8_t x_st = 0, x_ed = 0, y_st = 0, y_ed = 0;
float k = 0.0f, b = 0.0f;
if (y1 == y2)
{
(x1 <= x2) ? (x_st = x1):(x_st = x2);
(x1 <= x2) ? (x_ed = x2):(x_ed = x1);
for (col = x_st; col <= x_ed; col++)
{
oled_drawpoint(col, y1, pen);
}
}
else if (x1 == x2)
{
(y1 <= y2) ? (y_st = y1):(y_st = y2);
(y1 <= y2) ? (y_ed = y2):(y_ed = y1);
for (row = y_st; row <= y_ed; row++)
{
oled_drawpoint(x1, row, pen);
}
}
else
{
k = ((float)(y2 - y1)) / (x2 - x1);
b = (float)y1 - k * x1;
(x1 <= x2) ? (x_st = x1):(x_st = x2);
(x1 <= x2) ? (x_ed = x2):(x_ed = x2);
for (col = x_st; col <= x_ed; col++)
{
oled_drawpoint(col, (uint8_t)(col * k + b), pen);
}
}
}
//To add: rectangle, fillrectangle, circle, fillcircle,
/**
* @brief show a character
* @param row: row of character (0 to 4)
* @param col: column of character (1 to 20)
* @param chr: the character ready to show
* @retval None
*/
void oled_showchar(uint8_t row, uint8_t col, uint8_t chr)
{
uint8_t x = col * 6;
uint8_t y = row * 12;
uint8_t temp, t, t1;
uint8_t y0 = y;
chr = chr - ' ';
for (t = 0; t < 12; t++)
{
temp = asc2_1206[chr][t];
for (t1 = 0; t1 < 8; t1++)
{
if (temp&0x80)
oled_drawpoint(x, y, Pen_Write);
else
oled_drawpoint(x, y, Pen_Clear);
temp <<= 1;
y++;
if ((y - y0) == 12)
{
y = y0;
x++;
break;
}
}
}
}
//m^n, used in oled_shownum
static uint32_t oled_pow(uint8_t m, uint8_t n)
{
uint32_t result = 1;
while (n--)
result *= m;
return result;
}
/**
* @brief show a number
* @param row: row of number (0 to 4)
* @param col: column of number (1 to 20)
* @param num: the number ready to show
* @param mode: 0x01, fill number with '0'; 0x00, fill number with spaces (Espaces ou 0 devant le chiffre ex: mode = 0x01, num =)
* @param len: the length of the number
* @exemple num = 5, mode = 0x01, len =5 => print 00005
* @retval None
*/
void oled_shownum(uint8_t row, uint8_t col, uint32_t num, uint8_t mode, uint8_t len)
{
uint8_t t, temp;
uint8_t enshow = 0;
for (t = 0; t < len; t++)
{
temp = (num / oled_pow(10, len - t -1)) % 10;
if (enshow == 0 && t < (len - 1))
{
if (temp == 0)
{
if (mode == 0)
oled_showchar(row, col + t, ' ');
else
oled_showchar(row, col + t, '0');
continue;
}
else
enshow = 1;
}
oled_showchar(row, col + t, temp + '0');
}
}
/**
* @brief show a character string
* @param row: row of character string begin
* @param col: column of character string begin
* @param chr: the pointer to character string
* @retval None
*/
void oled_showstring(uint8_t row, uint8_t col, uint8_t *chr)
{
uint8_t n =0;
while (chr[n] != '\0')
{
oled_showchar(row, col, chr[n]);
col++;
if (col > 20)
{
col = 0;
row += 1;
}
n++;
}
}
/**
* @brief formatted output in oled 128*64
* @param row: row of character string begin, 0 <= row <= 4;
* @param col: column of character string begin, 1 <= col <= 20;
* @param *fmt: the pointer to format character string
* @retval None
* @note if the character length is more than one row at a time, the extra characters will be truncated
* @exemple oled_printf(1, 1, "PolyStar");
*/
void oled_printf(uint8_t row, uint8_t col, const char *fmt,...)
{
uint8_t LCD_BUF[128] = {0};
uint8_t remain_size = 0;
va_list ap;
if ((row > 4) || (col > 20) || (col < 1))
return;
va_start(ap, fmt);
vsprintf((char *)LCD_BUF, fmt, ap);
va_end(ap);
remain_size = 21 - col;
LCD_BUF[remain_size] = '\0';
oled_showstring(row, col, LCD_BUF);
}
void oled_LOGO(void)
{
oled_clear(Pen_Clear);
uint8_t temp_char = 0;
uint8_t x = 0, y = 0;
uint8_t i = 0;
for(; y < 64; y += 8)
{
for(x = 0; x < 128; x++)
{
temp_char = LOGO_BMP[x][y/8];
for(i = 0; i < 8; i++)
{
if(temp_char & 0x80) oled_drawpoint(x, y + i,Pen_Write);
else oled_drawpoint(x,y + i,Pen_Clear);
temp_char <<= 1;
}
}
}
oled_refresh_gram();
}
/**
* @brief initialize the oled module
* @param None
* @retval None
*/
void oled_init(void)
{
OLED_RST_Clr();
HAL_Delay(500);
OLED_RST_Set();
oled_write_byte(0xae, OLED_CMD); //turn off oled panel
oled_write_byte(0x00, OLED_CMD); //set low column address
oled_write_byte(0x10, OLED_CMD); //set high column address
oled_write_byte(0x40, OLED_CMD); //set start line address
oled_write_byte(0x81, OLED_CMD); //set contrast control resigter
oled_write_byte(0xcf, OLED_CMD); //set SEG output current brightness
oled_write_byte(0xa1, OLED_CMD); //set SEG/column mapping
oled_write_byte(0xc8, OLED_CMD); //set COM/row scan direction
oled_write_byte(0xa6, OLED_CMD); //set nomarl display
oled_write_byte(0xa8, OLED_CMD); //set multiplex display
oled_write_byte(0x3f, OLED_CMD); //1/64 duty
oled_write_byte(0xd3, OLED_CMD); //set display offset
oled_write_byte(0x00, OLED_CMD); //not offest
oled_write_byte(0xd5, OLED_CMD); //set display clock divide ratio/oscillator frequency
oled_write_byte(0x80, OLED_CMD); //set divide ratio
oled_write_byte(0xd9, OLED_CMD); //set pre-charge period
oled_write_byte(0xf1, OLED_CMD); //pre-charge: 15 clocks, discharge: 1 clock
oled_write_byte(0xda, OLED_CMD); //set com pins hardware configuration
oled_write_byte(0x12, OLED_CMD); //
oled_write_byte(0xdb, OLED_CMD); //set vcomh
oled_write_byte(0x40, OLED_CMD); //set vcom deselect level
oled_write_byte(0x20, OLED_CMD); //set page addressing mode
oled_write_byte(0x02, OLED_CMD); //
oled_write_byte(0x8d, OLED_CMD); //set charge pump enable/disable
oled_write_byte(0x14, OLED_CMD); //charge pump disable
oled_write_byte(0xa4, OLED_CMD); //disable entire dispaly on
oled_write_byte(0xa6, OLED_CMD); //disable inverse display on
oled_write_byte(0xaf, OLED_CMD); //turn on oled panel
oled_write_byte(0xaf, OLED_CMD); //display on
oled_clear(Pen_Clear);
oled_set_pos(0, 0);
oled_button_init();
}
// Fonctions pour le boutton
void oled_button_init(void) {
}
uint16_t oled_button_get_val(void) {
return 1;
//return adc_get_val(&bsp_oled_adc);
}
float oled_button_get_volt(void) {
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, 100);
return HAL_ADC_GetValue(&hadc1);
}
/**
****************************************************************************
* @file bsp_oled.h
* @brief Interface entre le board et l'Oled. Fonctions pour print et lire les bouttons
* @note
* @Version V1.0.0
* @Date 25/01/2020
****************************************************************
*/
#ifndef __BSP_OLED__H
#define __BSP_OLED__H
#include "stm32f4xx.h"
#include "spi.h"
#include <stdint.h>
#define Max_Column 128
#define Max_Row 64
#define X_WIDTH 128
#define Y_WIDTH 64
#define OLED_CMD 0x00
#define OLED_DATA 0x01
#define CHAR_SIZE_WIDTH 6
#define VHAR_SIZE_HIGHT 12
#define OLED_CMD_Set() HAL_GPIO_WritePin(OLED_DC_GPIO_Port, OLED_DC_Pin, GPIO_PIN_SET)
#define OLED_CMD_Clr() HAL_GPIO_WritePin(OLED_DC_GPIO_Port, OLED_DC_Pin, GPIO_PIN_RESET)
#define OLED_RST_Set() HAL_GPIO_WritePin(OLED_RST_GPIO_Port, OLED_RST_Pin, GPIO_PIN_SET)
#define OLED_RST_Clr() HAL_GPIO_WritePin(OLED_RST_GPIO_Port, OLED_RST_Pin, GPIO_PIN_RESET)
typedef enum
{
Pen_Clear = 0x00,
Pen_Write = 0x01,
Pen_Inversion = 0x02,
}Pen_Typedef;
/* function define */
void oled_init(void);
void oled_write_byte(uint8_t dat, uint8_t cmd);
void oled_display_on(void);
void oled_display_off(void);
void oled_refresh_gram(void);
// Drawing functions
void oled_clear(Pen_Typedef pen);
void oled_drawpoint(int8_t x, int8_t y, Pen_Typedef pen);
void oled_drawline(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2, Pen_Typedef pen);
void oled_showchar(uint8_t row, uint8_t col, uint8_t chr);
void oled_shownum(uint8_t row, uint8_t col, uint32_t num, uint8_t mode, uint8_t len);
void oled_showstring(uint8_t row, uint8_t col, uint8_t *chr);
void oled_printf(uint8_t row, uint8_t col, const char *fmt,...);
void oled_LOGO(void);
// Functions to read the button
void oled_button_init(void);