led_switch.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. #include <stdio.h>
  2. #define SCPI_ARGS_N_C 1
  3. #define SCPI_ARGS_N_Q 0
  4. #include "app/scpi/scpi_handler.h"
  5. #include "app/led/led.h"
  6. const uint8_t fsqvbl_CommandHandlerLED1 = 1; // IND:LED:LED1
  7. const uint8_t fsqvbl_CommandHandlerLED2 = 2; // IND:LED:LED2
  8. const uint8_t fsqvbl_CommandHandlerWRMP = 3; // IND:WRMP
  9. // -----
  10. // @argTokens, @argTypes
  11. // Declare argument parser entities
  12. // Supported arguments: 1=CHARACTER
  13. DECLARE_SCPI_ARGS_C( eScpiArg_Character );
  14. // Argument 1 Character Values allowed list / ACM Switch State
  15. DECLARE_ARGUMENT_CHARACTER_ALLOWED_LIST_EXPORT( IND_AllowedValues_SwitchState,
  16. "OFF",
  17. "ON",
  18. "GRN",
  19. "RED"
  20. );
  21. #include "app/scpi/commandHandlers/led_switch.h"
  22. #include "app/nfm/nfm_base.h"
  23. // Refer to:
  24. // [1] SCPI Specification, revision 1999.0
  25. // "Standard Commands for Programmable Instruments (SCPI), VERSION 1999.0, May 1999"
  26. // [2] Gpib Programming Tutorial, (http://g2pc1.bu.edu/~qzpeng/gpib/manual/GpibProgTut.pdf)
  27. // Electronics Group (http://www.few.vu.nl/~elec), 11 January 2000 Electronics Group
  28. // [3] IEEE 488.2 Standard, revision IEEE Std 488.2-1987 (1992)
  29. // "IEEE Standard Codes, Formats, Protocols, and Common Commands for Use With IEEE Std 488.1-1987, IEEE
  30. // =================================================================================
  31. // @fsqvbl_CommandHandlerMEASnSWITCH_group
  32. // State's virtual table
  33. static void fsqe_CommandHandlerLED_Switch_Group( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx );
  34. static void fsql_CommandHandlerLED_Switch_Group( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx );
  35. static const struct fFSeqEntry_t * fsqf_CommandHandlerLED_Switch_Group( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx, const struct fFSeqEntry_t * * pDeferredNext );
  36. const fFSeqVTable_t fsqvbl_CommandHandlerLEDSWITCH_group =
  37. {
  38. .f = fsqf_CommandHandlerLED_Switch_Group,
  39. .enter = fsqe_CommandHandlerLED_Switch_Group,
  40. .leave = fsql_CommandHandlerLED_Switch_Group
  41. };
  42. static void fsqe_CommandHandlerLED_Switch_Group( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx )
  43. {
  44. sProcessProgramDataCommonContext_t * common_ctx = ctx;
  45. common_ctx->MeasAndSwitch.idx = 0;
  46. SCPI_PARSE_ARGUMENTS( common_ctx ); (void)common_ctx->argsParserStatus; // status is modified
  47. }
  48. static void fsql_CommandHandlerLED_Switch_Group( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx )
  49. {
  50. }
  51. static const struct fFSeqEntry_t * fsqf_CommandHandlerLED_Switch_Group( const struct fFSeqEntry_t * this,
  52. tFSeqCtx_t ctx,
  53. const struct fFSeqEntry_t * * pDeferredNext )
  54. {
  55. const fFSeqEntry_t * nextstate = NULL;
  56. sProcessProgramDataCommonContext_t * common_ctx = ctx;
  57. sScpiParserContext_t * global_ctx = common_ctx->global_ctx;
  58. switch( common_ctx->event )
  59. {
  60. case eProgramData_Event_Write:
  61. {
  62. if( eScpiStatus_success != common_ctx->argsParserStatus ) // check argument parser status
  63. {
  64. common_ctx->status = eProgramDataArgumentSyntax; // parameter syntax error, caller should generate error message
  65. }
  66. else if( ! common_ctx->isQuery )
  67. {
  68. common_ctx->status = eProgramDataIllegalArgument; // forward set, illegal parameter value, caller should generate error message
  69. if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerLED1 )
  70. {
  71. // process first argument (switch state)
  72. common_ctx->SwitchState.state = SCPI_PROCESS_ARGUMENT_CHARACTER( common_ctx, IND_AllowedValues_SwitchState, 0 );
  73. }
  74. else if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerLED2 )
  75. {
  76. common_ctx->SwitchState.state = SCPI_PROCESS_ARGUMENT_CHARACTER( common_ctx, IND_AllowedValues_SwitchState, 0 );
  77. }
  78. else if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerWRMP )
  79. {
  80. common_ctx->SwitchState.state = SCPI_PROCESS_ARGUMENT_CHARACTER( common_ctx, IND_AllowedValues_SwitchState, 0 );
  81. }
  82. // check result
  83. if( SCPI_ARGUMENT_CHARACTER_INVALID_ID == common_ctx->SwitchState.state )
  84. {
  85. (void)common_ctx->status; // eProgramDataIllegalArgument
  86. }
  87. else
  88. {
  89. size_t error = 0;
  90. if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerLED1 )
  91. {
  92. if( common_ctx->SwitchState.state == 0 )
  93. {
  94. }
  95. else if( common_ctx->SwitchState.state == 2 )
  96. {
  97. }
  98. else if( common_ctx->SwitchState.state == 3 )
  99. {
  100. }
  101. else
  102. {
  103. error = 1; // Key State Syntax Error
  104. }
  105. }
  106. else if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerLED2 )
  107. {
  108. if( common_ctx->SwitchState.state == 0 )
  109. {
  110. LEDHandle.harmup_stop();
  111. LEDHandle.SetMode(eLedMode_Idle);
  112. ledRed(false);
  113. ledGreen(false);
  114. }
  115. else if( common_ctx->SwitchState.state == 2 )
  116. {
  117. LEDHandle.harmup_stop();
  118. LEDHandle.SetMode(eLedMode_Idle);
  119. ledRed(false);
  120. ledGreen(true);
  121. }
  122. else if( common_ctx->SwitchState.state == 3 )
  123. {
  124. LEDHandle.harmup_stop();
  125. LEDHandle.SetMode(eLedMode_Idle);
  126. ledRed(true);
  127. ledGreen(false);
  128. }
  129. else
  130. {
  131. error = 1; // Key State Syntax Error
  132. }
  133. }
  134. else if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerWRMP )
  135. {
  136. if( common_ctx->SwitchState.state == 0 )
  137. {
  138. LEDHandle.harmup_stop();
  139. LEDHandle.SetMode(eLedMode_Normal);
  140. LEDHandle.harmup_stop();
  141. }
  142. else if( common_ctx->SwitchState.state == 1 )
  143. {
  144. LEDHandle.harmup_stop();
  145. LEDHandle.SetMode(eLedMode_Normal);
  146. LEDHandle.harmup_init();
  147. }
  148. else
  149. {
  150. error = 1; // Key State Syntax Error
  151. }
  152. }
  153. switch( error )
  154. {
  155. case 1: // Key State Syntax Error
  156. {
  157. (void)common_ctx->status; // eProgramDataIllegalArgument
  158. }
  159. break;
  160. case 2: // Key State unavailable in this device
  161. {
  162. (void)common_ctx->status; // eProgramDataIllegalArgument
  163. }
  164. break;
  165. }
  166. if( 0 != error ) break;
  167. common_ctx->status = eProgramDataDone; // request processed, wait for reading...
  168. }
  169. }
  170. else
  171. {
  172. common_ctx->status = eProgramDataNeedRead; // request processed, wait for reading...
  173. }
  174. }
  175. break;
  176. case eProgramData_Event_Read:
  177. {
  178. // @idx - current position of the source data to be outputed
  179. if( common_ctx->MeasAndSwitch.idx == 0 ) // first reading
  180. {
  181. size_t length = 0;
  182. if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerLED1 )
  183. {
  184. eLed_color_t color = GET_LED_COLOR_STATE(LED1);
  185. if(color == eLed_color_off)
  186. {
  187. const char temp[5]= "OFF\n";
  188. length = _snprintf( common_ctx->tempBuffer, sizeof(common_ctx->tempBuffer), "%s", temp);
  189. }
  190. else if(color == eLed_color_red)
  191. {
  192. const char temp[5]= "RED\n";
  193. length = _snprintf( common_ctx->tempBuffer, sizeof(common_ctx->tempBuffer), "%s", temp);
  194. }
  195. else if(color == eLed_color_grn)
  196. {
  197. const char temp[5]= "GRN\n";
  198. length = _snprintf( common_ctx->tempBuffer, sizeof(common_ctx->tempBuffer), "%s", temp);
  199. }
  200. }
  201. else if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerLED2 )
  202. {
  203. eLed_color_t color = GET_LED_COLOR_STATE(LED2);
  204. if(color == eLed_color_off)
  205. {
  206. const char temp[5]= "OFF\n";
  207. length = _snprintf( common_ctx->tempBuffer, sizeof(common_ctx->tempBuffer), "%s", temp);
  208. }
  209. else if(color == eLed_color_red)
  210. {
  211. const char temp[5]= "RED\n";
  212. length = _snprintf( common_ctx->tempBuffer, sizeof(common_ctx->tempBuffer), "%s", temp);
  213. }
  214. else if(color == eLed_color_grn)
  215. {
  216. const char temp[5]= "GRN\n";
  217. length = _snprintf( common_ctx->tempBuffer, sizeof(common_ctx->tempBuffer), "%s", temp);
  218. }
  219. }
  220. else if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerWRMP )
  221. {
  222. eLed_color_t color = GET_LED_COLOR_STATE(LED2);
  223. if(color == eLed_color_red)
  224. {
  225. const char temp[5]= "ON\n";
  226. length = _snprintf( common_ctx->tempBuffer, sizeof(common_ctx->tempBuffer), "%s", temp);
  227. }
  228. else if(color == eLed_color_grn)
  229. {
  230. const char temp[5]= "OFF\n";
  231. length = _snprintf( common_ctx->tempBuffer, sizeof(common_ctx->tempBuffer), "%s", temp);
  232. }
  233. }
  234. if( length > 0 )
  235. {
  236. // place null-terminator in the end of line
  237. common_ctx->tempBuffer[length] = '\0';
  238. }
  239. else
  240. {
  241. fsq_RaiseError( SCPI_ERROR_INTERNAL_DEVICE,
  242. SCPI_ERROR_INTERNAL_DEVICE_MSG,
  243. global_ctx->sParser.xHandlerToken.shead,
  244. global_ctx->sParser.xHandlerToken.stail );
  245. common_ctx->status = eProgramData_SpecificError; // specific error already generated
  246. break;
  247. }
  248. }
  249. // Since @done flag is set, this dispatcher shall not be called anymore.
  250. // Since this handler is implemented as a single-state automat, there no
  251. // ... other states to go to:
  252. (void)nextstate;
  253. // modify current postion index:
  254. SCPI_RESPONSE_HELPER( common_ctx, common_ctx->MeasAndSwitch.idx );
  255. }
  256. break;
  257. }
  258. return nextstate;
  259. }