Ncurses

ncurses

Ncurses is a library of functions that manages an application’s display on character-cell terminals.

[1] http://www.tldp.org/HOWTO/NCURSES-Programming-HOWTO/index.html

[2] http://invisible-island.net/ncurses/ncurses-intro.html

Another version of CW keyboard program using ncurses.

/* file name = ic-7410_cw_keyboard_ncurses.c */
#include <ncurses.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <string.h>
#include <fcntl.h>
#include <time.h>
#include <sys/wait.h>
#define BUFSIZE 4096
#define BAUDRATE B19200
#define MYRIG "/dev/ttyUSB0"

struct termios saved_attributes;
static int fd = -1;

void serial_init(int fd) {
struct termios tio;
memset(&tio, 0, sizeof(tio));
tio.c_cflag = CS8 | CLOCAL | CREAD;
tio.c_cc[VTIME] = 0;
tio.c_cc[VEOL] = 0xfd; /* IC-7410 postamble */
tio.c_lflag = ICANON;
tio.c_iflag = IGNPAR | ICRNL;
cfsetispeed(&tio, BAUDRATE);
cfsetospeed(&tio, BAUDRATE);
tcsetattr(fd, TCSANOW, &tio);
}

int main (void) {
char c;
int i, nread, count=0, outputcount;
char message[BUFSIZE];
char output [BUFSIZE];
struct termios oldtio, newtio;
int nrow, ncol, row=0, col=0;

initscr();
raw();
noecho();
getmaxyx(stdscr,nrow,ncol);
start_color();
init_pair(1, COLOR_RED, COLOR_BLACK);
attron(COLOR_PAIR(1));
printw("CW keyboard.. n");
attroff(COLOR_PAIR(1));
row=1; col=0;
scrollok(stdscr, TRUE);
refresh();

fd = open(MYRIG, O_RDWR | O_NOCTTY);
if (fd < 0) {
fprintf(stderr,"Error: can not open %s n", MYRIG);
return (-1);
}
tcgetattr(fd, &oldtio);

serial_init(fd);

while (1) {
c=getch();
mvaddch(row,col,c);
col++;
if(col >= ncol*0.8 && c==' '){
row++; col=0;
}

if(c == 0x0a || c == 0x0d) {
row++; col=0;
}

if(row == nrow) {
row=nrow-1;
}

message[count++]=c;
if (c == '\004') { /* C-d */
break;
} else {
if(c == 0x07 || c == 0x7f) { /* backspace or delete */
count-=2; /* forget both backspace and char */
col -=2;
}
if(c == ' ' || c == '.' || c == '\012') { /* newline */
output[0] = 0xfe; /* IC-7410 preamble */
output[1] = 0xfe; /* IC-7410 preamble */
output[2] = 0x80; /* IC-7410 CI-V address */
output[3] = 0x00; /* my PC address (any) */
output[4] = 0x17; /* IC-7410 command for CW message */
for(i=0;i < count;i++) {
output[5+i] = message[i];
}
output[5+count] = 0xfd; /* IC-7410 postamble */
outputcount = count+6; /* total length = CW message + (5+1) */
write(fd, &output, outputcount);
count=0;
}
}
}
tcsetattr(fd, TCSANOW, &oldtio);
endwin();
return EXIT_SUCCESS;
}