Simplified

ncurses3

Yet another minor improvement. I suppose this version of the code is very readable. It might be better if I could make it to be less than 100 lines. Adding bells and whistles is relatively easy, the difficult part is making it simple.

/* file name = ic-7410_cw_keyboard_ncurses3.c */
/* % gcc source_code.c -lncurses */
#include <ncurses.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <string.h>
#include <fcntl.h>
#define  BUFSIZE  4096
#define  BAUDRATE B19200
#define  MYRIG    "/dev/ttyUSB0"

int fd=-1;

void send_cw(char* text) {       /* IC-7410 send CW */
  static char output [BUFSIZE] = {0xfe, 0xfe, 0x80, 0x00, 0x17};
  char *p;
  int  count;

  count=0; p=output+5;
  while(*p++ = *text++) {
    count++;
    if(count == 30) {            /* IC-7410 max CW text length */
      *p = 0xfd;                 /* IC-7410 postamble */
      write(fd, output, 5+count+1);
      count=0; p=output+5;
    }
  }
  if(count) {
    *(--p) = 0xfd;               /* replace zero with postamble */
    write(fd, &output, 5+count+1);
  }
}

void send_stored_text(int id) {  /* use Function Keys */
static char *text_array[]={
  "cq cq cq de jh1ood jh1ood jh1ood k ",
  "qrz? de jh1ood k "};

  send_cw(text_array[id-1]); /* id=1, 2, ... */
  attron(COLOR_PAIR(2)); printw (text_array[id-1]); attroff(COLOR_PAIR(2));
}

void serial_init(void) {
  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    c, i, count=0, nrow, ncol, row=0, col=0;
  char   word[BUFSIZE];
  struct termios oldtio;

  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();

  initscr    (); /* ncurses init */
  raw        ();
  noecho     ();
  keypad     (stdscr, TRUE);
  scrollok   (stdscr, TRUE);
  getmaxyx   (stdscr,nrow,ncol);
  start_color();
  init_pair  (1, COLOR_RED   , COLOR_BLACK);
  init_pair  (2, COLOR_YELLOW, COLOR_BLACK);
  attron     (COLOR_PAIR(1)); printw("CW keyboard.."); attroff(COLOR_PAIR(1));
  row=2; col=0; move(row, col);
  refresh    ();

  while ( (c=getch()) != 0x04) { /* EOT */
    switch (c) {
      case KEY_F(1): send_stored_text(1); break;
      case KEY_F(2): send_stored_text(2); break;
      case KEY_BACKSPACE:
        if(count) {              /* only within a word */
          getyx  (stdscr, row, col);
          count--; col--;        /* only within the same line */
          mvaddch(row, col, ' ');
          move   (row, col);     /* cursor goes back */
        }
        break;
      default:
        if(isprint(c)) {
          addch(c);
          word[count++] = c;
        }
        break;
    }

    getyx(stdscr, row, col);
    if(c == 0x0a || c==' ' && col >= ncol-10 || col == ncol)
      printw("n");
    refresh();

    if(c == 0x0a || c == ' ' || c == '.' || c == ',') {
      word[count] = 0; count=0;
      send_cw(word);
    }
  }

  tcsetattr(fd, TCSANOW, &oldtio); /* reset serial terminal */
  endwin   ();                     /* reset ncurses */
  return EXIT_SUCCESS;
}

Leave a Reply

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