Advertisement
Guest User

button.c

a guest
Nov 10th, 2015
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.10 KB | None | 0 0
  1. /**
  2.  * |----------------------------------------------------------------------
  3.  * | Copyright (C) Tilen Majerle, 2015
  4.  * |
  5.  * | This program is free software: you can redistribute it and/or modify
  6.  * | it under the terms of the GNU General Public License as published by
  7.  * | the Free Software Foundation, either version 3 of the License, or
  8.  * | any later version.
  9.  * |  
  10.  * | This program is distributed in the hope that it will be useful,
  11.  * | but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * | GNU General Public License for more details.
  14.  * |
  15.  * | You should have received a copy of the GNU General Public License
  16.  * | along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17.  * |----------------------------------------------------------------------
  18.  */
  19. #include "button.h"
  20.  
  21. /* Button states */
  22. #define BUTTON_STATE_START        0
  23. #define BUTTON_STATE_DEBOUNCE     1
  24. #define BUTTON_STATE_PRESSED      2
  25. #define BUTTON_STATE_WAITRELEASE  3
  26.  
  27. /* Internal structure */
  28. typedef struct {
  29.     TM_BUTTON_t* Buttons[BUTTON_MAX_BUTTONS];
  30.     uint16_t ButtonsCount;
  31. } TM_BUTTON_INT_t;
  32. static TM_BUTTON_INT_t Buttons;
  33.  
  34. /* Internal functions */
  35. void TM_BUTTON_INT_CheckButton(TM_BUTTON_t* ButtonStruct);
  36.  
  37. TM_BUTTON_t* TM_BUTTON_Init(uint8_t GPIOx, uint16_t GPIO_Pin, uint8_t ButtonState, void (*ButtonHandler)(TM_BUTTON_PressType_t)) {
  38.     TM_BUTTON_t* ButtonStruct;
  39. //  TM_GPIO_PuPd_t P;
  40.    
  41.     /* Init delay function */
  42.     TM_DELAY_Init();
  43.    
  44.     /* Check if available */
  45.     if (Buttons.ButtonsCount >= BUTTON_MAX_BUTTONS) {
  46.         return NULL;
  47.     }
  48.    
  49.     /* Allocate memory for button */
  50.     ButtonStruct = (TM_BUTTON_t *) malloc(sizeof(TM_BUTTON_t));
  51.    
  52.     /* Check if allocated */
  53.     if (ButtonStruct == NULL) {
  54.         return NULL;
  55.     }
  56.    
  57.     /* Save settings */
  58.     ButtonStruct->GPIOx = GPIOx;
  59.     ButtonStruct->GPIO_Pin = GPIO_Pin;
  60.     ButtonStruct->GPIO_State = ButtonState ? 1 : 0;
  61.     ButtonStruct->ButtonHandler = ButtonHandler;
  62.     /* Add to correctly initialize State for button */
  63.     ButtonStruct->State= BUTTON_STATE_START;
  64.    
  65.     /* Set default values */
  66.     ButtonStruct->PressDebounceTime = BUTTON_DEBOUNCE_TIME;
  67.     ButtonStruct->PressNormalTime = BUTTON_NORMAL_PRESS_TIME;
  68.     ButtonStruct->PressLongTime = BUTTON_LONG_PRESS_TIME;
  69.    
  70.     /* Init pin with pull resistor */
  71. //  if (ButtonStruct->GPIO_State) {
  72. //      /* Pulldown */
  73. //      P = TM_GPIO_PuPd_DOWN;
  74. //  } else {
  75. //      /* Pullup */
  76. //      P = TM_GPIO_PuPd_UP;
  77. //  }
  78.    
  79.     /* Init GPIO pin as input with proper pull resistor */
  80.     Chip_IOCON_PinMux(LPC_IOCON, GPIOx, GPIO_Pin,  IOCON_FUNC0, IOCON_DIGMODE_EN);
  81.     Chip_GPIO_SetPinDIRInput(LPC_GPIO, GPIOx, GPIO_Pin);
  82.  
  83. //  TM_GPIO_Init(GPIOx, GPIO_Pin, TM_GPIO_Mode_IN, TM_GPIO_OType_PP, P, TM_GPIO_Speed_Low);
  84.    
  85.     /* Save button */
  86.     Buttons.Buttons[Buttons.ButtonsCount++] = ButtonStruct;
  87.    
  88.     /* Return button pointer */
  89.     return ButtonStruct;
  90. }
  91.  
  92. TM_BUTTON_t* TM_BUTTON_SetPressTime(TM_BUTTON_t* ButtonStruct, uint16_t Normal, uint16_t Long) {
  93.     /* Set values */
  94.     ButtonStruct->PressNormalTime = Normal;
  95.     ButtonStruct->PressLongTime = Long;
  96.    
  97.     /* Return pointer */
  98.     return ButtonStruct;
  99. }
  100.  
  101. void TM_BUTTON_Update(void) {
  102.     uint16_t i;
  103.    
  104.     /* Go through all buttons */
  105.     for (i = 0; i < Buttons.ButtonsCount; i++) {
  106.         TM_BUTTON_INT_CheckButton(Buttons.Buttons[i]);
  107.     }
  108. }
  109.  
  110. /* Internal functions */
  111. void TM_BUTTON_INT_CheckButton(TM_BUTTON_t* ButtonStruct) {
  112.     uint32_t status; // = TM_GPIO_GetInputPinValue(ButtonStruct->GPIOx, ButtonStruct->GPIO_Pin);
  113.     status = Chip_GPIO_GetPortValue(LPC_GPIO, ButtonStruct->GPIOx);
  114.     status = (status & (uint32_t)(1<<ButtonStruct->GPIO_Pin));
  115.  
  116.     uint32_t now = TM_DELAY_Time();
  117.    
  118.     /* First stage */
  119.     if (ButtonStruct->State == BUTTON_STATE_START) {
  120.         /* Check if pressed */
  121.         if (status == ButtonStruct->GPIO_State) {
  122.             /* Button pressed, go to stage BUTTON_STATE_START */
  123.             ButtonStruct->State = BUTTON_STATE_DEBOUNCE;
  124.             /* Save pressed time */
  125.             ButtonStruct->StartTime = now;
  126.  
  127.         }
  128.     }
  129.  
  130.     if (ButtonStruct->State == BUTTON_STATE_DEBOUNCE) {
  131.         /* Button still pressed */
  132.         /* Check for debounce */
  133.         if (status == ButtonStruct->GPIO_State) {
  134.             if (now > (ButtonStruct->StartTime + ButtonStruct->PressDebounceTime)) {
  135.                 /* Button debounce OK, Goto Normal Press */
  136.                 ButtonStruct->State = BUTTON_STATE_PRESSED;
  137.                 if (ButtonStruct->ButtonHandler) {
  138.                     /* Call function callback */
  139.                     ButtonStruct->ButtonHandler(TM_BUTTON_PressType_OnPressed);
  140.                 }
  141.             }
  142.         } else if (status != ButtonStruct->GPIO_State) {
  143.             /* Not pressed */
  144.             /* It was bounce, start over */
  145.             /* Go to state BUTTON_STATE_START */
  146.             ButtonStruct->State = BUTTON_STATE_START;
  147.         }
  148.     }
  149.    
  150.     if (ButtonStruct->State == BUTTON_STATE_PRESSED) {
  151.         /* Button still pressed */
  152.         /* Check for long press */
  153.         if (status == ButtonStruct->GPIO_State) {
  154.             if (now > (ButtonStruct->StartTime + ButtonStruct->PressLongTime)) {
  155.                 /* Button pressed OK, call function */
  156.                 if (ButtonStruct->ButtonHandler) {
  157.                     /* Call function callback */
  158.                     ButtonStruct->ButtonHandler(TM_BUTTON_PressType_Long);
  159.                 }
  160.                
  161.                 /* Go to stage BUTTON_STATE_WAITRELEASE */
  162.                 ButtonStruct->State = BUTTON_STATE_WAITRELEASE;
  163.             }
  164.         } else if (status != ButtonStruct->GPIO_State) {
  165.             /* Not pressed */
  166.             if (now > (ButtonStruct->StartTime + ButtonStruct->PressNormalTime)) {
  167.                 /* Button pressed OK, call function */
  168.                 if (ButtonStruct->ButtonHandler) {
  169.                     /* Call function callback */
  170.                     ButtonStruct->ButtonHandler(TM_BUTTON_PressType_Normal);
  171.                 }
  172.                
  173.                 /* Go to stage BUTTON_STATE_WAITRELEASE */
  174.                 ButtonStruct->State = BUTTON_STATE_WAITRELEASE;
  175.             } else {
  176.                 /* Go to state BUTTON_STATE_START */
  177.                 ButtonStruct->State = BUTTON_STATE_START;
  178.             }
  179.         } else {
  180.             /* Go to state BUTTON_STATE_START */
  181.             ButtonStruct->State = BUTTON_STATE_START;
  182.         }
  183.     }
  184.    
  185.     if (ButtonStruct->State == BUTTON_STATE_WAITRELEASE) {
  186.         /* Wait till button released */
  187.         if (status != ButtonStruct->GPIO_State) {
  188.             /* Go to stage 0 again */
  189.             ButtonStruct->State = BUTTON_STATE_START;
  190.         }
  191.     }
  192.    
  193.     /* Save current status */
  194.     ButtonStruct->LastStatus = status;
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement