Quella demo è rilasciata sotto questa incredibile licenza.
/*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <joerg@FreeBSD.ORG> wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return. Joerg Wunsch
* ----------------------------------------------------------------------------
*
* Stdio demo, upper layer of LCD driver.
*
* $Id: lcd.c 1008 2005-12-28 21:38:59Z joerg_wunsch $
*/
Io preferirei la licenza LGPL, ma non so se visto le modifiche sia possibile cambiarla in LGPL, mi tocca riscriverla e specificare l'origine del codice.
// file lcd.c
#include "defines.h"
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <avr/io.h>
#include <util/delay.h>
#include "hd44780.h"
#include "lcd.h"
/*
* _LCD_rowAddress[], _LCD_nRows, _LCD_nColumns vengono inizializzati
* grazie alla macro LCD_20x4 o LCD_16x1 ecc presenti in lcd.h
*/
extern uint8_t _LCD_row_address[];
extern uint8_t _LCD_n_rows;
extern uint8_t _LCD_n_columns;
/*
* Mantiene info circa la riga corrente
*/
static uint8_t currentRow = 0;
/*
* Mantiene info circa la colonna corrente
*/
static uint8_t currentCol = 0;
/*
* il verso dello scroll verticale
*/
enum ScrollDirection { up, down };
/*
* il buffer di riga
*/
char *rowBuffer = 0;
/*
* sposta il "cursore" in base ai valori di currentRow currentColumn;
*/
void setRowAddress();
void setRowAddress()
{
// wait_ready da chiamare sempre prima di ogni dialogo con il display
hd44780_wait_ready(false);
// contiene l'indirizzo di ogni inizio riga
uint8_t currentAddress = _LCD_row_address[currentRow];
hd44780_outcmd(HD44780_DDADDR(currentAddress + currentCol));
}
void scroolView(enum ScrollDirection dir);
void scroolView(enum ScrollDirection dir)
{
uint8_t crow;
switch(dir) {
case up:
break;
case down:
// conta da riga 1 a riga _LCD_n_rows - 1
for (uint8_t irow = 1; irow < _LCD_n_rows; irow++) {
// crow contiene l'indirizzo della riga corrente irow
crow = _LCD_row_address[irow];
// conta da 0 a _LCD_n_columns - 1
for (uint8_t icol = 0; icol < _LCD_n_columns; icol++) {
hd44780_wait_ready(false);
hd44780_outcmd(HD44780_DDADDR(crow + icol));
hd44780_wait_ready(false);
// legge un carattere dal display e lo salva nel rowBuffer
rowBuffer[icol] = (char)hd44780_inbyte(1);
}
// scrive il contenuro di rowBuffer nel display partendo
// da irow-1 che sarebbe la riga precedente
for (uint8_t icol = 0; icol < _LCD_n_columns; icol++) {
hd44780_wait_ready(false);
hd44780_outcmd(HD44780_DDADDR(_LCD_row_address[irow-1]+icol));
hd44780_wait_ready(false);
hd44780_outdata(rowBuffer[icol]);
}
}
// pulisce l'ultima riga del display scrivendo 20 spazi.
for (uint8_t icol = 0; icol < _LCD_n_columns; icol++) {
hd44780_wait_ready(false);
hd44780_outcmd(HD44780_DDADDR(_LCD_row_address[_LCD_n_rows-1]+icol));
hd44780_wait_ready(false);
hd44780_outdata(' ');
}
break;
}
}
/*
* Setup the LCD controller. First, call the hardware initialization
* function, then adjust the display attributes we want.
*/
void
lcd_init(void)
{
/*
* alloca lo spazio per il buffer di riga
* numero di colonne + 1 per fare spazio a \0
*/
rowBuffer = calloc(_LCD_n_columns+1, sizeof(char));
hd44780_init();
/*
* Clear the display.
*/
hd44780_outcmd(HD44780_CLR);
hd44780_wait_ready(false);
/*
* Entry mode: auto-increment address counter, no display shift in
* effect.
*/
hd44780_outcmd(HD44780_ENTMODE(1, 0));
hd44780_wait_ready(false);
/*
* Enable display, activate non-blinking cursor.
*/
hd44780_outcmd(HD44780_DISPCTL(1, 1, 0));
hd44780_wait_ready(false);
}
/*
* Send character c to the LCD display. After a '\n' has been seen,
* the next character will first clear the display.
*/
int
lcd_putchar(char c, FILE *unused)
{
if (c == '\n') {
currentRow++;
// se necessario scrolla
if (currentRow > _LCD_n_rows - 1) {
scroolView(down);
currentRow = _LCD_n_rows - 1;
}
currentCol = 0;
setRowAddress();
} else {
hd44780_wait_ready(false);
hd44780_outdata(c);
currentCol++;
if (currentCol > _LCD_n_columns-1) {
currentCol = 0;
currentRow++;
// se necessario scrolla
if (currentRow > _LCD_n_rows - 1) {
scroolView(down);
currentRow = _LCD_n_rows - 1;
}
}
setRowAddress();
}
return 0;
}
int set_cursor_at(uint8_t row, uint8_t col)
{
if ((row > _LCD_n_rows-1) || (col > _LCD_n_columns))
return -1;
currentRow = row;
currentCol = col;
setRowAddress();
return 0;
}
// file lcd.h
#include <stdint.h>
/*
* LCD 16 column one row
* macro to be invoked outside the main, before lcd_init ()
*/
#define LCD_16x1 uint8_t _LCD_n_columns = 16;\
uint8_t _LCD_row_address[] = { 0x0, 0x40 };\
uint8_t _LCD_n_rows = 4
/*
* LCD 20 column four row
* macro to be invoked outside the main, before lcd_init ()
*/
#define LCD_20x4 uint8_t _LCD_n_columns = 20; \
uint8_t _LCD_row_address[] = { 0x0, 0x40, 0x14, 0x54 };\
uint8_t _LCD_n_rows = 4
/*
* Initialize LCD controller. Performs a software reset.
*/
void lcd_init(void);
/*
* Send one character to the LCD.
*/
int lcd_putchar(char c, FILE *stream);
/*
* set cursor position at row and col.
* return 0 on success, -1 otherwise
*/
int set_cursor_at(uint8_t row, uint8_t col);
Il main non è cambiato ad eccezione della macro che serve a specificare quale display usare, nel mio caso
un LCD 20x4.
// use LCD 20x4
LCD_20x4;
int
main(void)
{
...
Ciao.