forked from art103/LasaurGrbl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
joystick.c
296 lines (236 loc) · 7.23 KB
/
joystick.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
joystick.c - Sample a 2-axis analog input (joystick)
Part of LasaurGrbl
Copyright (c) 2013 Richard Taylor
LasaurGrbl is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
LasaurGrbl is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
#include <math.h>
#include <stdint.h>
#include <stdbool.h>
#include <inc/hw_ints.h>
#include <inc/hw_types.h>
#include <inc/hw_memmap.h>
#include <inc/hw_timer.h>
#include <inc/hw_gpio.h>
#include <driverlib/adc.h>
#include <driverlib/gpio.h>
#include <driverlib/sysctl.h>
#include <driverlib/timer.h>
#include <driverlib/interrupt.h>
#include "config.h"
#include "joystick.h"
#include "tasks.h"
// The joystick has no effect when the position is central +/- threshold.
#define ZERO_THRESHOLD 0x50
#define STATUS_CH0_IDLE 0x01
#define STATUS_CH1_IDLE 0x02
#define STATUS_XHAIR_ON 0x04
//
// This array is used for storing the data read from the ADC FIFO. It
// must be as large as the FIFO for the sequencer in use. This example
// uses sequence 3 which has a FIFO depth of 1. If another sequence
// was used with a deeper FIFO, then the array size must be changed.
//
static unsigned long joystick_x[1] = {0};
static unsigned long joystick_y[1] = {0};
static unsigned long joystick_center[2] = {0};
static unsigned long status = STATUS_CH0_IDLE | STATUS_CH1_IDLE;
uint8_t current_jog_z = 0;
static uint8_t enabled = 0;
static bool paused = false;
static struct task_manual_move_data task_data = {0.0};
static void x_handler(void) {
//
// Clear the ADC interrupt flag.
//
ADCIntClear(ADC0_BASE, 0);
//
// Read ADC Value.
//
ADCSequenceDataGet(ADC0_BASE, 0, joystick_x);
// Set the center position
if (joystick_center[0] == 0)
joystick_center[0] = joystick_x[0];
status |= STATUS_CH0_IDLE;
}
static void y_handler(void) {
//
// Clear the ADC interrupt flag.
//
ADCIntClear(ADC0_BASE, 1);
//
// Read ADC Value.
//
ADCSequenceDataGet(ADC0_BASE, 1, joystick_y);
// Set the center position
if (joystick_center[1] == 0)
joystick_center[1] = joystick_y[0];
status |= STATUS_CH1_IDLE;
}
static bool buttonDown = false;
static void button_handler(void) {
GPIOIntClear(JOY_PORT, JOY_MASK);
int32_t button = GPIOPinRead(JOY_PORT, JOY_MASK);
if (button > 0) {
buttonDown = true;
}
if (buttonDown && button <= 0) {
// release
buttonDown = false;
if (enabled == 1) {
enabled = 0;
task_enable(TASK_SET_OFFSET, 0);
}
else {
enabled = 1;
}
}
}
static void jog_z_handler(void) {
GPIOIntClear(JOG_Z_PORT, JOG_Z_MASK);
current_jog_z = ((JOG_Z_MASK) & GPIOPinRead(JOG_Z_PORT, JOG_Z_MASK));
}
static void joystick_isr(void) {
TimerIntClear(JOY_TIMER, TIMER_TIMA_TIMEOUT);
task_data.x_offset = 0.0;
task_data.y_offset = 0.0;
task_data.z_offset = 0.0;
if (enabled && !paused) {
unsigned long x = joystick_x[0];
unsigned long y = joystick_y[0];
double x_off = 0;
double y_off = 0;
if (x > joystick_center[0] + ZERO_THRESHOLD) {
x_off = (double)(x - joystick_center[0]) / 0x800;
} else if (x < joystick_center[0] - ZERO_THRESHOLD) {
x_off = -(double)(joystick_center[0] - x) / 0x800;
}
if (y > joystick_center[1] + ZERO_THRESHOLD) {
y_off = (double)(y - joystick_center[1]) / 0x800;
} else if (y < joystick_center[1] - ZERO_THRESHOLD) {
y_off = -(double)(joystick_center[1] - y) / 0x800;
}
// Have two speed levels for accuracy and speed when wanted.
if (fabs(y_off) < 0.5)
y_off /= 50.0;
else// if (fabs(y_off) < 1.0)
y_off /= 5.0;
if (fabs(x_off) < 0.5)
x_off /= 50.0;
else// if (fabs(x_off) < 1.0)
x_off /= 5.0;
#ifdef JOY_INVERT_Y
task_data.x_offset = -y_off;
#else
task_data.x_offset = y_off;
#endif
#ifdef JOY_INVERT_X
task_data.y_offset = -x_off;
#else
task_data.y_offset = x_off;
#endif
if( current_jog_z ){
switch(current_jog_z){
case (1<<JOG_Z_UP_BIT):
task_data.z_offset = 0.0003; break;
case (1<<JOG_Z_DOWN_BIT):
task_data.z_offset = -0.0003; break;
default:
task_data.z_offset = 0;
}
}
if( enabled || current_jog_z ){
task_data.rate = 20000;
task_enable(TASK_MANUAL_MOVE, &task_data);
}
if( enabled ){
//
// Trigger the next ADC conversion.
//
if (status & STATUS_CH0_IDLE)
ADCProcessorTrigger(ADC0_BASE, 0);
if (status & STATUS_CH1_IDLE)
ADCProcessorTrigger(ADC0_BASE, 1);
}
}
}
void joystick_init(void) {
// Register Joystick button isr
GPIOPinTypeGPIOInput(JOY_PORT, JOY_MASK);
GPIOPadConfigSet(JOY_PORT, JOY_MASK, GPIO_STRENGTH_4MA, GPIO_PIN_TYPE_STD_WPU);
GPIOIntTypeSet(JOY_PORT, JOY_MASK, GPIO_BOTH_EDGES);
GPIOIntRegister(JOY_PORT, button_handler);
GPIOIntEnable(JOY_PORT, JOY_MASK);
//
// The ADC0 peripheral must be enabled for use.
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);
//
// Select the analog ADC function for these pins.
//
GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_0);
GPIOPinTypeADC(GPIO_PORTD_BASE, GPIO_PIN_3);
// Use sequences 0 and 1 for x and y.
ADCSequenceConfigure(ADC0_BASE, 0, ADC_TRIGGER_PROCESSOR, 0);
ADCSequenceConfigure(ADC0_BASE, 1, ADC_TRIGGER_PROCESSOR, 0);
// Single ended sample on CH3 (X) and CH4 (Y).
ADCSequenceStepConfigure(ADC0_BASE, 0, 0, ADC_CTL_CH3 | ADC_CTL_IE | ADC_CTL_END);
ADCSequenceStepConfigure(ADC0_BASE, 1, 0, ADC_CTL_CH4 | ADC_CTL_IE | ADC_CTL_END);
// Enable the sequences.
ADCSequenceEnable(ADC0_BASE, 0);
ADCSequenceEnable(ADC0_BASE, 1);
// Register ISRs.
ADCIntRegister(ADC0_BASE, 0, x_handler);
ADCIntRegister(ADC0_BASE, 1, y_handler);
ADCIntEnable(ADC0_BASE, 0);
ADCIntEnable(ADC0_BASE, 1);
// Trigger the first conversion (auto-center)
ADCProcessorTrigger(ADC0_BASE, 0);
ADCProcessorTrigger(ADC0_BASE, 1);
// Configure timer
SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER3);
TimerConfigure(JOY_TIMER, TIMER_CFG_PERIODIC);
//Register Jog Z buttons
GPIOPinTypeGPIOInput(JOG_Z_PORT, JOG_Z_MASK);
GPIOIntTypeSet(JOG_Z_PORT, JOG_Z_MASK, GPIO_BOTH_EDGES);
GPIOIntRegister(JOG_Z_PORT, jog_z_handler);
GPIOIntEnable(JOG_Z_PORT, JOG_Z_MASK);
GPIOPadConfigSet(JOG_Z_PORT,JOG_Z_MASK,GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_STD_WPD);
// Create a 10ms timer callback
TimerLoadSet64(JOY_TIMER, SysCtlClockGet() / 500);
TimerIntRegister(JOY_TIMER, TIMER_A, joystick_isr);
TimerIntEnable(JOY_TIMER, TIMER_TIMA_TIMEOUT);
IntPrioritySet(INT_TIMER3A, CONFIG_JOY_PRIORITY);
TimerEnable(JOY_TIMER, TIMER_A);
}
bool joystick_is_enabled (void) {
if (enabled == 1) {
return true;
}
else {
return false;
}
}
void joystick_enable(void){
paused = false;
// enabled = 1;
GPIOIntEnable(JOY_PORT, JOY_MASK);
ADCIntEnable(ADC0_BASE, 0);
ADCIntEnable(ADC0_BASE, 1);
GPIOIntEnable(JOG_Z_PORT, JOG_Z_MASK);
}
void joystick_disable(void){
paused = true;
// enabled = 0;
GPIOIntDisable(JOY_PORT, JOY_MASK);
ADCIntDisable(ADC0_BASE, 0);
ADCIntDisable(ADC0_BASE, 1);
GPIOIntDisable(JOG_Z_PORT, JOG_Z_MASK);
}