Stored Messages

ncurses2

Minor improvement.

/* file name = ic-7410_cw_keyboard_ncurses2.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"

void send_cw(int fd, char* message) {
  static char output [BUFSIZE] = {0xfe, 0xfe, 0x80, 0x00, 0x17};
  char *p;
  int  count;

  count = 0; p=output+5;
  while(*p++ = *message++) {
    count++;
    if(count == 30) { /* message can not exceed 30 chars */
      *p = 0xfd; /* IC-7410 postamble */
      write(fd, output, 5+count+1);
      count=0; p=output+5;
    }
  }
  if(count) {
    *(--p) = 0xfd; /* replace zero with IC-7410 postamble */
    write(fd, &output, 5+count+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) {
  int fd, c, i, nread, count=0;
  int nrow, ncol, row=0, col=0;
  char message[BUFSIZE], output [BUFSIZE];
  struct termios oldtio;

  initscr();
  raw();
  noecho();
  keypad(stdscr, TRUE);
  scrollok(stdscr, TRUE);
  start_color();
  init_pair(1, COLOR_RED   , COLOR_BLACK);
  init_pair(2, COLOR_YELLOW, COLOR_BLACK);
  attron(COLOR_PAIR(1)); printw("CW keyboard.. n"); attroff(COLOR_PAIR(1));
  getmaxyx(stdscr,nrow,ncol);
  row=2; col=0; move(row, col);
  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 ( (c=getch()) != 0x04) { /* EOT */
    switch (c) {
      case KEY_F(1):
        send_cw(fd, "cq de jh1ood k ");
        attron(COLOR_PAIR(2));
        printw("CQ DE JH1OOD K ");
        attroff(COLOR_PAIR(2));
        getyx(stdscr, row, col); move(row, col);
        break;
      case KEY_F(2):
        send_cw(fd, "qrz? de jh1ood k ");
        attron(COLOR_PAIR(2));
        printw("QRZ? DE JH1OOD K ");
        attroff(COLOR_PAIR(2));
        getyx(stdscr, row, col); move(row, col);
        break;
      case KEY_BACKSPACE:
        count--; col--; mvaddch(row, col, ' '); move(row, col);
        break;
      case 0x0a: /* LF */
        row++; col = 0;
        if(row >= nrow) {wscrl(stdscr,1);row--;}
        move(row, col);
        break;
      default:
        mvaddch(row, col, c);
        col++;
        message[count++] = c;
        break;
    }

    if(col >= ncol-10 && c==' ' || col == ncol){
      row++; col= 0;
      if(row >= nrow) {wscrl(stdscr,1);row--;}
      move(row, col);
    }

    if(c == ' ' || c == '.' || c == ',' || c == 0x0a) {
      message[count] = 0;
      count=0;
      send_cw(fd, message);
    }
  }
  tcsetattr(fd, TCSANOW, &oldtio);
  endwin();
  return EXIT_SUCCESS;
}

Leave a Reply

Your email address will not be published. Required fields are marked *