power_switch.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #include <stdio.h>
  2. #define SCPI_ARGS_N_C 2
  3. #define SCPI_ARGS_N_Q 0
  4. #include "app/scpi/scpi_handler.h"
  5. #include "app/control_table/control_table.h"
  6. // -----
  7. // @argTypesCommand
  8. // Declare argument parser entities for command form
  9. // Supported arguments: 1=NUMERIC
  10. DECLARE_SCPI_ARGS_C( eScpiArg_Numeric, eScpiArg_Numeric );
  11. // -----
  12. // @argTypesQuery
  13. // Declare argument parser entities for query form
  14. // Supported arguments: 0
  15. DECLARE_SCPI_ARGS_Q();
  16. DECLARE_ARGUMENT_NUMERIC_VALUES_I32(AllowedValues_PortNumber, 0, 16);
  17. const uint8_t fsqvbl_CommandHandlerPortSet = 1; // CTRL:PORT
  18. const uint8_t fsqvbl_CommandHandlerMeasStart = 2; // MEAS:START
  19. const uint8_t fsqvbl_CommandHandlerMeasStop = 3; // MEAS:STOP
  20. const uint8_t fsqvbl_CommandHandlerMeasCont = 4; // MEAS:CONTinue
  21. const uint8_t fsqvbl_CommandHandlerMeasMode = 5; // MEAS:MODE
  22. #include "app/scpi/commandHandlers/power_switch.h"
  23. #include "app/nfm/nfm_base.h"
  24. // Refer to:
  25. // [1] SCPI Specification, revision 1999.0
  26. // "Standard Commands for Programmable Instruments (SCPI), VERSION 1999.0, May 1999"
  27. // [2] Gpib Programming Tutorial, (http://g2pc1.bu.edu/~qzpeng/gpib/manual/GpibProgTut.pdf)
  28. // Electronics Group (http://www.few.vu.nl/~elec), 11 January 2000 Electronics Group
  29. // [3] IEEE 488.2 Standard, revision IEEE Std 488.2-1987 (1992)
  30. // "IEEE Standard Codes, Formats, Protocols, and Common Commands for Use With IEEE Std 488.1-1987, IEEE
  31. // =================================================================================
  32. // @fsqvbl_CommandHandlerMEASnSWITCH_group
  33. // State's virtual table
  34. static void fsqe_CommandHandlerCtrl_Switch_Group( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx );
  35. static void fsql_CommandHandlerCtrl_Switch_Group( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx );
  36. static const struct fFSeqEntry_t * fsqf_CommandHandlerCtrl_Switch_Group( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx, const struct fFSeqEntry_t * * pDeferredNext );
  37. const fFSeqVTable_t fsqvbl_CommandHandlerCtrl_group =
  38. {
  39. .f = fsqf_CommandHandlerCtrl_Switch_Group,
  40. .enter = fsqe_CommandHandlerCtrl_Switch_Group,
  41. .leave = fsql_CommandHandlerCtrl_Switch_Group
  42. };
  43. static void fsqe_CommandHandlerCtrl_Switch_Group( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx )
  44. {
  45. sProcessProgramDataCommonContext_t * common_ctx = ctx;
  46. common_ctx->MeasAndSwitch.idx = 0;
  47. SCPI_PARSE_ARGUMENTS( common_ctx ); (void)common_ctx->argsParserStatus; // status is modified
  48. }
  49. static void fsql_CommandHandlerCtrl_Switch_Group( const struct fFSeqEntry_t * this, tFSeqCtx_t ctx )
  50. {
  51. }
  52. static const struct fFSeqEntry_t * fsqf_CommandHandlerCtrl_Switch_Group( const struct fFSeqEntry_t * this,
  53. tFSeqCtx_t ctx,
  54. const struct fFSeqEntry_t * * pDeferredNext )
  55. {
  56. const fFSeqEntry_t * nextstate = NULL;
  57. sProcessProgramDataCommonContext_t * common_ctx = ctx;
  58. sScpiParserContext_t * global_ctx = common_ctx->global_ctx;
  59. switch( common_ctx->event )
  60. {
  61. case eProgramData_Event_Write:
  62. {
  63. if( eScpiStatus_success != common_ctx->argsParserStatus ) // check argument parser status
  64. {
  65. common_ctx->status = eProgramDataArgumentSyntax; // parameter syntax error, caller should generate error message
  66. }
  67. else if( ! common_ctx->isQuery )
  68. {
  69. common_ctx->status = eProgramDataIllegalArgument; // forward set, illegal parameter value, caller should generate error message
  70. if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerPortSet )
  71. {
  72. // Parse first argument
  73. const sNumericEntry_t * ne = SCPI_PROCESS_ARGUMENT_NUMERIC(common_ctx, &AllowedValues_PortNumber, 0);
  74. if( ScpiNumericSuccess != ne->error ) break;
  75. uint16_t port1 = ne->Value.demicalInteger;
  76. // Parse second argument
  77. ne = SCPI_PROCESS_ARGUMENT_NUMERIC(common_ctx, &AllowedValues_PortNumber, 1);
  78. if( ScpiNumericSuccess != ne->error ) break;
  79. uint16_t port2 = ne->Value.demicalInteger;
  80. if((port1 || port2) && (port1 == port2)) break;
  81. if( port1 > NFMClass->properties.allowedOutputPorts || port2 > NFMClass->properties.allowedOutputPorts)
  82. {
  83. common_ctx->status = eProgramDataRuntimeError;
  84. break;
  85. }
  86. if( !NFMClass->methods.portMethods.setPortState(eNFMPort_1, port1) || !NFMClass->methods.portMethods.setPortState(eNFMPort_2, port2) )
  87. {
  88. common_ctx->status = eProgramDataRuntimeError;
  89. break;
  90. }
  91. sTableTablePoint_t portCommutation =
  92. {
  93. .port1 = NFMClass->methods.portMethods.getPortState(eNFMPort_1),
  94. .port2 = NFMClass->methods.portMethods.getPortState(eNFMPort_2)
  95. };
  96. ControlHandle.PortSet(portCommutation);
  97. common_ctx->status = eProgramDataDone;
  98. }
  99. }
  100. else
  101. {
  102. common_ctx->status = eProgramDataNeedRead; // request processed, wait for reading...
  103. (void)nextstate; // stay in this state
  104. }
  105. }
  106. break;
  107. case eProgramData_Event_Read:
  108. {
  109. // @idx - current position of the source data to be outputed
  110. if( common_ctx->MeasAndSwitch.idx == 0 ) // first reading
  111. {
  112. size_t length = 0;
  113. if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerPortSet )
  114. {
  115. sTableTablePoint_t portState = ControlHandle.PortRead();
  116. NFMClass->methods.portMethods.setPortState(eNFMPort_1, portState.port1);
  117. NFMClass->methods.portMethods.setPortState(eNFMPort_2, portState.port2);
  118. length = _snprintf( common_ctx->tempBuffer, sizeof(common_ctx->tempBuffer), "%d, %d", portState.port1, portState.port2);
  119. }
  120. else if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerMeasStart )
  121. {
  122. ControlHandle.Start_Meas();
  123. length = _snprintf( common_ctx->tempBuffer, sizeof(common_ctx->tempBuffer), "%s", "OK\n");
  124. }
  125. else if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerMeasStop )
  126. {
  127. ControlHandle.Stop_Meas();
  128. length = _snprintf( common_ctx->tempBuffer, sizeof(common_ctx->tempBuffer), "%s", "OK\n");
  129. }
  130. else if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerMeasCont )
  131. {
  132. ControlHandle.Continue_Meas();
  133. length = _snprintf( common_ctx->tempBuffer, sizeof(common_ctx->tempBuffer), "%s", "OK\n");
  134. }
  135. else if( common_ctx->handler_ctx == &fsqvbl_CommandHandlerMeasMode )
  136. {
  137. uint8_t res = ControlHandle.Mode_Meas();
  138. if(res != ERROR_CODE)
  139. {
  140. length = _snprintf( common_ctx->tempBuffer, sizeof(common_ctx->tempBuffer), "%s", res ? "TABLE\n" : "MANUAL\n");
  141. }
  142. }
  143. if( length > 0 )
  144. {
  145. // place null-terminator in the end of line
  146. common_ctx->tempBuffer[length] = '\0';
  147. }
  148. else
  149. {
  150. fsq_RaiseError( SCPI_ERROR_INTERNAL_DEVICE,
  151. SCPI_ERROR_INTERNAL_DEVICE_MSG,
  152. global_ctx->sParser.xHandlerToken.shead,
  153. global_ctx->sParser.xHandlerToken.stail );
  154. common_ctx->status = eProgramData_SpecificError; // specific error already generated
  155. break;
  156. }
  157. }
  158. // Since @done flag is set, this dispatcher shall not be called anymore.
  159. // Since this handler is implemented as a single-state automat, there no
  160. // ... other states to go to:
  161. (void)nextstate;
  162. // modify current postion index:
  163. SCPI_RESPONSE_HELPER( common_ctx, common_ctx->MeasAndSwitch.idx );
  164. }
  165. break;
  166. }
  167. return nextstate;
  168. }