/*
 * $Id: sj.c,v 1.1 2010/10/19 05:20:45 bsd Exp $
 *
 */

/*
 * Author : Brian Dean, bsd@bdmicro.com
 * Date   : 2 March, 2004
 */

/*
 * Magnevation SpeakJet Speech and Sound Sythesizer Module
 * 
 * This directory contains a complete test program for demonstrating the
 * use of the Magnevation SpeakJet speach chip with the BDMICRO MAVRIC
 * and MAVRIC-II microcontroller boards.  Feel free to copy the relevent
 * function definitions into your own applications and to customize them
 * for your needs.
 * 
 * The code is written in C using the GNU GCC C Compiler.  A Makefile is
 * provided which is known to work correctly on the FreeBSD Unix
 * operating system.
 * 
 * Configuration:
 *  
 *   Hook up the SpeakJet according to its data sheet.  Attach the
 *   SpeakJet output to an amplifier.  Attach your MAVRIC or MAVRIC-II
 *   PORTD3 to SpeakJet pin 11 (Serial TTL Data).  It is important to use
 *   PORTD3 and not the MAVRIC/MAVRIC-II UART1 TX pin because the UART1
 *   TX pin is a level shifted RS232 voltage, while the SpeakJet utilizes
 *   just the TTL level.  PORTD3 on the MAVRIC/MAVRIC-II is the TTL level
 *   RS232 output pin which is the one to use with the SpeakJet.
 * 
 *   When the MAVRIC/MAVRIC-II comes out of reset, it will speak the
 *   phrase: "Welcome to the BDMICRO MAVRIC SpeakJet Test Program".
 * 
 * For more information about the MAVRIC and MAVRIC-II microcontroller
 * boards, see:
 * 
 *   http://www.bdmicro.com/
 * 
 * For more information about the Magnevation SpeakJet Speech chip, see:
 * 
 *   http://www.speakjet.com/
 * 
 *
 */

#include <stdio.h>
#include <inttypes.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>

#define WP 5
uint8_t announce[] = {
/* welcome  */ 145, 131, 145, 4, 195, 136, 140, WP,
/* to       */ 192, 162, WP,
/* the      */ 169, 134, WP,
/* bdmicro  */ 170, 128, 0, 174, 128, 0, 140, 155, 194, 148, 137, WP,
/* mavric   */ 140, 154, 166, 148, 129, 194, WP,
/* speakjet */ 187, 198, 128, 194, 4, 165, 131, 191, WP,
/* test     */ 191, 131, 187, 191, WP,
/* program  */ 199, 148, 137, 179, 148, 132, 140
};

#define N_ANNC sizeof(announce)

#define TTY "/dev/cu.USA19H2b24P1.1"

int main(int argc, char * argv[])
{
  int fd;
  int i;
  unsigned int code;
  uint8_t b;
  unsigned int bytes=0;

  fd = open(TTY, O_WRONLY);
  if (fd < 0) {
    fprintf(stderr, "error opening %s: %s\n",
            TTY, strerror(errno));
    exit(1);
  }

  while (fscanf(stdin, " %d", &code) == 1) {
    b = code;
    //printf("%d ", code);
    write(fd, &b, 1);
    bytes++;
  }
    

  if (!bytes) {
    for (i=0; i<N_ANNC; i++)
      write(fd, &announce[i], 1);
  }
  
}


