2021-03-28 08:35:27 +00:00
|
|
|
/***************************************************************************
|
2023-02-08 15:33:02 +00:00
|
|
|
* Copyright (C) 2021 - 2023 by Federico Amedeo Izzo IU2NUO, *
|
2022-06-02 07:56:05 +00:00
|
|
|
* Niccolò Izzo IU2KIN *
|
|
|
|
* Frederik Saraci IU2NRO *
|
|
|
|
* Silvano Seva IU2KWO *
|
2021-03-28 08:35:27 +00:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/> *
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2023-07-21 06:35:39 +00:00
|
|
|
#include <peripherals/gpio.h>
|
2024-10-08 17:19:59 +00:00
|
|
|
#include <peripherals/spi.h>
|
2021-03-28 08:35:27 +00:00
|
|
|
#include <interfaces/display.h>
|
|
|
|
#include <interfaces/delays.h>
|
|
|
|
#include "hwconfig.h"
|
|
|
|
|
2022-08-10 10:26:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \internal
|
|
|
|
* Send one row of pixels to the display.
|
|
|
|
* Pixels in framebuffer are stored "by rows", while display needs data to be
|
|
|
|
* sent "by columns": this function performs the needed conversion.
|
|
|
|
*
|
|
|
|
* @param row: pixel row to be be sent.
|
|
|
|
*/
|
2024-01-17 20:46:05 +00:00
|
|
|
static void display_renderRow(uint8_t row, uint8_t *frameBuffer)
|
2021-03-28 08:35:27 +00:00
|
|
|
{
|
2022-08-10 10:26:04 +00:00
|
|
|
/* magic stuff */
|
|
|
|
uint8_t *buf = (frameBuffer + 128 * row);
|
|
|
|
for (uint8_t i = 0; i<16; i++)
|
2021-03-28 08:35:27 +00:00
|
|
|
{
|
2022-08-10 10:26:04 +00:00
|
|
|
uint8_t tmp[8] = {0};
|
|
|
|
for (uint8_t j = 0; j < 8; j++)
|
|
|
|
{
|
|
|
|
uint8_t tmp_buf = buf[j*16 + i];
|
|
|
|
int count = __builtin_popcount(tmp_buf);
|
|
|
|
while (count > 0)
|
|
|
|
{
|
|
|
|
int pos = __builtin_ctz(tmp_buf);
|
|
|
|
tmp[pos] |= 1UL << j;
|
|
|
|
tmp_buf &= ~(1 << pos);
|
|
|
|
count--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-08 17:19:59 +00:00
|
|
|
spi_send(&spi2, tmp, 8);
|
2021-03-28 08:35:27 +00:00
|
|
|
}
|
2022-08-10 10:26:04 +00:00
|
|
|
}
|
2021-03-28 08:35:27 +00:00
|
|
|
|
2022-08-10 10:26:04 +00:00
|
|
|
|
|
|
|
void display_init()
|
|
|
|
{
|
2021-03-28 08:35:27 +00:00
|
|
|
gpio_setMode(LCD_CS, OUTPUT);
|
|
|
|
gpio_setMode(LCD_RST, OUTPUT);
|
|
|
|
gpio_setMode(LCD_RS, OUTPUT);
|
|
|
|
|
|
|
|
gpio_setPin(LCD_CS);
|
|
|
|
gpio_clearPin(LCD_RS);
|
|
|
|
|
|
|
|
gpio_clearPin(LCD_RST); /* Reset controller */
|
|
|
|
delayMs(1);
|
|
|
|
gpio_setPin(LCD_RST);
|
|
|
|
delayMs(5);
|
|
|
|
|
2024-10-08 17:19:59 +00:00
|
|
|
uint8_t initData[] =
|
|
|
|
{
|
|
|
|
0x2F, /* Voltage Follower On */
|
|
|
|
0x81, /* Set Electronic Volume */
|
|
|
|
0x15, /* Contrast, initial setting */
|
|
|
|
0xA2, /* Set Bias = 1/9 */
|
|
|
|
0xA1, /* A0 Set SEG Direction */
|
|
|
|
0xC0, /* Set COM Direction */
|
|
|
|
0xA4, /* White background, black pixels */
|
|
|
|
0xAF, /* Set Display Enable */
|
|
|
|
};
|
|
|
|
|
|
|
|
spi_acquire(&spi2);
|
2021-03-28 08:35:27 +00:00
|
|
|
gpio_clearPin(LCD_CS);
|
2024-10-08 17:19:59 +00:00
|
|
|
gpio_clearPin(LCD_RS); /* RS low -> command mode */
|
|
|
|
spi_send(&spi2, initData, sizeof(initData));
|
2021-03-28 08:35:27 +00:00
|
|
|
gpio_clearPin(LCD_CS);
|
2024-10-08 17:19:59 +00:00
|
|
|
spi_release(&spi2);
|
2021-03-28 08:35:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void display_terminate()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-01-17 20:46:05 +00:00
|
|
|
void display_renderRows(uint8_t startRow, uint8_t endRow, void *fb)
|
2021-03-28 08:35:27 +00:00
|
|
|
{
|
2024-10-08 17:19:59 +00:00
|
|
|
spi_acquire(&spi2);
|
2021-03-28 08:35:27 +00:00
|
|
|
gpio_clearPin(LCD_CS);
|
|
|
|
|
|
|
|
for(uint8_t row = startRow; row < endRow; row++)
|
|
|
|
{
|
2024-10-08 17:19:59 +00:00
|
|
|
uint8_t command[3];
|
|
|
|
command[0] = 0xB0 | row; /* Set Y position */
|
|
|
|
command[1] = 0x10; /* Set X position */
|
|
|
|
command[2] = 0x04;
|
|
|
|
|
2021-03-28 08:35:27 +00:00
|
|
|
gpio_clearPin(LCD_RS); /* RS low -> command mode */
|
2024-10-08 17:19:59 +00:00
|
|
|
spi_send(&spi2, command, 3);
|
2021-03-28 08:35:27 +00:00
|
|
|
gpio_setPin(LCD_RS); /* RS high -> data mode */
|
2024-01-17 20:46:05 +00:00
|
|
|
display_renderRow(row, (uint8_t *) fb);
|
2021-03-28 08:35:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gpio_setPin(LCD_CS);
|
2024-10-08 17:19:59 +00:00
|
|
|
spi_release(&spi2);
|
2021-03-28 08:35:27 +00:00
|
|
|
}
|
|
|
|
|
2024-01-17 20:46:05 +00:00
|
|
|
void display_render(void *fb)
|
2021-03-28 08:35:27 +00:00
|
|
|
{
|
2024-01-17 20:46:05 +00:00
|
|
|
display_renderRows(0, CONFIG_SCREEN_HEIGHT / 8, fb);
|
2021-03-28 08:35:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void display_setContrast(uint8_t contrast)
|
|
|
|
{
|
2024-10-08 17:19:59 +00:00
|
|
|
uint8_t command[2];
|
|
|
|
command[0] = 0x81; /* Set Electronic Volume */
|
|
|
|
command[1] = contrast >> 2; /* Controller contrast range is 0 - 63 */
|
2021-03-28 08:35:27 +00:00
|
|
|
|
2024-10-08 17:19:59 +00:00
|
|
|
spi_acquire(&spi2);
|
|
|
|
gpio_clearPin(LCD_CS);
|
2021-03-28 08:35:27 +00:00
|
|
|
|
2024-10-08 17:19:59 +00:00
|
|
|
gpio_clearPin(LCD_RS); /* RS low -> command mode */
|
|
|
|
spi_send(&spi2, command, 2);
|
2021-03-28 08:35:27 +00:00
|
|
|
gpio_setPin(LCD_CS);
|
2024-10-08 17:19:59 +00:00
|
|
|
spi_release(&spi2);
|
2021-03-28 08:35:27 +00:00
|
|
|
}
|
2023-07-13 15:30:52 +00:00
|
|
|
|
|
|
|
void display_setBacklightLevel(uint8_t level)
|
|
|
|
{
|
|
|
|
(void) level;
|
|
|
|
}
|