Geodesic Light Dome Controller Program

Dependencies:   mbed

serial.c

Committer:
lolpcc
Date:
2010-03-24
Revision:
1:dc58f0b0eeec
Parent:
0:a7af7ec8b12f

File content as of revision 1:dc58f0b0eeec:

#include "mbed.h"
#include "useful.h"

Serial rs232(p13, p14);  // tx, rx
/******************************************/
/*                                        */
/*  Test the RS232 interface              */
/*  Should send and recieve data on the   */
/*  serial port, send a string, and get   */
/*  back what is returned from the RX     */
/*                                        */
/******************************************/
void rs232_opener(void)
{
    rs232.baud(9600);
    rs232.printf("\n\r");
    rs232.printf("Dome Controler\n\r");
    rs232.printf("\n\rCMD > ");
}

void rs232_output_string(char *buf)
{
    int a = 0;
    while(a != strlen(buf)){
        rs232.putc(buf[a]);
        if(buf[a]=='\n')
            rs232.putc('\r');
        a++;
    }
}

int rs232_readable(void)
{
    return(rs232.readable());
}

int rs232_gets(char *s,int len) 
{
    char   c;
    int    cnt=0;

    while ((c = rs232.getc()) != 0) {
        if ((c == 0x0a) || (c==0x0d)) {
            rs232.printf("\n\r");
            *s++ = '\0';
            return(cnt);    /* Return length */
        } else if (c==0x7f) {  /* Delete */
            rs232.putc(0x08);
            rs232.putc(0x20);
            rs232.putc(0x08);
            cnt--;
            *s--;
        } else if (c==0x08) {  /* BS */
            rs232.putc(0x08);
            rs232.putc(0x20);
            rs232.putc(0x08);
            cnt--;
            *s--;
        } else if (c==025) {  /* CTRL-U */
            while (cnt!=0) {
                rs232.putc(0x08);
                rs232.putc(0x20);
                rs232.putc(0x08);
                cnt--;
                *s--;
            }
        } else {
            *s++ = c;
            rs232.putc(c);
            cnt++;
        }
    }
    return(cnt);
}

/******************************************/
/*                                        */
/* Set the baud rate of the serial line   */
/* from the command line input            */
/*                                        */
/******************************************/
#define BAUD_NUMBER 10
void baud_rate(int c, char **a)
{
    int    b,cnt;
    int    v[BAUD_NUMBER] = {300,600,1200,2400,4800,9600,19200,38400,57600,15200};
    
    b = atoi(a[1]);
    cnt = 0;
    while(cnt < BAUD_NUMBER){
        if(v[cnt]==b){    
            rs232.baud(b);
            return;
        }
        cnt++;
    }
    cnt=0;
    lprintf("Select a speed from ");
    while(cnt < BAUD_NUMBER){
        lprintf("%d ",v[cnt]);
        cnt++;
    }
    lprintf("\n\r");
}

void serial_test(int c, char **a)
{
    char    ch;
    
    rs232_output_string("Serial Test Routine\n\r");
    
    while(1){
        ch = rs232.getc();
        printf("Got %c\n\r",ch);
        if(ch=='q')
            return;
    }
}