Landtiger LPC1768 C BigLib 1
A self made, custom C library for the LandTiger board.
 
Loading...
Searching...
No Matches
joystick.c
Go to the documentation of this file.
1#include "joystick.h"
2#include "rit.h"
3#include <LPC17xx.h>
4
5#include <stdlib.h>
6
12
15
16// RIT JOB
17
19{
20 if ((LPC_GPIO1->FIOPIN & (1 << 29)) == 0 && functions[JOY_ACTION_UP].function && functions[JOY_ACTION_UP].enabled)
22
23 if ((LPC_GPIO1->FIOPIN & (1 << 25)) == 0 && functions[JOY_ACTION_SEL].function && functions[JOY_ACTION_SEL].enabled)
25
26 if ((LPC_GPIO1->FIOPIN & (1 << 26)) == 0 && functions[JOY_ACTION_DOWN].function &&
29
30 if ((LPC_GPIO1->FIOPIN & (1 << 27)) == 0 && functions[JOY_ACTION_LEFT].function &&
33
34 if ((LPC_GPIO1->FIOPIN & (1 << 28)) == 0 && functions[JOY_ACTION_RIGHT].function &&
37}
38
39// PUBLIC FUNCTIONS
40
42{
43 // Joystick PINS are 1.25 to 1.29. Need to set them as GPIO
44 // These go from bit 18 to 27 in the PINSEL3 register.
45 LPC_PINCON->PINSEL3 &= ~(0x3FF << 18);
46
47 // Setting Pin 25-29 as inputs (0) in the FIODIR for GPIO1
48 LPC_GPIO1->FIODIR &= ~(0x1F << 25);
49
50 if (!RIT_IsEnabled())
51 return JOY_ERR_RIT_UNINIT;
52
55 return JOY_ERR_OK;
56}
57
62
63// ACTIONS
64
66{
67 if ((u8)action == JOY_ACTION_ALL)
68 {
69 for (u8 i = 0; i < JOY_ACTION_COUNT; i++)
70 {
71 if (!functions[i].function || functions[i].enabled)
72 continue;
73
74 functions[i].enabled = true;
75 }
76
77 return;
78 }
79
80 if (!functions[action].function)
81 return; // No function associated.
82
83 functions[action].enabled = true;
84}
85
87{
88 if ((u8)action == JOY_ACTION_ALL)
89 {
90 for (u8 i = 0; i < JOY_ACTION_COUNT; i++)
91 {
92 if (!functions[i].function || !functions[i].enabled)
93 continue;
94
95 functions[i].enabled = false;
96 }
97
98 return;
99 }
100
101 if (!functions[action].function)
102 return; // No function associated.
103
104 functions[action].enabled = false;
105}
106
108{
109 functions[action] = (FunctionWrapper){
110 .function = function,
111 .enabled = false,
112 };
113}
114
116{
117 functions[action] = (FunctionWrapper){
118 .function = NULL,
119 .enabled = false,
120 };
121}
_PRIVATE FunctionWrapper functions[JOY_ACTION_COUNT]
Array of function pointers containing the hanlder for each joystick function.
Definition joystick.c:14
void JOYSTICK_Deinit(void)
Deinitializes the joystick, by removing all functions associated with the actions,...
Definition joystick.c:58
void JOYSTICK_UnsetFunction(JOYSTICK_Action action)
Unsets a functionality for the given action, if previously set.
Definition joystick.c:115
JOYSTICK_Error JOYSTICK_Init(void)
Initializes the joystick peripheral with the given options.
Definition joystick.c:41
void JOYSTICK_SetFunction(JOYSTICK_Action action, JOYSTICK_Function function)
Sets a functionality for the given action.
Definition joystick.c:107
_PRIVATE void handle_polling(void)
Definition joystick.c:18
void JOYSTICK_DisableAction(JOYSTICK_Action action)
Disables the specific action, and disables the binding with its functionality, without un-binding it ...
Definition joystick.c:86
void JOYSTICK_EnableAction(JOYSTICK_Action action)
Enables the specified action, thus enabling the binding with the associated functionality,...
Definition joystick.c:65
void(* JOYSTICK_Function)(void)
Joystick function handler function pointer.
JOYSTICK_Error
@ JOY_ERR_OK
No error.
@ JOY_ERR_RIT_UNINIT
During initialization, POLL_WITH_RIT was requested but the RIT was not enabled.
JOYSTICK_Action
@ JOY_ACTION_SEL
@ JOY_ACTION_COUNT
@ JOY_ACTION_RIGHT
@ JOY_ACTION_LEFT
@ JOY_ACTION_UP
@ JOY_ACTION_DOWN
#define JOY_ACTION_ALL
RIT_Error RIT_EnableJob(RIT_Job job)
Include this job in the RIT handler queue.
Definition rit_job.c:65
RIT_Error RIT_RemoveJob(RIT_Job job)
Removes a job from the RIT interrupt handler job queue.
Definition rit_job.c:117
RIT_Error RIT_AddJob(RIT_Job job, u8 multiplier_factor)
Definition rit_job.c:104
bool RIT_IsEnabled(void)
Returns whether the RIT is counting or not.
Definition rit.c:60
#define RIT_NO_DIVIDER
Tells the RIT that the specified job needs to be executed at exactly the interval of interrupts chose...
Definition rit_types.h:8
JOYSTICK_Function function
Definition joystick.c:9
uint8_t u8
Definition types.h:8
#define _PRIVATE
Definition types.h:37