123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- #include <stdio.h>
- #include <time.h>
- #include <string.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #include <termios.h>
- #include <unistd.h>
- #include "sleep_us.h"
- int pert_fd;
- unsigned int char_delay;
- const static unsigned char rowoffset[4] =
- { 0x80, 0x80+0x40, 0x80 + 0x14, 0x80 + 0x40 + 0x14 };
- void wrtch(unsigned char c) {
- if (write(pert_fd,&c,1) != 1)
-
-
- fprintf(stderr,"Error writing to pert: %d\n",c);
- tcdrain(pert_fd);
- sleep_us(char_delay);
- }
- void putcode(unsigned char code) {
- wrtch((char)0xfe);
- wrtch(code);
- sleep_us(char_delay);
- }
- void wrt(char *p) {
- while(*p) {
- wrtch(*p);
- p++;
- }
- }
- void wrtln(int row, char *p) {
- putcode(rowoffset[row]);
- wrt(p);
- }
- void display_init(char *device_name) {
- struct termios term;
- pert_fd = open(device_name, O_WRONLY | O_NONBLOCK);
- if (pert_fd < 0) {
- fprintf(stderr, "Cannot open device %s for writing !\n", device_name);
- exit(EXIT_FAILURE);
- }
-
- if (tcgetattr(pert_fd,&term) < 0) {
- fprintf(stderr,"Unable to get terminal attributes\n");
- exit(EXIT_FAILURE);
- }
-
- term.c_cflag = CRTSCTS | CS8 | CLOCAL | CREAD;
- term.c_iflag = ICRNL | IGNPAR;
- term.c_oflag = 0;
- term.c_lflag = 0;
- term.c_cc[VMIN] = 1;
- term.c_cc[CTIME] = 0;
- cfsetispeed(&term,B38400);
- cfsetospeed(&term,B38400);
-
- tcflush(pert_fd,TCIFLUSH);
-
- if (tcsetattr(pert_fd,TCSANOW,&term) < 0) {
- fprintf(stderr,"Unable to set terminal attributes\n");
- exit(EXIT_FAILURE);
- }
-
- putcode(0x38);
- putcode(0x06);
- putcode(0x10);
- putcode(0x0c);
- putcode(0x01);
- }
- void display_close() {
- close(pert_fd);
- }
- void backlight(int on) {
- if (on)
- putcode(0x03);
- else
- putcode(0x02);
- }
|