~vbursian/research-assistant/intervers

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
////////////////////////////////////////////////////////////////////////////////
/*! @file CommandParser.c  MSDN command parser.
- Part of the MSDN firmware.
- Part of the Research Assistant project.
- Copyright(C) 2016, Viktor E. Bursian, St.Petersburg, Russia.
                     Viktor.Bursian@mail.ioffe.ru
*///////////////////////////////////////////////////////////////////////////////
#include "CommandParser.h"
#include "Microsin_USB_IO.h"
#include "zakavych.h"

const char * const  DeviceName = ZAKAVYCH(MSDN_SHORTNAME);

const char * const  BuildDate = __DATE__;


void  ProcessReceivedByte (const int16_t  received_byte)
{
  static uint8_t              ParserStage = 0;
                              //!< Parser stages:
                              //!< - 0 - command char expected;
                              //!< - 1 to 8 - argument digit or [CR]LF expected;
                              //!< - 9 -    got command and 8 digits,
                              //!<          [CR]LF expected;
                              //!< - 0xD0 - got command and CR, LF expected;
                              //!< - 0xDn - got command, n digits and CR,
                              //!<          LF expected;
                              //!< - 0xEn - got command, n digits and [CR]LF,
                              //!<          to be executed;
                              //!< - 0xF0 - sintax error encounted, LF expected.
                              //!< - 0xFF - sintax error encounted, got LF,
                              //!<          now, error to be reported.
  static char                 Command = ' ';
  static uint8_t              Argument8  = 0;
  static uint16_t             Argument16 = 0;
  static uint32_t             Argument32 = 0;
  int8_t                      ErrNo;

  switch( ParserStage ){
    case  0:
        if( (0x21 <= received_byte) && (received_byte <= 0x7F) ){
          Command = received_byte;
          Argument8 = 0;
          ParserStage = 1;
        }else if( received_byte == 0x0A ){
          ParserStage = 0xFF;
        }else{
          ParserStage = 0xF0;
        }
        break;
    case  1:
    case  2:
        if( received_byte == 0x0D ){
          ParserStage += 0xCF; // -1 + 0xD0
        }else if( received_byte == 0x0A ){
          ParserStage += 0xDF; // -1 + 0xE0
        }else{
          ParserStage++;
          if ((received_byte >= 0x41) && (received_byte <= 0x46) ){
            Argument8 = (Argument8 << 4) + (received_byte - 0x40 + 0x09);
          }else if( (received_byte >= 0x61) && (received_byte <= 0x66) ){
            Argument8 = (Argument8 << 4) + (received_byte - 0x60 + 0x09);
          }else if( (received_byte >= 0x30) && (received_byte <= 0x39) ){
            Argument8 = (Argument8 << 4) + (received_byte - 0x30);
          }else{
            ParserStage = 0xF0;
          }
        }
        break;
    case  3:
        Argument16 = Argument8;
    case  4:
        if( received_byte == 0x0D ){
          ParserStage += 0xCF; // -1 + 0xD0
        }else if( received_byte == 0x0A ){
          ParserStage += 0xDF; // -1 + 0xE0
        }else{
          ParserStage++;
          if ((received_byte >= 0x41) && (received_byte <= 0x46) ){
            Argument16 = (Argument16 << 4) + (received_byte - 0x40 + 0x09);
          }else if( (received_byte >= 0x61) && (received_byte <= 0x66) ){
            Argument16 = (Argument16 << 4) + (received_byte - 0x60 + 0x09);
          }else if( (received_byte >= 0x30) && (received_byte <= 0x39) ){
            Argument16 = (Argument16 << 4) + (received_byte - 0x30);
          }else{
            ParserStage = 0xF0;
          }
        }
        break;
    case  5:
        Argument32 = Argument16;
    case  6:
    case  7:
    case  8:
        if( received_byte == 0x0D ){
          ParserStage += 0xCF; // -1 + 0xD0
        }else if( received_byte == 0x0A ){
          ParserStage += 0xDF; // -1 + 0xE0
        }else{
          ParserStage++;
          if ((received_byte >= 0x41) && (received_byte <= 0x46) ){
            Argument32 = (Argument32 << 4) + (received_byte - 0x40 + 0x09);
          }else if( (received_byte >= 0x61) && (received_byte <= 0x66) ){
            Argument32 = (Argument32 << 4) + (received_byte - 0x60 + 0x09);
          }else if( (received_byte >= 0x30) && (received_byte <= 0x39) ){
            Argument32 = (Argument32 << 4) + (received_byte - 0x30);
          }else{
            ParserStage = 0xF0;
          }
        }
        break;
    case  9:
        if( received_byte == 0x0D ){
          ParserStage = 0xD8;
        }else if( received_byte == 0x0A ){
          ParserStage = 0xE8;
        }else{
          ParserStage = 0xF0;
        }
        break;
    case 0xD0:
    case 0xD1:
    case 0xD2:
    case 0xD3:
    case 0xD4:
    case 0xD5:
    case 0xD6:
    case 0xD7:
    case 0xD8:
        if( received_byte == 0x0A ){
          ParserStage += 0x10;
        }else{
          ParserStage = 0xF0;
        }
        break;
    case 0xF0:
        if( received_byte == 0x0A ){
          ParserStage = 0xFF;
        }
        break;
  }
  switch( ParserStage ){
    case  0xE0:
        if( Command == '?' ){
          SendCharToUSB('@');
          SendStringToUSB(DeviceName);
          Send_EOL_ToUSB();
        }else if( Command == ':' ){
          SendCharToUSB(':');
          SendStringToUSB(BuildDate);
          Send_EOL_ToUSB();
        }else if( (ErrNo = ExecuteCommand0(Command)) ){
          SendCharToUSB('#');
          SendCharToUSB(0x30+ErrNo);
          Send_EOL_ToUSB();
        }
        ParserStage = 0;
        break;
    case  0xE1:
    case  0xE2:
        if( (ErrNo = ExecuteCommand2(Command,Argument8)) ){
          SendCharToUSB('#');
          SendCharToUSB(0x30+ErrNo);
          Send_EOL_ToUSB();
        }
        ParserStage = 0;
        break;
    case  0xE3:
    case  0xE4:
        if( (ErrNo = ExecuteCommand4(Command,Argument16)) ){
          SendCharToUSB('#');
          SendCharToUSB(0x30+ErrNo);
          Send_EOL_ToUSB();
        }
        ParserStage = 0;
        break;
    case  0xE5:
    case  0xE6:
    case  0xE7:
    case  0xE8:
        if( (ErrNo = ExecuteCommand8(Command,Argument32)) ){
          SendCharToUSB('#');
          SendCharToUSB(0x30+ErrNo);
          Send_EOL_ToUSB();
        }
        ParserStage = 0;
        break;
    case  0xFF:
        SendCharToUSB('#');
        Send_EOL_ToUSB();
        ParserStage = 0;
        break;
  }
}