Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
jmSwitch.c
00001 /************************************************************************* 00002 * @file jmSwitch.c 00003 * @brief Switch Debounce Module 00004 * 00005 * @version 1.0 00006 * @date Feb 12, 2011 00007 */ 00008 00009 #include "jmRingBuffer.h" 00010 #include "jmMessages.h" 00011 #include "jmSwitch.h" 00012 #include "LPC17xx.h" 00013 #include "main.h" 00014 #include "jmCommands.h" 00015 #include "jmMessages.h" 00016 #include "jmRingBuffer.h" 00017 #include "stdio.h" 00018 00019 #define swClose 0 00020 #define swOpen 1 00021 #define swNotInit 2 00022 00023 00024 // Static structure creation 00025 struct StructSwitch sSwitch[switchQty]; 00026 00027 /*********************************************************************** 00028 * @brief Limit Switch Module Reset 00029 * @param[in] none 00030 * @return none 00031 **********************************************************************/ 00032 void SwitchModuleReset(void){ 00033 int i; 00034 for(i=0;i<switchQty;i++){ 00035 sSwitch[i].state=swNotInit; 00036 } 00037 } 00038 00039 /*********************************************************************** 00040 * @brief Initialize an input Switch process 00041 * Format: command name (arg info)min..max values 00042 * Command Line Format: swInit (Switch id)0..7 (Pin number)0..432 (Debounce)1..255 00043 * @param[in] Extracted from command line (Switch id)0..7 (Pin number)0..432 (Debounce)1..255 00044 * @return Send Message GPPS0 id pin debounce state 00045 **********************************************************************/ 00046 void cli_SwitchInit(void){ 00047 //get switch id, DIP number, debounce time and update structure 00048 unsigned int swNum, pin, debounce, state, port,bitValue; 00049 if(ExtractUInteger(pLine,&swNum,0,7)){ 00050 if(ExtractUInteger(pLine,&pin,0,432)){ 00051 if(ExtractUInteger(pLine,&debounce,1,255)){ 00052 // Get port, bit and bit value 00053 port = pin/100; 00054 bitValue = 1<<(pin%100); 00055 00056 sSwitch[swNum].pin = (uint8_t)pin; 00057 sSwitch[swNum].port = jmGPIO[port]; 00058 sSwitch[swNum].bitValue = bitValue; 00059 sSwitch[swNum].debounce = (uint8_t)debounce; 00060 // read Switch present state 00061 state = sSwitch[swNum].port->FIOPIN &= sSwitch[swNum].bitValue; 00062 if(state>0)sSwitch[swNum].state = swOpen; 00063 else sSwitch[swNum].state = swClose; 00064 rGPPS0(swNum); // Message GPPS0 id pin debounce state 00065 if(Feedback)printf("SW %d Pin %d Debounce %d Value %d\n",swNum,pin,debounce,state); 00066 00067 return; 00068 } 00069 } 00070 } 00071 if(Help)printf("swInit (Switch id)0..7 (Pin number)0..432 (Debounce)1..255\n"); 00072 00073 // Ignore pending command line 00074 NextCommand(nl,pLine); 00075 } 00076 00077 /*********************************************************************** 00078 * @brief Switch Edge Detection 00079 * @param[in] none 00080 * @return Send Message GPPS0 id pin debounce state 00081 **********************************************************************/ 00082 void SwitchEdgeDetect(void){ 00083 unsigned int presentState,i; 00084 00085 for(i=0;i<switchQty;i++){ // for each switch SM 00086 if(sSwitch[i].state < swNotInit){ // Switch initialized ? 00087 if(sSwitch[i].eggTimer==0){ // debouncing ? 00088 00089 presentState = sSwitch[i].port->FIOPIN &= sSwitch[i].bitValue; 00090 00091 if(presentState > 0)presentState = swOpen; // present switch state 00092 else presentState = swClose; 00093 00094 if(sSwitch[i].state > presentState ){ // High to Low edge ? 00095 sSwitch[i].eggTimer = sSwitch[i].debounce; // load debounce time 00096 // memorize presentState 00097 sSwitch[i].state = presentState; 00098 rGPPS0(i); // GPPS0 message 00099 } 00100 else{ 00101 if(presentState > sSwitch[i].state ){ // Low to High edge ? 00102 sSwitch[i].eggTimer = sSwitch[i].debounce;// load debounce time 00103 // memorize presentState 00104 sSwitch[i].state = presentState; 00105 rGPPS0(i); // GPPS0 message 00106 } 00107 } 00108 } 00109 } 00110 } 00111 } 00112 00113 /*********************************************************************** 00114 * @brief Command line interface to read Limit Switch Status 00115 * Format: command name (arg info)min..max values 00116 * Command Line Format: swRead (Switch id number)0..7 00117 * @param[in] Extracted from command line (switch id number)0..7 00118 * @return Message GPPS0 id pin debounce state 00119 **********************************************************************/ 00120 void cli_SwitchRead(void){ 00121 unsigned int swNumber; 00122 if(ExtractUInteger(pLine,&swNumber,0,7)){ 00123 // One Message GPPS0 id pin debounce state 00124 rGPPS0(swNumber); 00125 return; 00126 } 00127 if(Help)printf("swRead (Switch id number)0..7\n"); 00128 // Ignore pending command line 00129 NextCommand(nl,pLine); 00130 } 00131 00132 /** Get Module Process Properties Command Line Interface 00133 * @brief Command Line Interface to Get All Public Process Properties 00134 * @param[in] none 00135 * @returns 8 Messages: GPPS0 id pin debounce state 00136 */ 00137 void cli_GPPS0(void){ 00138 int i; 00139 for(i=0;i<switchQty;i++){ 00140 rGPPS0(i); // Message GPPS0 id pin debounce state 00141 } 00142 } 00143 00144 /** Public Properties Message 00145 * @brief Send Process Properties to update GUI 00146 * @param[in] id Process identification 00147 * @returns Message GPPS0 id pin debounce state 00148 */ 00149 void rGPPS0(unsigned int id ) 00150 { 00151 printf("GPPS0 %d %d %d %d\n",id,sSwitch[id].pin,sSwitch[id].debounce,sSwitch[id].state); 00152 }
Generated on Sun Jul 17 2022 04:38:57 by
