Control up to 4 steppers axis with limit switch logic from serial port

Dependencies:   mbed

Committer:
jm
Date:
Sat Feb 12 16:49:03 2011 +0000
Revision:
0:3e676fcd9c71
jmStepperAxis Command Line Interface Module

Who changed what in which revision?

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