Skip to content

Commit

Permalink
Merge pull request #15 from sweetlilmre/lcd-cleanup
Browse files Browse the repository at this point in the history
Lcd cleanup
  • Loading branch information
sweetlilmre committed May 24, 2015
2 parents 5ac5188 + 2b45aad commit ca91f1c
Show file tree
Hide file tree
Showing 14 changed files with 358 additions and 27 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Tapuino
The $20 C64 Tape Emulator

Details can be found on my blog: http://www.sweetlilmre.com
The FAQ and other docs can be found on the wiki: https://github.com/sweetlilmre/tapuino/wiki

Portions of this code are copyright by their respective owners:

Expand Down
29 changes: 29 additions & 0 deletions config-user.h.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/********************************************************************************/
// User selectable configuration settings
//
/********************************************************************************/

/********************************************************************************/
// LCD Definitions
/********************************************************************************/

// uncomment one of these sets for your specific LCD

// #define LCD_USE_1602_LCD_MODULE
// #define LCD_I2C_ADDR 0x27 // I2C address for the LCD

// #define LCD_USE_SSD1306_OLED_MODULE
// #define LCD_I2C_ADDR 0x3C // I2C address for the OLED

// #define LCD_USE_SSD131X_OLED_MODULE
// #define LCD_I2C_ADDR 0x3C // I2C address for the OLED

/********************************************************************************/
// Language Definitions
/********************************************************************************/

// uncomment one of these for your language

// #define TAPUINO_LANGUAGE_EN
// #define TAPUINO_LANGUAGE_IT

15 changes: 9 additions & 6 deletions config.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
#ifndef _CONFIG_H
#define _CONFIG_H

// LCD Definitions
// I2C config and expander data lines
#define LCD_USE_1602_LCD_MODULE
#define LCD_I2C_ADDR 0x27 // I2C address for the LCD
#include "config-user.h"

//#define LCD_USE_SD131X_OLED_MODULE
//#define LCD_I2C_ADDR 0x3C // I2C address for the OLED
// thanks stack overflow: http://stackoverflow.com/questions/4301471/c-macro-to-test-if-more-than-one-defined
#if defined(LCD_USE_SSD1306_OLED_MODULE) + defined(LCD_USE_1602_LCD_MODULE) + defined(LCD_USE_SSD131X_OLED_MODULE) != 1
#error Ether no or multiple LCD types defined! Have you created your config-user.h file?
#endif

#if defined(TAPUINO_LANGUAGE_EN) + defined(TAPUINO_LANGUAGE_IT) != 1
#error Either no or multiple languages defined! Have you created your config-user.h file?
#endif


#define LCD_BIT_RS 0 // Register select
Expand Down
18 changes: 17 additions & 1 deletion dsip_lcd.c → disp_LCD_HD44780.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include <util/delay.h>
#include <avr/pgmspace.h>

#include "i2cmaster.h"
#include "i2c_master.h"

// commands
#define LCD_CLEARDISPLAY 0x01
Expand Down Expand Up @@ -59,6 +59,17 @@
#define LCD_5x10DOTS 0x04
#define LCD_5x8DOTS 0x00

uint8_t backslashChar[8] = {
0b00000,
0b10000,
0b01000,
0b00100,
0b00010,
0b00001,
0b00000,
0b00000
};


// When the display powers up, it is configured as follows:
//
Expand Down Expand Up @@ -96,8 +107,11 @@ void lcd_begin(uint8_t lcd_addr, uint8_t cols, uint8_t lines, uint8_t dotsize);
void lcd_display();
void lcd_clear();
void lcd_home();
void lcd_createChar(uint8_t location, uint8_t charmap[]);

void lcd_write(uint8_t value) {
// replace backslash with redefined char
if (value == '\\') value = 0x01;
send(value, _BV(LCD_BIT_RS));
}

