Command all jmCLIG modules from serial port

Committer:
jm
Date:
Sat Feb 12 16:50:25 2011 +0000
Revision:
0:9112e09912db
jmAll Command Line Interface Module

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jm 0:9112e09912db 1 /*************************************************************************
jm 0:9112e09912db 2 * @file jmSwitch.c
jm 0:9112e09912db 3 * @brief Switch Debounce Module
jm 0:9112e09912db 4 *
jm 0:9112e09912db 5 * @version 1.0
jm 0:9112e09912db 6 * @date Feb 12, 2011
jm 0:9112e09912db 7 */
jm 0:9112e09912db 8
jm 0:9112e09912db 9 #include "jmRingBuffer.h"
jm 0:9112e09912db 10 #include "jmMessages.h"
jm 0:9112e09912db 11 #include "jmSwitch.h"
jm 0:9112e09912db 12 #include "LPC17xx.h"
jm 0:9112e09912db 13 #include "main.h"
jm 0:9112e09912db 14 #include "jmCommands.h"
jm 0:9112e09912db 15 #include "jmMessages.h"
jm 0:9112e09912db 16 #include "jmRingBuffer.h"
jm 0:9112e09912db 17 #include "stdio.h"
jm 0:9112e09912db 18
jm 0:9112e09912db 19 #define swClose 0
jm 0:9112e09912db 20 #define swOpen 1
jm 0:9112e09912db 21 #define swNotInit 2
jm 0:9112e09912db 22
jm 0:9112e09912db 23
jm 0:9112e09912db 24 // Static structure creation
jm 0:9112e09912db 25 struct StructSwitch sSwitch[switchQty];
jm 0:9112e09912db 26
jm 0:9112e09912db 27 /***********************************************************************
jm 0:9112e09912db 28 * @brief Limit Switch Module Reset
jm 0:9112e09912db 29 * @param[in] none
jm 0:9112e09912db 30 * @return none
jm 0:9112e09912db 31 **********************************************************************/
jm 0:9112e09912db 32 void SwitchModuleReset(void){
jm 0:9112e09912db 33 int i;
jm 0:9112e09912db 34 for(i=0;i<switchQty;i++){
jm 0:9112e09912db 35 sSwitch[i].state=swNotInit;
jm 0:9112e09912db 36 }
jm 0:9112e09912db 37 }
jm 0:9112e09912db 38
jm 0:9112e09912db 39 /***********************************************************************
jm 0:9112e09912db 40 * @brief Initialize an input Switch process
jm 0:9112e09912db 41 * Format: command name (arg info)min..max values
jm 0:9112e09912db 42 * Command Line Format: swInit (Switch id)0..7 (Pin number)0..432 (Debounce)1..255
jm 0:9112e09912db 43 * @param[in] Extracted from command line (Switch id)0..7 (Pin number)0..432 (Debounce)1..255
jm 0:9112e09912db 44 * @return Send Message GPPS0 id pin debounce state
jm 0:9112e09912db 45 **********************************************************************/
jm 0:9112e09912db 46 void cli_SwitchInit(void){
jm 0:9112e09912db 47 //get switch id, DIP number, debounce time and update structure
jm 0:9112e09912db 48 unsigned int swNum, pin, debounce, state, port,bitValue;
jm 0:9112e09912db 49 if(ExtractUInteger(pLine,&swNum,0,7)){
jm 0:9112e09912db 50 if(ExtractUInteger(pLine,&pin,0,432)){
jm 0:9112e09912db 51 if(ExtractUInteger(pLine,&debounce,1,255)){
jm 0:9112e09912db 52 // Get port, bit and bit value
jm 0:9112e09912db 53 port = pin/100;
jm 0:9112e09912db 54 bitValue = 1<<(pin%100);
jm 0:9112e09912db 55
jm 0:9112e09912db 56 sSwitch[swNum].pin = (uint8_t)pin;
jm 0:9112e09912db 57 sSwitch[swNum].port = jmGPIO[port];
jm 0:9112e09912db 58 sSwitch[swNum].bitValue = bitValue;
jm 0:9112e09912db 59 sSwitch[swNum].debounce = (uint8_t)debounce;
jm 0:9112e09912db 60 // read Switch present state
jm 0:9112e09912db 61 state = sSwitch[swNum].port->FIOPIN &= sSwitch[swNum].bitValue;
jm 0:9112e09912db 62 if(state>0)sSwitch[swNum].state = swOpen;
jm 0:9112e09912db 63 else sSwitch[swNum].state = swClose;
jm 0:9112e09912db 64 rGPPS0(swNum); // Message GPPS0 id pin debounce state
jm 0:9112e09912db 65 if(Feedback)printf("SW %d Pin %d Debounce %d Value %d\n",swNum,pin,debounce,state);
jm 0:9112e09912db 66
jm 0:9112e09912db 67 return;
jm 0:9112e09912db 68 }
jm 0:9112e09912db 69 }
jm 0:9112e09912db 70 }
jm 0:9112e09912db 71 if(Help)printf("swInit (Switch id)0..7 (Pin number)0..432 (Debounce)1..255\n");
jm 0:9112e09912db 72
jm 0:9112e09912db 73 // Ignore pending command line
jm 0:9112e09912db 74 NextCommand(nl,pLine);
jm 0:9112e09912db 75 }
jm 0:9112e09912db 76
jm 0:9112e09912db 77 /***********************************************************************
jm 0:9112e09912db 78 * @brief Switch Edge Detection
jm 0:9112e09912db 79 * @param[in] none
jm 0:9112e09912db 80 * @return Send Message GPPS0 id pin debounce state
jm 0:9112e09912db 81 **********************************************************************/
jm 0:9112e09912db 82 void SwitchEdgeDetect(void){
jm 0:9112e09912db 83 unsigned int presentState,i;
jm 0:9112e09912db 84
jm 0:9112e09912db 85 for(i=0;i<switchQty;i++){ // for each switch SM
jm 0:9112e09912db 86 if(sSwitch[i].state < swNotInit){ // Switch initialized ?
jm 0:9112e09912db 87 if(sSwitch[i].eggTimer==0){ // debouncing ?
jm 0:9112e09912db 88
jm 0:9112e09912db 89 presentState = sSwitch[i].port->FIOPIN &= sSwitch[i].bitValue;
jm 0:9112e09912db 90
jm 0:9112e09912db 91 if(presentState > 0)presentState = swOpen; // present switch state
jm 0:9112e09912db 92 else presentState = swClose;
jm 0:9112e09912db 93
jm 0:9112e09912db 94 if(sSwitch[i].state > presentState ){ // High to Low edge ?
jm 0:9112e09912db 95 sSwitch[i].eggTimer = sSwitch[i].debounce; // load debounce time
jm 0:9112e09912db 96 // memorize presentState
jm 0:9112e09912db 97 sSwitch[i].state = presentState;
jm 0:9112e09912db 98 rGPPS0(i); // GPPS0 message
jm 0:9112e09912db 99 }
jm 0:9112e09912db 100 else{
jm 0:9112e09912db 101 if(presentState > sSwitch[i].state ){ // Low to High edge ?
jm 0:9112e09912db 102 sSwitch[i].eggTimer = sSwitch[i].debounce;// load debounce time
jm 0:9112e09912db 103 // memorize presentState
jm 0:9112e09912db 104 sSwitch[i].state = presentState;
jm 0:9112e09912db 105 rGPPS0(i); // GPPS0 message
jm 0:9112e09912db 106 }
jm 0:9112e09912db 107 }
jm 0:9112e09912db 108 }
jm 0:9112e09912db 109 }
jm 0:9112e09912db 110 }
jm 0:9112e09912db 111 }
jm 0:9112e09912db 112
jm 0:9112e09912db 113 /***********************************************************************
jm 0:9112e09912db 114 * @brief Command line interface to read Limit Switch Status
jm 0:9112e09912db 115 * Format: command name (arg info)min..max values
jm 0:9112e09912db 116 * Command Line Format: swRead (Switch id number)0..7
jm 0:9112e09912db 117 * @param[in] Extracted from command line (switch id number)0..7
jm 0:9112e09912db 118 * @return Message GPPS0 id pin debounce state
jm 0:9112e09912db 119 **********************************************************************/
jm 0:9112e09912db 120 void cli_SwitchRead(void){
jm 0:9112e09912db 121 unsigned int swNumber;
jm 0:9112e09912db 122 if(ExtractUInteger(pLine,&swNumber,0,7)){
jm 0:9112e09912db 123 // One Message GPPS0 id pin debounce state
jm 0:9112e09912db 124 rGPPS0(swNumber);
jm 0:9112e09912db 125 return;
jm 0:9112e09912db 126 }
jm 0:9112e09912db 127 if(Help)printf("swRead (Switch id number)0..7\n");
jm 0:9112e09912db 128 // Ignore pending command line
jm 0:9112e09912db 129 NextCommand(nl,pLine);
jm 0:9112e09912db 130 }
jm 0:9112e09912db 131
jm 0:9112e09912db 132 /** Get Module Process Properties Command Line Interface
jm 0:9112e09912db 133 * @brief Command Line Interface to Get All Public Process Properties
jm 0:9112e09912db 134 * @param[in] none
jm 0:9112e09912db 135 * @returns 8 Messages: GPPS0 id pin debounce state
jm 0:9112e09912db 136 */
jm 0:9112e09912db 137 void cli_GPPS0(void){
jm 0:9112e09912db 138 int i;
jm 0:9112e09912db 139 for(i=0;i<switchQty;i++){
jm 0:9112e09912db 140 rGPPS0(i); // Message GPPS0 id pin debounce state
jm 0:9112e09912db 141 }
jm 0:9112e09912db 142 }
jm 0:9112e09912db 143
jm 0:9112e09912db 144 /** Public Properties Message
jm 0:9112e09912db 145 * @brief Send Process Properties to update GUI
jm 0:9112e09912db 146 * @param[in] id Process identification
jm 0:9112e09912db 147 * @returns Message GPPS0 id pin debounce state
jm 0:9112e09912db 148 */
jm 0:9112e09912db 149 void rGPPS0(unsigned int id )
jm 0:9112e09912db 150 {
jm 0:9112e09912db 151 printf("GPPS0 %d %d %d %d\n",id,sSwitch[id].pin,sSwitch[id].debounce,sSwitch[id].state);
jm 0:9112e09912db 152 }