Skip to content

Commit

Permalink
working with ascii and not glyph (I'm not sure why my screen changed …
Browse files Browse the repository at this point in the history
…mode)
  • Loading branch information
jeromelebel committed Mar 17, 2013
1 parent 70e7eda commit 7bbc1bb
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 8 deletions.
50 changes: 48 additions & 2 deletions hd44780.cpp
Expand Up @@ -12,6 +12,19 @@
#define SPI_MAX_SPEED 10000 /// maximum speed is 1 Mhz
#define MAX_COLUMN 20

#define ENGLISH_JAPANESE_FONT 0b00
#define WESTERN_EUROPEAN_1_FONT 0b01
#define ENGLISH_RUSSIAN_FONT 0b10
#define WESTERN_EUROPEAN_2_FONT 0b11

#define CLEAR_COMMAND 0b1
#define HOME_COMMAND 0b10
#define CURSOR_MODE_COMMAND(inc_dec, shift) (0b100 | ((0b1 & inc_dec) << 1) | (0b1 & shift))
#define SCREEN_CURSOR_COMMAND(screen_on_off, cursor_on_off, blinking_on_off) (0b1000 | ((0b1 & screen_on_off) << 2) | ((0b1 & cursor_on_off) << 1) | (0b1 & blinking_on_off))
#define FONT_COMMAND(font) (0b111000 | (0b11 & font))
#define MOVE_TO_DDRAM_COMMAND(address) (0b10000000 | (0b1111111 & address))
#define MOVE_TO_CGRAM_COMMAND(address) (0b1000000 | (0b111111 & address))

HD44780::HD44780(void)
{
_font = &font1;
Expand Down Expand Up @@ -63,7 +76,7 @@ void HD44780::closeDevice(void)

int HD44780::initDevice(int line, int row)
{
unsigned short initialization[] = { 0b111001, 0b1000, 0b10, 0b110, 0b10, 0b1100, 0b1111, 0b11100, 0b10 };
unsigned short initialization[] = { FONT_COMMAND(ENGLISH_JAPANESE_FONT), SCREEN_CURSOR_COMMAND(0, 0, 0), CLEAR_COMMAND, CURSOR_MODE_COMMAND(1, 0), HOME_COMMAND, SCREEN_CURSOR_COMMAND(1, 0, 0) };
this->sendCommand(initialization, sizeof(initialization));
_line = line;
_row = row;
Expand All @@ -82,14 +95,18 @@ int HD44780::sendCommand(unsigned short *commands, unsigned int length, unsigned
spi.speed_hz = SPI_MAX_SPEED;
spi.bits_per_word = SPI_BITS_PER_WORD;

/* printf("command size %d %d\n", length, delay);
for (unsigned int ii = 0; ii < length; ii += 2) {
printf(" command %d %d '%c'\n", ((unsigned char *)commands)[ii], ((unsigned char *)commands)[ii + 1], (((unsigned char *)commands)[ii] >= 32) ? ((unsigned char *)commands)[ii] : '-');
}*/
if(ret = ioctl (_spi_fd, SPI_IOC_MESSAGE(1), &spi) < 0){
printf("ERROR while sending\n");
return -1;
}
return 0;
}

void HD44780::setCursorPosition(int row)
void HD44780::setGlyphPosition(int row)
{
unsigned char position;
unsigned short command;
Expand All @@ -102,6 +119,26 @@ void HD44780::setCursorPosition(int row)
this->sendCommand(&command, 2);
}

void HD44780::moveToLine(unsigned int line)
{
unsigned short command;

if (line == 0) {
command = HOME_COMMAND;
} else {
command = MOVE_TO_DDRAM_COMMAND(0xC0);
}
this->sendCommand(&command, sizeof(command));
}

void HD44780::showCursor(unsigned int show, unsigned int blinking)
{
unsigned short command;

command = SCREEN_CURSOR_COMMAND(1, show, blinking);
this->sendCommand(&command, sizeof(command));
}

void HD44780::print(const char *string)
{
this->print((const unsigned char *)string);
Expand All @@ -110,12 +147,21 @@ void HD44780::print(const char *string)
void HD44780::print(const unsigned char *string)
{
while (string[0]) {
//this->printGlyphCharacter(string[0]);
this->printCharacter(string[0]);
string++;
}
}

void HD44780::printCharacter(unsigned char character)
{
unsigned short command;

command = 0x200 | character;
this->sendCommand(&command, sizeof(command));
}

void HD44780::printGlyphCharacter(unsigned char character)
{
unsigned char ii;
unsigned char glyph[CHARACTERE_WIDTH];
Expand Down
5 changes: 4 additions & 1 deletion hd44780.h
Expand Up @@ -21,9 +21,12 @@ class HD44780
int initDevice(int line, int row);
void closeDevice(void);

void setCursorPosition(int row);
void showCursor(unsigned int show, unsigned int blinking);
void setGlyphPosition(int row);
void print(const unsigned char *string);
void print(const char *string);
void printCharacter(unsigned char character);
void printGlyphCharacter(unsigned char character);
void printGlyph(unsigned char glyph[CHARACTERE_WIDTH]);
void moveToLine(unsigned int line);
};
21 changes: 16 additions & 5 deletions main.cpp
Expand Up @@ -21,13 +21,24 @@ int main(int argc, char* argv[])
printf("can't open device\n");
return 1;
}
if (display->initDevice(2, 16) != 0) {
printf("can't init device\n");
return 1;
}
ii = 1;
while (argc > ii) {
display->print(argv[ii]);
if (strcmp(argv[ii], "--init") == 0) {
if (display->initDevice(2, 16) != 0) {
printf("can't init device\n");
return 1;
}
} else if (strcmp(argv[ii], "--secondline") == 0) {
display->moveToLine(1);
} else if (strcmp(argv[ii], "--firstline") == 0) {
display->moveToLine(0);
} else if (strcmp(argv[ii], "--showcursor") == 0) {
display->showCursor(1, 1);
} else if (strcmp(argv[ii], "--hidecursor") == 0) {
display->showCursor(0, 0);
} else {
display->print(argv[ii]);
}
ii++;
}
return 0;
Expand Down

0 comments on commit 7bbc1bb

Please sign in to comment.