@@ -21,15 +21,15 @@ extern WebUSB WebUSBSerial;
2121#endif
2222
2323// generic JSON error codes
24- #define ERR_UNEXP_STATE 1 // unexpected state
25- #define ERR_EXP_OBJ 2 // expecting object
26- #define ERR_EXP_STR 3 // expecting string
27- #define ERR_EXP_SEP 4 // expecting ':'
28- #define ERR_UNEXP_SUB 5 // unexpected substate
29- #define ERR_UNEXP_ESTR 6 // unexpected state at end of string
30- #define ERR_UNEXP_CONT 7 // unexpected continuation
31- #define ERR_UNEXP_VAL 8 // unexpected value
32- #define ERR_UNEXP_ACONT 9 // unexpected array continuation
24+ #define ERR_UNEXP_STATE 1 // unexpected state
25+ #define ERR_EXP_OBJ 2 // expecting object
26+ #define ERR_EXP_STR 3 // expecting string
27+ #define ERR_EXP_SEP 4 // expecting ':'
28+ #define ERR_UNEXP_SUB 5 // unexpected substate
29+ #define ERR_UNEXP_ESTR 6 // unexpected state at end of string
30+ #define ERR_UNEXP_CONT 7 // unexpected continuation
31+ #define ERR_UNEXP_VAL 8 // unexpected value
32+ #define ERR_UNEXP_ACONT 9 // unexpected array continuation
3333
3434// ftDuino specific errors
3535#define ERR_UNK_CMD 10 // unknown command
@@ -40,6 +40,7 @@ extern WebUSB WebUSBSerial;
4040#define ERR_INV_MODE 15 // mode invalid for port
4141#define ERR_INT 16 // internal error
4242#define ERR_ILL_REQ 17 // illegal get request
43+ #define ERR_ILL_TYPE 18 // illegal (counter) type specification
4344
4445void JsonParser::reply_error (char id) {
4546#ifdef ARDUINO
@@ -79,6 +80,7 @@ void JsonParser::cmd_reset(void) {
7980 cmd = CMD_NONE;
8081 mode = MODE_NONE;
8182 parm = PARM_NONE;
83+ type = TYPE_NONE;
8284 req = REQ_NONE;
8385 port.type = port::PORT_NONE;
8486 value.valid = false ;
@@ -94,6 +96,12 @@ void JsonParser::reset(void) {
9496 cmd_reset ();
9597
9698#ifdef ARDUINO
99+ // all counter inputs trigger on falling edge and clear them
100+ for (uint8_t c=0 ;c<4 ;c++) {
101+ ftduino.counter_set_mode (Ftduino::C1+c, Ftduino::C_EDGE_FALLING);
102+ ftduino.counter_clear (Ftduino::C1+c);
103+ }
104+
97105 // reset all ftduino ports
98106 digitalWrite (LED_BUILTIN, LOW);
99107 for (uint8_t i=0 ;i<8 ;i++)
@@ -143,6 +151,8 @@ void JsonParser::check_name(void) {
143151 parm = PARM_VALUE;
144152 else if (strcmp (substate_value.value .str , " mode" ) == 0 )
145153 parm = PARM_MODE;
154+ else if (strcmp (substate_value.value .str , " type" ) == 0 )
155+ parm = PARM_TYPE;
146156 else
147157 reply_error (ERR_UNK_PARM);
148158 }
@@ -161,6 +171,18 @@ void JsonParser::check_value(void) {
161171 lowercase_str (); // value string are all case insensitive
162172
163173 if (cmd == CMD_GET) {
174+
175+ if ((depth == 1 ) && parm == PARM_TYPE) {
176+ printf (" PARM TYPE %s\n " , substate_value.value .str );
177+
178+ if (startsWith (substate_value.value .str , " state" ))
179+ type = TYPE_STATE;
180+ else if (startsWith (substate_value.value .str , " counter" ))
181+ type = TYPE_COUNTER;
182+ else
183+ reply_error (ERR_ILL_TYPE);
184+ }
185+
164186 if ((depth == 1 ) && (parm == PARM_PORT)) {
165187 port.type = port::PORT_NONE;
166188 port.index = substate_value.value .str [1 ] -' 0' - 1 ;
@@ -345,6 +367,26 @@ void JsonParser::cmd_complete(void) {
345367#endif
346368 }
347369
370+ if (port.type == port::PORT_C) {
371+ if ((type == TYPE_NONE) || (type == TYPE_STATE)) {
372+ #ifdef ARDUINO
373+ uint16_t v = ftduino.counter_get_state (Ftduino::C1 + port.index );
374+ char port_name[3 ] = { ' C' , (char )(' 1' + port.index ), 0 };
375+ reply_value (port_name, true , v);
376+ #else
377+ printf (" get counter state %d\n " , port.index );
378+ #endif
379+ } else if (type == TYPE_COUNTER) {
380+ #ifdef ARDUINO
381+ uint16_t v = ftduino.counter_get (Ftduino::C1 + port.index );
382+ char port_name[3 ] = { ' C' , (char )(' 1' + port.index ), 0 };
383+ reply_value (port_name, false , v);
384+ #else
385+ printf (" get counter value %d\n " , port.index );
386+ #endif
387+ }
388+ }
389+
348390 if (req == REQ_DEVS) {
349391#ifdef ARDUINO
350392 // currently only one master is supported
@@ -363,7 +405,7 @@ void JsonParser::cmd_complete(void) {
363405 if (req == REQ_VER) {
364406#ifdef ARDUINO
365407 // currently only one master is supported
366- Serial.print (" { \" version\" : \" 0.9.0 \" }" );
408+ Serial.print (" { \" version\" : \" 0.9.1 \" }" );
367409 Serial.flush ();
368410#else
369411 printf (" Version request\n " );
@@ -389,6 +431,14 @@ void JsonParser::cmd_complete(void) {
389431 }
390432 break ;
391433
434+ case port::PORT_C:
435+ #ifdef ARDUINO
436+ ftduino.counter_clear (Ftduino::C1+port.index );
437+ #else
438+ printf (" setting counter port\n " );
439+ #endif
440+ break ;
441+
392442 // act according to output set commands
393443 case port::PORT_O:
394444 if (mode != MODE_NONE) {
@@ -439,7 +489,7 @@ void JsonParser::cmd_complete(void) {
439489 reply_error (ERR_INV_MODE);
440490 }
441491 break ;
442-
492+
443493 // act according to output set commands
444494 case port::PORT_M:
445495 if (mode != MODE_NONE) {
0 commit comments