Full color support, rework the directory structure
This commit is contained in:
189
firmware/Bsp/Drivers/DisplayDriver.cpp
Normal file
189
firmware/Bsp/Drivers/DisplayDriver.cpp
Normal file
@@ -0,0 +1,189 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Max Regan
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <cstring>
|
||||
|
||||
#include "Bsp/Drivers/DisplayDriver.h"
|
||||
#include "Bsp/macros.h"
|
||||
#include "Bsp/font.h"
|
||||
|
||||
namespace BSP {
|
||||
|
||||
using Common::Schedule::NextTime;
|
||||
using Common::ReturnCode;
|
||||
|
||||
DisplayDriver::DisplayDriver(Common::Schedule::TaskScheduler &scheduler, SpiDriver &spi)
|
||||
: m_scheduler(scheduler)
|
||||
, m_spi(spi)
|
||||
, m_is_dirty(true)
|
||||
, m_dirty_line_min(0)
|
||||
, m_dirty_line_max(0)
|
||||
{
|
||||
buffer_init(DEFAULT_COLOR);
|
||||
}
|
||||
|
||||
ReturnCode DisplayDriver::init()
|
||||
{
|
||||
return Common::ReturnCode::OK;
|
||||
}
|
||||
|
||||
NextTime DisplayDriver::execute()
|
||||
{
|
||||
return NextTime::never();
|
||||
}
|
||||
|
||||
// TODO: write my own implementation
|
||||
#define R2(n) n, n + 2*64, n + 1*64, n + 3*64
|
||||
#define R4(n) R2(n), R2(n + 2*16), R2(n + 1*16), R2(n + 3*16)
|
||||
#define R6(n) R4(n), R4(n + 2*4 ), R4(n + 1*4 ), R4(n + 3*4 )
|
||||
|
||||
static constexpr unsigned char BitReverseTable256[256] =
|
||||
{
|
||||
R6(0), R6(2), R6(1), R6(3)
|
||||
};
|
||||
|
||||
unsigned char ReverseBitsLookupTable(unsigned char v)
|
||||
{
|
||||
return BitReverseTable256[v];
|
||||
}
|
||||
|
||||
void DisplayDriver::buffer_init(Color color)
|
||||
{
|
||||
uint8_t dataval = 0xFF;
|
||||
if (color == Color::BLACK) {
|
||||
dataval = 0x00;
|
||||
}
|
||||
|
||||
// TODO: Support initializing to other colors
|
||||
|
||||
for (size_t i = 0; i < ARRAY_SIZE(m_buffer.lines); i++) {
|
||||
struct display_line &line = m_buffer.lines[i];
|
||||
line.mode = 1; // Update display
|
||||
line.line = i + 1; // Line numbers start at 1
|
||||
for (size_t j = 0; j < ARRAY_SIZE(line.data); j++) {
|
||||
line.data[j] = dataval;
|
||||
}
|
||||
}
|
||||
|
||||
m_buffer.dummy = 0;
|
||||
}
|
||||
|
||||
void DisplayDriver::set_dirty(unsigned int y)
|
||||
{
|
||||
if (!m_is_dirty) {
|
||||
m_is_dirty = true;
|
||||
m_dirty_line_min = y;
|
||||
m_dirty_line_max = y;
|
||||
} else {
|
||||
m_dirty_line_min = MIN(y, m_dirty_line_min);
|
||||
m_dirty_line_max = MAX(y, m_dirty_line_max);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: write my own implementation
|
||||
#define R2(n) n, n + 2*64, n + 1*64, n + 3*64
|
||||
#define R4(n) R2(n), R2(n + 2*16), R2(n + 1*16), R2(n + 3*16)
|
||||
#define R6(n) R4(n), R4(n + 2*4 ), R4(n + 1*4 ), R4(n + 3*4 )
|
||||
|
||||
void DisplayDriver::draw_hline(uint32_t x, uint32_t y, uint32_t width, Color color)
|
||||
{
|
||||
for (uint32_t i = 0; i < width; i++) {
|
||||
set_pixel(x + i, y, color);
|
||||
}
|
||||
}
|
||||
|
||||
void DisplayDriver::write_glyph(uint32_t x_off, uint32_t y_off,
|
||||
const struct font *f, const struct glyph *g,
|
||||
Color color)
|
||||
{
|
||||
for (size_t x = 0; x < g->width; x++) {
|
||||
for (size_t y = 0; y < g->height; y++) {
|
||||
uint32_t byte_x = x / 8;
|
||||
uint32_t bit_x = 7 - (x % 8);
|
||||
uint8_t bit = (g->bitmap[byte_x + g->width_bytes * y] >> bit_x) & 1;
|
||||
if (bit) {
|
||||
set_pixel(g->left + x_off + x, y_off + y + f->height - g->top, color);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void DisplayDriver::char_at(uint32_t *x_off, uint32_t y_off, char c, const struct font *font, Color color)
|
||||
{
|
||||
const struct glyph *g = glyph_for_char(font, c);
|
||||
if (g == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (*x_off + font->width >= DISPLAY_WIDTH) {
|
||||
return;
|
||||
}
|
||||
|
||||
write_glyph(*x_off, y_off, font, g, color);
|
||||
|
||||
m_dirty_line_min = MIN(m_dirty_line_min, y_off);
|
||||
m_dirty_line_max = MAX(m_dirty_line_max, y_off + g->height);
|
||||
m_is_dirty = true;
|
||||
*x_off += font->width;
|
||||
}
|
||||
|
||||
void DisplayDriver::string_at(uint32_t *x_off, uint32_t y_off, const char *string, const struct font *font, Color color)
|
||||
{
|
||||
int i = 0;
|
||||
while (string[i]) {
|
||||
char_at(x_off, y_off, string[i], font, color);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
void DisplayDriver::refresh()
|
||||
{
|
||||
if (!m_is_dirty) {
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t *start = (uint8_t *) &m_buffer.lines[m_dirty_line_min];
|
||||
// Data size
|
||||
size_t size = sizeof(m_buffer.lines[0]) * (m_dirty_line_max - m_dirty_line_min + 1);
|
||||
// Trailer dummy data
|
||||
size += 2;
|
||||
|
||||
m_spi.tx_blocking(start, size);
|
||||
m_is_dirty = false;
|
||||
m_dirty_line_min = DISPLAY_HEIGHT - 1;
|
||||
m_dirty_line_max = 0;
|
||||
}
|
||||
|
||||
void DisplayDriver::clear(Color color)
|
||||
{
|
||||
buffer_init(color);
|
||||
m_is_dirty = true;
|
||||
m_dirty_line_min = 0;
|
||||
m_dirty_line_max = DISPLAY_HEIGHT - 1;
|
||||
}
|
||||
|
||||
//TODO: put me somewhere fonty
|
||||
const struct glyph *DisplayDriver::glyph_for_char(const struct font *font, char c)
|
||||
{
|
||||
return font->glyphs[(size_t) c];
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user