view main.cc @ 0:6dfcd8e5b8df

first release
author Frank Kingswood <frank@kingswood-consulting.co.uk>
date Tue, 25 Mar 2014 20:31:00 +0000
parents
children a70e0a8ac73e
line wrap: on
line source
1 /* #------------------------------------------------------------------------#
2 | |
3 | main.cc |
4 | |
5 | USB-GPIB startup and main loop. |
6 | |
7 | Based in part on Simple example for Teensy USB Development Board. |
8 | http://www.pjrc.com/teensy/ Copyright (c) 2008 PJRC.COM, LLC |
9 | |
10 | Copyright 2014, Frank A. Kingswood, www.kingswood-consulting.co.uk |
11 | |
12 #------------------------------------------------------------------------# */
14 #include <avr/io.h>
15 #include <avr/pgmspace.h>
16 #include <stdint.h>
17 #include <util/delay.h>
18 #include "usb_serial.h"
19 #include "serial.h"
20 #include "gpib.h"
22 #define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
24 uint8_t recv_str(char *buf, uint8_t size);
25 const UsbSerial __attribute__((__progmem__)) USB;
28 /* -------------------------------------------------------------------------- */
30 void Version();
31 int16_t Command(char *);
33 int main(void)
34 {
35 uint8_t n;
36 static char buf[100];
38 // set for 16 MHz clock, and turn on the LED
39 CPU_PRESCALE(0);
41 GPIB.Initialize();
42 GPIB.LED(true);
43 Serial.Initialize();
45 // initialize the USB, and then wait for the host
46 // to set configuration. If the Teensy is powered
47 // without a PC connected to the USB port, this
48 // will wait forever.
49 USB.Initialize();
50 while (!usb_configured())
51 { }
52 _delay_ms(1000);
54 while (1)
55 {
56 // wait for the user to run their terminal emulator program
57 // which sets DTR to indicate it is ready to receive.
58 while (!(usb_serial_get_control() & USB_SERIAL_DTR))
59 { }
61 // discard anything that was received prior. Sometimes the
62 // operating system or other software will send a modem
63 // "AT command", which can still be buffered.
64 usb_serial_flush_input();
66 // print a nice welcome message
67 Version();
69 // state
70 bool DirectMode=false;
72 // and then listen for commands and process them
73 while(1)
74 {
75 //GPIB.Report();
76 USB.Send(ROMS("++"));
78 GPIB.LED(false);
80 n = recv_str(buf, sizeof(buf));
81 if(n == 255)
82 break;
83 buf[n]=0;
85 GPIB.LED(true);
87 if(n>3 && buf[0]=='+' && buf[1]=='+')
88 {
89 USB.Send();
90 Command(buf+2);
91 }
92 else if(!DirectMode)
93 {
94 USB.Send();
95 Command(buf);
96 }
97 else
98 {
99 // send GPIB
100 }
101 }
102 }
103 }
105 // Receive a string from the USB serial port. The string is stored
106 // in the buffer and this function will not exceed the buffer size.
107 // A carriage return or newline completes the string, and is not
108 // stored into the buffer.
109 // The return value is the number of characters received, or 255 if
110 // the virtual serial connection was closed while waiting.
111 //
112 uint8_t recv_str(char *buf, uint8_t size)
113 {
114 int16_t r;
115 uint8_t count=0;
117 while(count < size)
118 {
119 r = usb_serial_getchar();
120 if(r==-1)
121 {
122 if(!usb_configured() || !(usb_serial_get_control() & USB_SERIAL_DTR))
123 {
124 // user no longer connected
125 return 255;
126 }
127 // just a normal timeout, keep waiting
128 }
129 else
130 {
131 if(r=='\r' || r=='\n')
132 return count;
133 if(r=='\b' && count>0)
134 { --buf;
135 --count;
136 usb_serial_putchar(r);
137 usb_serial_putchar(' ');
138 usb_serial_putchar(r);
139 }
140 else if(r >= ' ' && r <= '~')
141 {
142 *buf++ = r;
143 usb_serial_putchar(r);
144 count++;
145 }
146 }
147 }
148 return count;
149 }
151 /* ----- EOF main.cc ----- */