]> rtime.felk.cvut.cz Git - pes-rpp/rpp-simulink.git/blobdiff - rpp/blocks/sfunction_lout.c
Change license to MIT
[pes-rpp/rpp-simulink.git] / rpp / blocks / sfunction_lout.c
index c05e81da2208dbcdafa2013ca5e919836506b436..66044c3273cabede7f83c692b9fd764529a6f76d 100644 (file)
@@ -1,20 +1,28 @@
-/* Copyright (C) 2013 Czech Technical University in Prague
+/* Copyright (C) 2013, 2014 Czech Technical University in Prague
  *
  * Authors:
  *     - Carlos Jenkins <carlos@jenkins.co.cr>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use,
+ * copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following
+ * conditions:
  *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
  *
  * File : sfunction_lout.c
  * Abstract:
  *     trailer.c
  *
  * Compile with:
- *     <matlabroot>/bin/mex CFLAGS="\$CFLAGS -std=c99" sfunction_lout.c
+ *     <matlabroot>/bin/mex sfunction_lout.c
  */
 
+/*
+%YAML 1.2
+---
+Name: Digital Output
+Category: IO blocks
+Header: rpp/lout.h
+Mnemonic: LOUT
 
-#define S_FUNCTION_NAME sfunction_lout
-#include "header.c"
+Inputs:
+  - { name: "Digital Output",   type: "bool"  }
 
+Outputs:
+  - { name: "ErrFlag",          type: "bool"  }
 
-static void mdlInitializeSizes(SimStruct *S)
-{
-    // Number of expected parameters
-    ssSetNumSFcnParams(S, 2);
+Parameters:
+  - { name: "Pin number", type: "uint8", range: "[1-8]" }
 
-    // Check parameters
-    if(checkParametersMismatch(S)) {
-        return;
-    }
+# Description and Help is in Markdown mark-up
+Description: |
 
-    // Set the parameter's tunable status
-    ssSetSFcnParamTunable(S, 0, 1);
-    ssSetSFcnParamTunable(S, 1, 0);
+  Sends the digital value to the specified logic output pin on the RPP
+  board.
 
-    ssSetNumPWork(S, 0);
+  An input of 1 sets the pin high and 0 sets the pin low.
 
-    if(!ssSetNumDWork(S, 0)) {
-        return;
-    }
+  If an error is detected while setting the pin, the ErrFlag is set
+  high.
 
-    // Set the number of input ports.
-    if(!ssSetNumInputPorts(S, 1)) {
+Help: |
+
+  This block allows to write to the digital outputs on the RPP board. The ErrFlag should raise if 
+  `rpp_lout_set()` or `rpp_lout_update()` returns error. Because the ErrFlag should never set, 
+  once set the following steps will never clear it back. `rpp_lout_update()` is called on each 
+  block, which is not the most efficient but guaranties consistent behavior.
+
+Status: Stable
+
+RPP API functions used:
+    - rpp_lout_set()
+    - rpp_lout_update()
+
+Relevant demos:
+    - digital_passthrough
+    - led_blink_all
+    - led_blink
+...
+*/
+
+#define S_FUNCTION_NAME sfunction_lout
+#include "header.c"
+
+
+static void mdlInitializeSizes(SimStruct *S)
+{
+    /*
+     * Configure parameters: 1
+     *  - Pin number: [1-8]
+     */
+    if (!rppSetNumParams(S, 1)) {
         return;
     }
 
-    // Configure the input port 1
-    ssSetInputPortDataType(S, 0, SS_UINT8);
-    ssSetInputPortWidth(S, 0, 1);
-    ssSetInputPortComplexSignal(S, 0, COMPLEX_NO);
-    ssSetInputPortDirectFeedThrough(S, 0, 1);
-    ssSetInputPortAcceptExprInRTW(S, 0, 1);
-    ssSetInputPortOverWritable(S, 0, 1);
-    ssSetInputPortOptimOpts(S, 0, SS_REUSABLE_AND_LOCAL);
-    ssSetInputPortRequiredContiguous(S, 0, 1);
-
-    // Set the number of output ports.
-    if(!ssSetNumOutputPorts(S, 0)) {
+    /*
+     * Configure input ports: 1
+     *  - Digital output.
+     */
+    if (!ssSetNumInputPorts(S, 1)) {
         return;
     }
+    rppAddInputPort(S, 0, SS_BOOLEAN);
 
-    // This S-function can be used in referenced model simulating in normal mode.
-    ssSetModelReferenceNormalModeSupport(S, MDL_START_AND_MDL_PROCESS_PARAMS_OK);
-
-    // Set the number of sample time.
-    ssSetNumSampleTimes(S, 1);
-
-  /*
-   * All options have the form SS_OPTION_<name> and are documented in
-   * matlabroot/simulink/include/simstruc.h. The options should be
-   * combined with bitwise OR as in
-   *   ssSetOptions(S, (SS_OPTION_name1 | SS_OPTION_name2))
-   */
-  ssSetOptions(S,
-               SS_OPTION_USE_TLC_WITH_ACCELERATOR |
-               SS_OPTION_CAN_BE_CALLED_CONDITIONALLY |
-               SS_OPTION_EXCEPTION_FREE_CODE |
-               SS_OPTION_WORKS_WITH_CODE_REUSE |
-               SS_OPTION_SFUNCTION_INLINED_FOR_RTW |
-               SS_OPTION_DISALLOW_CONSTANT_SAMPLE_TIME);
-}
-
-
-static void mdlInitializeSampleTimes(SimStruct *S)
-{
-    const double* sampleTime = mxGetPr(SAMPLE_TIME);
-    const size_t stArraySize = mxGetM(SAMPLE_TIME) * mxGetN(SAMPLE_TIME);
-    ssSetSampleTime(S, 0, sampleTime[0]);
-
-    if(stArraySize == 1) {
-        ssSetOffsetTime(S, 0, (sampleTime[0] == CONTINUOUS_SAMPLE_TIME?
-        FIXED_IN_MINOR_STEP_OFFSET: 0.0));
-    } else {
-        ssSetOffsetTime(S, 0, sampleTime[1]);
+    /*
+     * Configure output ports: 1
+     *  - Error flag.
+     */
+    if (!ssSetNumOutputPorts(S, 1)) {
+        return;
     }
+    rppAddOutputPort(S, 0, SS_BOOLEAN);
 
-    #if defined(ssSetModelReferenceSampleTimeDefaultInheritance)
-    ssSetModelReferenceSampleTimeDefaultInheritance(S);
-    #endif
+    /* Set standard options for this block */
+    rppSetStandardOptions(S);
 }
 
 
 #ifdef MATLAB_MEX_FILE
+#define MDL_CHECK_PARAMETERS
 static void mdlCheckParameters(SimStruct *S)
 {
-    // Check the parameter 1
-    if EDIT_OK(S, 0) {
-        int_T dimsArray[2] = { 1, 1 };
-
-        // Check the parameter attributes
-        ssCheckSFcnParamValueAttribs(S, 0, "P1", DYNAMICALLY_TYPED, 2, dimsArray, 0);
-    }
-
-    // Check the parameter 2 (sample time)
-    if(!checkSampleTime(S, 1)) {
+    /* Check the parameter 1 */
+    if (!rppValidParamRange(S, 0, 1, 8)) {
         return;
     }
 }
+#endif
 
 
+#ifdef MATLAB_MEX_FILE
+#define MDL_SET_WORK_WIDTHS
 static void mdlSetWorkWidths(SimStruct *S)
 {
-    // Set number of run-time parameters
-    if(!ssSetNumRunTimeParams(S, 1)) {
+    /* Set number of run-time parameters */
+    if (!ssSetNumRunTimeParams(S, 1)) {
         return;
     }
 
-    // Register the run-time parameter 1
-    ssRegDlgParamAsRunTimeParam(S, 0, 0, "p1", ssGetDataTypeId(S, "uint8"));
+    /* Register the run-time parameter 1 */
+    ssRegDlgParamAsRunTimeParam(S, 0, 0, "p1", SS_UINT8);
 }
 #endif
 
 
+#define COMMON_MDLINITIALIZESAMPLETIMES_INHERIT
 #define UNUSED_MDLOUTPUTS
 #define UNUSED_MDLTERMINATE
 #include "trailer.c"