Expand All @@ -109,6 +123,8 @@ void command(uint8_t value) {

void lcd_init(uint8_t lcd_addr) {
lcd_begin(lcd_addr, MAX_LCD_LINE_LEN, LCD_NUM_LINES, LCD_5x8DOTS);
// can't define this as the zeroth character as zero is null in strings :)! :)
lcd_createChar(1, backslashChar);
}

void lcd_begin(uint8_t lcd_addr, uint8_t cols, uint8_t lines, uint8_t dotsize) {
Expand Down
2 changes: 1 addition & 1 deletion disp_oled_SSD131x.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "config.h"
#ifdef LCD_USE_SD131X_OLED_MODULE
#ifdef LCD_USE_SSD131X_OLED_MODULE

#include <util/delay.h>
#include "i2cmaster.h"
Expand Down
187 changes: 187 additions & 0 deletions disp_oled_ssd1306.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
#include "config.h"
#ifdef LCD_USE_SSD1306_OLED_MODULE

#include <string.h>
#include <avr/pgmspace.h>
#include "i2c_master.h"
#include "font8x8.h"

#define NUM_COLS 16
#define NUM_ROWS 2
#define NUM_CHARS NUM_COLS * NUM_ROWS

static uint8_t _addr; // I2C address
static uint8_t _row, _col;
static uint8_t _buffer[NUM_CHARS];
static uint8_t _displayCursor;

void write_raw(uint8_t value, uint8_t cursor);
void ssd1306_fillscreen(uint8_t fill);
void ssd1306_send_command_start(void);
void ssd1306_send_command(uint8_t command);
void ssd1306_send_data_start(void);

// Init Sequence
const uint8_t ssd1306_init_sequence [] PROGMEM = {
0xAE, // Display OFF (sleep mode)
0x20, 0b00, // Set Memory Addressing Mode
// 00=Horizontal Addressing Mode; 01=Vertical Addressing Mode;
// 10=Page Addressing Mode (RESET); 11=Invalid
0xB0, // Set Page Start Address for Page Addressing Mode, 0-7
0xC8, // Set COM Output Scan Direction
0x00, // ---set low column address
0x10, // ---set high column address
0x40, // --set start line address
0x81, 0x3F, // Set contrast control register
0xA1, // Set Segment Re-map. A0=address mapped; A1=address 127 mapped.
0xA6, // Set display mode. A6=Normal; A7=Inverse
0xA8, 0x3F, // Set multiplex ratio (1 to 64)
0xA4, // Output RAM to Display
// 0xA4=Output follows RAM content; 0xA5,Output ignores RAM content
0xD3, 0x00, // Set display offset. 00 = no offset
0xD5, // --set display clock divide ratio/oscillator frequency
0xF0, // --set divide ratio
0xD9, 0x22, // Set pre-charge period
0xDA, 0x12, // Set com pins hardware configuration
0xDB, // --set vcomh
0x20, // 0x20,0.77xVcc
0x8D, 0x14, // Set DC-DC enable
0xAF // Display ON in normal mode
};


void lcd_init(uint8_t lcd_addr)
{
uint8_t i;
_addr = lcd_addr;
_row = 0;
_col = 0;
_displayCursor = 0;

i2c_init();

for (i = 0; i < sizeof (ssd1306_init_sequence); i++) {
ssd1306_send_command(pgm_read_byte(&ssd1306_init_sequence[i]));
}
ssd1306_fillscreen(0);
memset(_buffer, 32, NUM_CHARS);
}

void lcd_cursor() {
uint8_t curValue = _buffer[_row * NUM_COLS + _col];
_displayCursor = 1;
write_raw(curValue, 1);
}

void lcd_noCursor(){
uint8_t curValue = _buffer[_row * NUM_COLS + _col];
_displayCursor = 0;
write_raw(curValue, 0);
}

void lcd_backlight() {

}

void lcd_noBacklight() {

}


void write_raw(uint8_t value, uint8_t cursor) {
uint8_t i, v, col;
uint8_t c = value - 32;

col = _col << 3; // convert to pixel
ssd1306_send_command_start();
i2c_write(0xb0 + _row);
i2c_write((col & 0x0f) | 0x00);
i2c_write(((col & 0xf0) >> 4) | 0x10);

ssd1306_send_data_start();
for (i = 0; i < 8; i++)
{
v = pgm_read_byte(&font8x8[c * 8 + i]);
// add underline for cursor
if (cursor) v |= 0x80;
i2c_write(v);
}
i2c_stop();
}

void lcd_setCursor(uint8_t col, uint8_t row)
{
if ((row < NUM_ROWS) && (col < NUM_COLS)) {
if (_displayCursor) {
uint8_t curValue = _buffer[_row * NUM_COLS + _col];
write_raw(curValue, 0);
}

_col = col;
_row = row;

if (_displayCursor) {
uint8_t curValue = _buffer[_row * NUM_COLS + _col];
write_raw(curValue, 1);
}
}
}

// ----------------------------------------------------------------------------

void lcd_write(uint8_t value) {
write_raw(value, 0);

_buffer[_row * NUM_COLS + _col] = value;
_col++;
if (_col >= NUM_COLS) {
_col = 0;
_row = (_row + 1) % NUM_ROWS;
}
if (_displayCursor) {
uint8_t curValue = _buffer[_row * NUM_COLS + _col];
write_raw(curValue, 1);
}
}

void lcd_print(char *s) {
while (*s) {
lcd_write(*s++);
}
}

// ----------------------------------------------------------------------------
void ssd1306_fillscreen(uint8_t fill) {
uint8_t m, n;
for (m = 0; m < 8; m++)
{
ssd1306_send_command_start();
i2c_write(0xb0 + m); // page0 - page7
i2c_write(0x00); // low column start address
i2c_write(0x10); // high column start address
ssd1306_send_data_start();
for (n = 0; n < 128; n++)
{
i2c_write(fill);
}
i2c_stop();
}
}

void ssd1306_send_command_start(void) {
i2c_start(_addr << 1);
i2c_write(0x00); // command
}

void ssd1306_send_command(uint8_t command) {
ssd1306_send_command_start();
i2c_write(command);
i2c_stop();
}

void ssd1306_send_data_start(void) {
i2c_start(_addr << 1);
i2c_write(0x40); // data
}

#endif
Loading

0 comments on commit ca91f1c

Please sign in to comment.