Initial commit for stepper controller
[radiotelescope.git] / stepper_controller / arduino / stepper / serial_command.h
1 //
2 // Product: Serial Command/Response library for QPnano
3 // Version: 0.1
4 // Date:    25 November 2010
5 //
6 //                      +-----------------------+
7 //                      |   d e c i s i o n s   |
8 //                      +-----------------------|
9 //                      | a n d   d e s i g n s |
10 //                      +-----------------------+
11 //
12 // Copyright (C) 2010 Decisions and Designs Pty Ltd. All rights reserved.
13 //
14 // This software may be distributed and modified under the terms of the GNU
15 // General Public License version 2 (GPL) as published by the Free Software
16 // Foundation and appearing in the file GPL.TXT included in the packaging of
17 // this file. Please note that GPL Section 2[b] requires that all works based
18 // on this software must also be made publicly available under the terms of
19 // the GPL ("Copyleft").
20 //
21 // Contact information:
22 // Decisions and Designs Web site: http://www.decisions-and-designs.com.au
23 // e-mail:                         [email protected]
24 //
25
26 #ifndef serial_command_h
27 #define serial_command_h
28
29 #include "qpn_port.h"
30
31 // This Serial command parser and response transmitter is based on Samek's
32 // implementation of the Orthoganol Component state pattern (see PSiC/C++,
33 // Second Ed., Section 5.4). The serial parser could have been built as a
34 // library that maintains state for the parser attached to each serial port
35 // but the Orthagonal Component approach is intended to allow reuse of this
36 // machine with minimal support code in the container HSM. BSP driver code
37 // is passed the container HSM address for delivery of the received serial
38 // characters and the Serial component is passed function pointers to the
39 // serial transmission services in the BSP driver. When Serial parses a
40 // serial command (and optional arguments) it delivers a signal for that
41 // command (asynchronously) to the container's queue. The container can
42 // access the arguments and perform some action in response.
43 //   
44
45 /* Serial I/O class (public to allow access to VArgs by a container class)   */
46
47 typedef uint8_t (*SerialTransmit)(uint8_t ch);
48 typedef void    (*SerialTransmitComplete)(void);
49
50 /* for vargs */
51 // #define PACKED_VARGS 1    /* disallow leading argument spaces */
52 #define VARG_COUNT      4    /* there are four two bit ids in the uint8_t */
53 #define VARG_TYPE       0x3  /* right shifted varg type mask */
54 #define VARG_NONE       0x0
55 #define VARG_UINT       0x1
56 #define VARG_INT        0x2
57 #define VARG_DBL        0x3
58 #define VARG_NEGATIVE   0x80
59 #define VARG_HEX        0x40
60
61 /* VArg number processor errors */
62 enum {
63     V_OK = 0,
64     V_INVALID,
65     V_OVERFLOW
66 };
67
68 #define EOC 0x80
69
70 typedef struct varg_s {
71     uint8_t type;\r
72         union {\r
73                 QParam param;
74         uint32_t uint32;
75         int32_t  int32;
76         double   dbl;
77     } v;
78 } VArg;
79
80 typedef struct MenuTag {
81     char ch;
82     uint8_t idx;
83 } Menu;
84
85 typedef struct MenuSignalTag {
86     uint8_t sig;
87     uint8_t varg;
88 } MenuSignal;
89
90 typedef struct SerialCommandTag {
91     QActive super;
92     QActive *container;
93     SerialTransmit serialTransmit;
94     SerialTransmitComplete serialTransmitComplete;
95     Menu *menu;
96     Menu *parser;
97     MenuSignal *menuSignals;
98     VArg vargs[VARG_COUNT];
99     double vdiv;
100     uint8_t vidx;
101     char *txBuffer;
102     uint8_t txHead;
103     uint8_t txTail;
104     uint8_t txSize;
105 } SerialCommand;
106
107 /* Services available to the container class for SerialCommand */
108 \r
109 #define SerialCommand_init(S) QHsm_init((QHsm *)(S))\r
110
111 void SerialCommand_ctor(SerialCommand *me, QActive *container, Menu *menu,
112                         MenuSignal *menuSignals, char *txBuffer, uint8_t txSize,\r
113                                                 uint8_t port, uint32_t baud);
114
115 void serialDispatch(SerialCommand *me, QSignal sig, QParam par);
116
117 /* If a command accepted more than one varg then the container must release
118    the vargs when that signal has been processed. If not, SerialCommand will
119    fail subsequent commands when the need to refill the vargs is reached */
120 void serialReleaseVargs(SerialCommand *me);
121
122 /* Print data to the serial transmitter */
123 void serialDumpArgs(SerialCommand *me);
124 void serialChar(SerialCommand *me, char c);
125 void serialStr(SerialCommand *me, char *s);
126 void serialUnum(SerialCommand *me, uint32_t v);
127 void serialNum(SerialCommand *me, int32_t v);
128 void serialDbl(SerialCommand *me, double number, uint8_t digits);
129 void serialNACK(SerialCommand *me, uint8_t unused);
130 void serialACK(SerialCommand *me, uint8_t only);
131
132 #endif                                                  /* serial_command_h */

UCC git Repository :: git.ucc.asn.au