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

333 lines
6.9 KiB
C

4 years ago
/**
* dogs.c - Interface for drawing to the EADOGS102N-6 display.
* Copyright (C) 2020 Clyne Sullivan
*
* 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.
* See the GNU General Public License for more details.
*/
#include "ch.h"
#include "dogs.h"
#include "hal.h"
#define SET_DATA palSetPad(GPIOC, 15)
#define SET_CMD palClearPad(GPIOC, 15)
#define CS_HIGH palSetPad(GPIOA, 4)
#define CS_LOW palClearPad(GPIOA, 4)
unsigned char dogs_buffer[DISP_WIDTH * DISP_HEIGHT / 8];
static volatile bool dogs_spi_done = false;
4 years ago
static void dogs_init_display();
static void dogs_spi_callback(SPIDriver *spid)
{
if (spiIsBufferComplete(spid))
dogs_spi_done = true;
}
4 years ago
void dogs_init()
{
// SPI
palSetPadMode(GPIOB, 1, PAL_MODE_ALTERNATE(1)); // MOSI
palSetPadMode(GPIOA, 5, PAL_MODE_ALTERNATE(0)); // CLK
palSetPadMode(GPIOA, 4, PAL_MODE_OUTPUT_PUSHPULL); // nCS
palSetPadMode(GPIOC, 14, PAL_MODE_OUTPUT_PUSHPULL); // nRST
palSetPadMode(GPIOC, 15, PAL_MODE_OUTPUT_PUSHPULL); // CD
palSetPad(GPIOA, 4);
palSetPad(GPIOC, 14);
palClearPad(GPIOC, 15);
static const SPIConfig spicfg = {
4 years ago
false,
dogs_spi_callback,
0, // cr1
4 years ago
0
};
spiStart(&SPID1, &spicfg);
4 years ago
dogs_init_display();
dogs_clear();
dogs_flush();
4 years ago
}
void dogs_write_data(unsigned char byte)
{
SET_DATA;
dogs_spi_done = false;
spiStartSend(&SPID1, 1, &byte);
while (!dogs_spi_done);
4 years ago
}
void dogs_write_cmd(unsigned char byte)
{
SET_CMD;
dogs_spi_done = false;
spiStartSend(&SPID1, 1, &byte);
while (!dogs_spi_done);
4 years ago
}
void dogs_set_column(unsigned int col)
{
//if (col < DISP_WIDTH) {
dogs_write_cmd(0x10 | ((col >> 4) & 0xF));
dogs_write_cmd(0x00 | (col & 0xF));
//}
}
void dogs_set_power(unsigned int bits)
{
dogs_write_cmd(0x28 | (bits & 0x7));
}
void dogs_set_scroll_line(unsigned int startline)
{
dogs_write_cmd(0x40 | (startline & 0x3F));
}
void dogs_set_page(unsigned int page)
{
dogs_write_cmd(0xB0 | (page & 0xF));
}
void dogs_set_vlcd_ratio(unsigned int ratio)
{
dogs_write_cmd(0x20 | (ratio & 0x7));
}
void dogs_set_contrast(unsigned int value)
{
dogs_write_cmd(0x81);
dogs_write_cmd(0x00 | (value & 0x3F));
}
void dogs_set_pixelson(bool on)
{
dogs_write_cmd(on ? 0xA5 : 0xA4);
}
void dogs_set_invert(bool invert)
{
dogs_write_cmd(invert ? 0xA7 : 0xA6);
}
void dogs_set_sleep(bool sleep)
{
dogs_write_cmd(sleep ? 0xAE : 0xAF);
}
void dogs_set_segdir(bool normal)
{
dogs_write_cmd(normal ? 0xA0 : 0xA1);
}
void dogs_set_comdir(bool normal)
{
dogs_write_cmd(normal ? 0xC0 : 0xC8);
}
void dogs_reset()
{
dogs_write_cmd(0xE2);
}
void dogs_set_bias(bool higher)
{
dogs_write_cmd(higher ? 0xA2 : 0xA3);
}
void dogs_set_advanced(unsigned int bits)
{
dogs_write_cmd(0xFA);
dogs_write_cmd(0x10 | bits);
}
void dogs_init_display()
{
CS_LOW;
dogs_reset();
CS_HIGH;
chThdSleepMilliseconds(100);
CS_LOW;
dogs_set_scroll_line(0);
dogs_set_segdir(true);
dogs_set_comdir(false);
dogs_set_pixelson(false);
dogs_set_invert(false);
dogs_set_bias(true);
dogs_set_power(0x07);
dogs_set_vlcd_ratio(7);
dogs_set_contrast(0x10);
dogs_set_advanced(0x83);
dogs_set_sleep(false);
CS_HIGH;
}
void dogs_clear()
{
unsigned char *ptr = dogs_buffer;
unsigned int count = sizeof(dogs_buffer);
for (; count > 8; count -= 8) {
*ptr++ = 0;
*ptr++ = 0;
*ptr++ = 0;
*ptr++ = 0;
*ptr++ = 0;
*ptr++ = 0;
*ptr++ = 0;
4 years ago
*ptr++ = 0;
}
}
void dogs_flush()
{
unsigned char *ptr = dogs_buffer;
CS_LOW;
for (int page = 0; page < 8; page++) {
dogs_set_page(page);
dogs_set_column(30);
SET_DATA;
dogs_spi_done = false;
spiStartSend(&SPID1, 102, ptr);
ptr += 102;
while (!dogs_spi_done);
4 years ago
}
CS_HIGH;
}
void draw_pixel(int x, int y, bool state)
{
if (state)
dogs_buffer[y / 8 * DISP_WIDTH + x] |= (1 << (y % 8));
else
dogs_buffer[y / 8 * DISP_WIDTH + x] &= ~(1 << (y % 8));
}
void draw_bitmap(int x, int y, const unsigned char *buffer)
{
// Prepare source information
const unsigned char *src = buffer;
const int width = *src++;
const int height = *src++;
int sbit = 0;
for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) {
draw_pixel(x + i, y + j, *src & (1 << sbit));
if (++sbit == 8) {
sbit = 0;
++src;
}
}
}
}
static const unsigned char draw_number_bitmaps[10][10] = {
{ 8, 8, // '0'
0b00011000,
0b00100100,
0b01000010,
0b01000010,
0b01000010,
0b01000010,
0b00100100,
0b00011000,
},
{ 8, 8, // '1'
0b00011000,
0b00101000,
0b00001000,
0b00001000,
0b00001000,
0b00001000,
0b00001000,
0b00111110,
},
{ 8, 8, // '2'
0b00011000,
0b00100100,
0b01000010,
0b00000010,
0b00000100,
0b00001000,
0b00010000,
0b01111110,
},
{ 8, 8, // '3'
0b00111000,
0b01000100,
0b00000010,
0b00111100,
0b00000100,
0b00000010,
0b01000100,
0b00111000,
},
{ 8, 8, // '4'
0b00000100,
0b00001100,
0b00010100,
0b00100100,
0b01111100,
0b00000100,
0b00000100,
0b00000100,
},
{ 8, 8, // '5'
0b01111110,
0b01000000,
0b01111000,
0b00000100,
0b00000010,
0b00000010,
0b01000100,
0b00111000,
},
{ 8, 8, // '6'
0b00011100,
0b00100000,
0b01000000,
0b01011100,
0b01100010,
0b01000010,
0b00100100,
0b00011000,
},
{ 8, 8, // '7'
0b01111110,
0b00000010,
0b00000100,
0b00001000,
0b00010000,
0b00010000,
0b00010000,
0b00010000,
},
{ 8, 8, // '8'
0b00011000,
0b00100100,
0b01000010,
0b00111100,
0b00100100,
0b01000010,
0b00100100,
0b00011000,
},
{ 8, 8, // '9'
0b00011000,
0b00100100,
0b01000010,
0b00100010,
0b00011110,
0b00000010,
0b01000100,
0b00111000,
},
};
void draw_number(int x, int y, int number)
{
if (number < 0)
number = -number;
int tmp = number;
int count;
for (count = 0; tmp; count++)
tmp /= 10;
x += count * 8;
do {
x -= 8;
draw_bitmap(x, y, draw_number_bitmaps[number % 10]);
} while (number /= 10);
}