re-organized HAL
git-svn-id: https://svn.vbchaos.nl/svn/hsb/trunk@220 05563f52-14a8-4384-a975-3d1654cca0fa
This commit is contained in:
@@ -1,141 +0,0 @@
|
||||
// -----------------------------------------------------------------------------
|
||||
/// @file keypadMatrix.h
|
||||
/// @brief File description
|
||||
// -----------------------------------------------------------------------------
|
||||
// Micro-Key bv
|
||||
// Industrieweg 28, 9804 TG Noordhorn
|
||||
// Postbus 92, 9800 AB Zuidhorn
|
||||
// The Netherlands
|
||||
// Tel: +31 594 503020
|
||||
// Fax: +31 594 505825
|
||||
// Email: support@microkey.nl
|
||||
// Web: www.microkey.nl
|
||||
// -----------------------------------------------------------------------------
|
||||
/// $Revision$
|
||||
/// $Author$
|
||||
/// $Date$
|
||||
// (c) 2017 Micro-Key bv
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
/// @defgroup {group_name} {group_description}
|
||||
/// Description
|
||||
|
||||
/// @file keypadMatrix.h
|
||||
/// @ingroup {group_name}
|
||||
|
||||
|
||||
#ifndef KEYPAD_INC_KEYPADMATRIX_H_
|
||||
#define KEYPAD_INC_KEYPADMATRIX_H_
|
||||
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Include files
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
#include "FreeRTOS.h"
|
||||
#include "semphr.h"
|
||||
#include "task.h"
|
||||
|
||||
#include "platform.h"
|
||||
#include "IODevice.h"
|
||||
|
||||
#include "stm32f10x_exti.h"
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Constant and macro definitions
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
#define KEYPAD_NUMBER_OF_ROWS (4)
|
||||
#define KEYPAD_NUMBER_OF_COLUMNS (4)
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Type definitions.
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
struct KeypadQueueItem
|
||||
{
|
||||
char byte;
|
||||
};
|
||||
|
||||
typedef enum
|
||||
{
|
||||
RELEASED = 0,
|
||||
PRESSED = (!RELEASED)
|
||||
}Keypad_KeyState;
|
||||
|
||||
struct keypadElement
|
||||
{
|
||||
T_PL_GPIO gpio;
|
||||
EXTI_InitTypeDef EXTI_InitStruct;
|
||||
};
|
||||
|
||||
struct Keypad
|
||||
{
|
||||
struct IODevice device;
|
||||
struct keypadElement row[KEYPAD_NUMBER_OF_ROWS];
|
||||
struct keypadElement column[KEYPAD_NUMBER_OF_COLUMNS];
|
||||
Keypad_KeyState lastState[KEYPAD_NUMBER_OF_ROWS][KEYPAD_NUMBER_OF_COLUMNS];
|
||||
xTaskHandle taskHandle;
|
||||
SemaphoreHandle_t scanSemaphore;
|
||||
xQueueHandle rxQueue;
|
||||
int waitToDebounce_ms;
|
||||
};
|
||||
|
||||
struct KeypadParameters
|
||||
{
|
||||
int rxQueueSize;
|
||||
};
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Function declarations
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
/** ----------------------------------------------------------------------------
|
||||
* Keypad_construct
|
||||
* contructor for the Keypad driver
|
||||
*
|
||||
* @param self Keypad object to initialize
|
||||
* @param parameters Parameters to use for initialisation
|
||||
* @param debounceTime debounce time for the keypad to use
|
||||
*
|
||||
* @return ErrorStatus SUCCESS if initialisation was successful
|
||||
* ERROR otherwise
|
||||
*
|
||||
* @todo
|
||||
* -----------------------------------------------------------------------------
|
||||
*/
|
||||
extern ErrorStatus Keypad_construct(struct Keypad* self, struct KeypadParameters* parameters, int debounceTime);
|
||||
|
||||
|
||||
/** ----------------------------------------------------------------------------
|
||||
* Keypad_destruct
|
||||
* destructor for the Keypad driver
|
||||
*
|
||||
* @param self Keypad object to destruct
|
||||
*
|
||||
* @return ErrorStatus SUCCESS if initialisation was successful
|
||||
* ERROR otherwise
|
||||
*
|
||||
* @todo
|
||||
* -----------------------------------------------------------------------------
|
||||
*/
|
||||
extern void Keypad_destruct (const struct Keypad* self);
|
||||
|
||||
/** ----------------------------------------------------------------------------
|
||||
* Keypad_getDefaultParameters
|
||||
* Returns default parameters for a keypad
|
||||
*
|
||||
* @param parameters Keypad parameters struct that will be
|
||||
* filled with default values
|
||||
*
|
||||
* @return ErrorStatus SUCCESS if initialisation was successful
|
||||
* ERROR otherwise
|
||||
*
|
||||
* @todo
|
||||
* -----------------------------------------------------------------------------
|
||||
*/
|
||||
extern ErrorStatus Keypad_getDefaultParameters(struct KeypadParameters* parameters);
|
||||
|
||||
|
||||
|
||||
#endif /* KEYPAD_INC_KEYPADMATRIX_H_ */
|
||||
@@ -1,221 +0,0 @@
|
||||
// -----------------------------------------------------------------------------
|
||||
/// @file keypadMatrix.c
|
||||
/// @brief Description
|
||||
// -----------------------------------------------------------------------------
|
||||
// Micro-Key bv
|
||||
// Industrieweg 28, 9804 TG Noordhorn
|
||||
// Postbus 92, 9800 AB Zuidhorn
|
||||
// The Netherlands
|
||||
// Tel: +31 594 503020
|
||||
// Fax: +31 594 505825
|
||||
// Email: support@microkey.nl
|
||||
// Web: www.microkey.nl
|
||||
// -----------------------------------------------------------------------------
|
||||
/// $Revision$
|
||||
/// $Author$
|
||||
/// $Date$
|
||||
// (c) 2017 Micro-Key bv
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
/// @file keypadMatrix.c
|
||||
/// @ingroup {group_name}
|
||||
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Include files
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include "FreeRTOSFixes.h"
|
||||
|
||||
#include "Logger.h"
|
||||
|
||||
#include "keypadMatrix.h"
|
||||
#include "platform.h"
|
||||
|
||||
#include "stm32f10x_it.h"
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Constant and macro definitions
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
#define KEYPAD_STACK_SIZE (512)
|
||||
#define KEYPAD_TASK_PRIORITY (3)
|
||||
#define KEYPAD_DEF_QUEUESIZE (32)
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Type definitions
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// File-scope variables
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Function declarations
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
static ErrorStatus read(const struct IODevice* self, char* buffer, size_t length, size_t* actualLength);
|
||||
static void KeypadTask(void* parameters);
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Function definitions
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
|
||||
ErrorStatus Keypad_construct(struct Keypad* self, struct KeypadParameters* parameters, int debounceTime)
|
||||
{
|
||||
int rowCounter = 0;
|
||||
int colCounter = 0;
|
||||
ErrorStatus returnValue = SUCCESS;
|
||||
|
||||
if(keypad != NULL)
|
||||
{
|
||||
IODevice_construct(&self->device, read, NULL);
|
||||
|
||||
if(returnValue == SUCCESS)
|
||||
{
|
||||
//! Create semaphore to synchronize with Keypad/EXTI interrupt handler
|
||||
vSemaphoreCreateBinary(self->scanSemaphore);
|
||||
}
|
||||
|
||||
self->waitToDebounce_ms = debounceTime;
|
||||
|
||||
// Initialize memory to keep track of state changes per key
|
||||
for (rowCounter = 0; rowCounter < KEYPAD_NUMBER_OF_ROWS; rowCounter++)
|
||||
{
|
||||
for (colCounter = 0; colCounter < KEYPAD_NUMBER_OF_COLUMNS; colCounter++)
|
||||
{
|
||||
self->lastState[rowCounter][colCounter] = RELEASED;
|
||||
}
|
||||
}
|
||||
|
||||
//! Create a new FREERTOS queue to handle data from Keypad input to app
|
||||
self->rxQueue = xQueueCreate(parameters->rxQueueSize, sizeof(struct KeypadQueueItem));
|
||||
if (self->rxQueue == 0)
|
||||
{
|
||||
//! Queue identifier is 0 -> error
|
||||
returnValue = ERROR; //! Set error flag
|
||||
}
|
||||
|
||||
if(returnValue == SUCCESS)
|
||||
{
|
||||
xTaskCreate(KeypadTask, (const char*)"keypadTask", KEYPAD_STACK_SIZE, keypad, KEYPAD_TASK_PRIORITY, self->taskHandle);
|
||||
}
|
||||
|
||||
if(returnValue == SUCCESS)
|
||||
{
|
||||
//! take txSemaphore
|
||||
if (xSemaphoreTake(self->scanSemaphore, 0) == pdFALSE)
|
||||
{
|
||||
//! An error has occurred
|
||||
returnValue = ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
if(returnValue == SUCCESS)
|
||||
{
|
||||
LOGGER_INFO("Keypad task started");
|
||||
}
|
||||
else
|
||||
{
|
||||
LOGGER_ERROR("Keypad task FAILED");
|
||||
}
|
||||
}
|
||||
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
|
||||
void Keypad_Destruct (const struct Keypad* self)
|
||||
{
|
||||
vTaskDelete(self->taskHandle);
|
||||
vQueueDelete(self->rxQueue);
|
||||
}
|
||||
|
||||
|
||||
ErrorStatus Keypad_getDefaultParameters(struct KeypadParameters* parameters)
|
||||
{
|
||||
ErrorStatus errorStatus = SUCCESS;
|
||||
|
||||
parameters->rxQueueSize = KEYPAD_DEF_QUEUESIZE;
|
||||
|
||||
return errorStatus;
|
||||
}
|
||||
|
||||
static ErrorStatus read(const struct IODevice* self, char* buffer, size_t length, size_t* actualLength)
|
||||
{
|
||||
ErrorStatus errorStatus = SUCCESS;
|
||||
|
||||
*actualLength = 1;
|
||||
|
||||
return errorStatus;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void KeypadTask(void* parameters)
|
||||
{
|
||||
int rowCounter = 0;
|
||||
int colCounter = 0;
|
||||
struct Keypad* self = (struct Keypad*) parameters;
|
||||
while (1)
|
||||
{
|
||||
// Wait for an interrupt to occur on one of the keypad columns
|
||||
xSemaphoreTake(self->scanSemaphore, portMAX_DELAY);
|
||||
// Debounce the keypad and wait for debounceTime prior to do anything
|
||||
vTaskDelay(self->waitToDebounce_ms);
|
||||
|
||||
// Set all row outputs
|
||||
for (rowCounter = 0; rowCounter < KEYPAD_NUMBER_OF_ROWS; rowCounter++)
|
||||
{
|
||||
GPIO_SetBits(self->row[rowCounter].gpio.GPIO_Typedef, self->row[rowCounter].gpio.GPIO_InitStruct.GPIO_Pin);
|
||||
}
|
||||
|
||||
// Scan through each row individually by resetting it (output level low) and check all column levels
|
||||
|
||||
for (rowCounter = 0; rowCounter < KEYPAD_NUMBER_OF_ROWS; rowCounter++)
|
||||
{
|
||||
GPIO_ResetBits(self->row[rowCounter].gpio.GPIO_Typedef, self->row[rowCounter].gpio.GPIO_InitStruct.GPIO_Pin);
|
||||
|
||||
for (colCounter = 0; colCounter < KEYPAD_NUMBER_OF_COLUMNS; colCounter++)
|
||||
{
|
||||
if (GPIO_ReadInputDataBit(self->column[colCounter].gpio.GPIO_Typedef, self->column[colCounter].gpio.GPIO_InitStruct.GPIO_Pin) == (uint8_t)Bit_SET)
|
||||
{
|
||||
if (self->lastState[rowCounter][colCounter] == PRESSED)
|
||||
{
|
||||
// Key has been released
|
||||
}
|
||||
else
|
||||
{
|
||||
// nothing changed
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (self->lastState[rowCounter][colCounter] == RELEASED)
|
||||
{
|
||||
// Key has been pressed
|
||||
}
|
||||
else
|
||||
{
|
||||
// nothing changed
|
||||
}
|
||||
}
|
||||
}
|
||||
GPIO_SetBits(self->row[rowCounter].gpio.GPIO_Typedef, self->row[rowCounter].gpio.GPIO_InitStruct.GPIO_Pin);
|
||||
}
|
||||
|
||||
// Reset all row outputs and return to IRQ status
|
||||
for (rowCounter = 0; rowCounter < KEYPAD_NUMBER_OF_ROWS; rowCounter++)
|
||||
{
|
||||
GPIO_ResetBits(self->row[rowCounter].gpio.GPIO_Typedef, self->row[rowCounter].gpio.GPIO_InitStruct.GPIO_Pin);
|
||||
}
|
||||
|
||||
IRQ_setKeypadEXTI(self, ENABLE);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user