Statistics
| Revision:

root / demos / joystick / src / joystick / discrete_control / joystick.c @ 1741

History | View | Annotate | Download (4.46 KB)

1
#include <dragonfly_lib.h>
2
#include <wireless.h>
3
#include <wl_defs.h>
4
#include "../../common/colonet_defs.h"
5
#include <serial.h>
6
#include <stdio.h>
7

    
8
/* Current state codes */
9
#define TRIGGER_DOWN              1
10
#define TRIGGER_UP               (1<<1)
11
#define BUTTON_DOWN              (1<<2)
12
#define BUTTON_UP                (1<<3)
13
#define X_LEFT                   (1<<4)
14
#define X_CENTER                 (1<<5)
15
#define X_RIGHT                  (1<<6)
16
#define Y_UP                     (1<<7)
17
#define Y_CENTER                 (1<<8)
18
#define Y_DOWN                   (1<<9)
19

    
20
/* State change codes */
21
#define TRIGGER_PRESSED           1
22
#define TRIGGER_RELEASED          (1<<1)
23
#define BUTTON_PRESSED            (1<<2)
24
#define BUTTON_RELEASED           (1<<3)
25
#define MOVED_LEFT                (1<<4)
26
#define MOVED_X_CENTER            (1<<5)
27
#define MOVED_RIGHT               (1<<6)
28
#define MOVED_UP                  (1<<7)
29
#define MOVED_Y_CENTER            (1<<8)
30
#define MOVED_DOWN                (1<<9)
31

    
32
/* Command Types */
33
#define CNTL_FORWARD 'f'
34
#define CNTL_STOP    's'
35
#define CNTL_BACK    'b'
36
#define CNTL_LEFT    'l'
37
#define CNTL_RIGHT   'r'
38
#define CNTL_BUZZ0   't'
39
#define CNTL_BUZZ1   'u'
40

    
41
#define WL_CNTL_GROUP 4
42

    
43
int joystick_status (void);
44
int joystick_change (int old, int new);
45

    
46
PacketGroupHandler cntlHandler = {WL_CNTL_GROUP, NULL, NULL, NULL, NULL};
47

    
48
int main (void) {
49
          dragonfly_init(SERIAL | USB | COMM | ORB | ANALOG);
50
        ADCSRA = _BV(ADEN) | _BV(ADPS2) | _BV(ADPS1) | _BV(ADPS0);
51
          wl_init();
52
          wl_register_packet_group(&cntlHandler);
53

    
54
          orb_set_color(BLUE);
55
  
56
          int change;
57
          int last_status = 0; 
58
          int cur_status = 0;
59
  
60
        while (1) {
61
                last_status = cur_status;
62
                cur_status = joystick_status();
63
                change = joystick_change(last_status, cur_status);
64

    
65
                if (change & MOVED_UP) {
66
                        orb_set_color(GREEN);
67
                        wl_send_global_packet(4, CNTL_FORWARD, NULL, 0, 0);
68
                }
69
                if (change & MOVED_Y_CENTER) {
70
                        orb_set_color(RED);
71
                        wl_send_global_packet(4, CNTL_STOP, NULL, 0, 0);
72
                }
73
                if (change & MOVED_DOWN) {
74
                        orb_set_color(BLUE);
75
                        wl_send_global_packet(4, CNTL_BACK, NULL, 0, 0);
76
                }
77
                if (change & MOVED_LEFT) {
78
                        orb_set_color(ORANGE);
79
                        wl_send_global_packet(4, CNTL_LEFT, NULL, 0, 0);
80
                }
81
                if (change & MOVED_X_CENTER) {
82
                        orb_set_color(RED);
83
                        wl_send_global_packet(4, CNTL_STOP, NULL, 0, 0);
84
                }
85
                if (change & MOVED_RIGHT) {
86
                        orb_set_color(YELLOW);
87
                        wl_send_global_packet(4, CNTL_RIGHT, NULL, 0, 0);
88
                }
89
                if (change & TRIGGER_PRESSED) {
90
                        orb_set_color(PURPLE);
91
                        wl_send_global_packet(4, CNTL_BUZZ0, NULL, 0, 0);
92
                }
93
                if (change & BUTTON_PRESSED) {
94
                        orb_set_color(YELLOW);
95
                        wl_send_global_packet(4, CNTL_STOP, NULL, 0, 0);
96
                }
97
                delay_ms(100);
98
        }
99
  
100
}
101

    
102
int joystick_change (int old, int new) {
103
        int trigger_change = 0;
104
        int button_change = 0;
105
        int x_change = 0;
106
        int y_change = 0;
107
        /* Check for trigger change */
108
        if ((old & TRIGGER_DOWN) && (new & TRIGGER_UP))
109
                trigger_change = TRIGGER_RELEASED;
110
        if ((old & TRIGGER_UP) && (new & TRIGGER_DOWN)) 
111
                trigger_change = TRIGGER_PRESSED;
112
        /* Check for button change */
113
        if ((old & BUTTON_DOWN) && (new & BUTTON_UP))
114
                button_change = BUTTON_RELEASED;
115
        if ((old & BUTTON_UP) && (new & BUTTON_DOWN))
116
                button_change = BUTTON_PRESSED;
117
        /* Check for X change */
118
        if ((old & X_CENTER) && (new & X_RIGHT))
119
                x_change = MOVED_RIGHT;
120
        if ((old & X_CENTER) && (new & X_LEFT))
121
                x_change = MOVED_LEFT;
122
        if (!(old & X_CENTER) && (new & X_CENTER))
123
                x_change = MOVED_X_CENTER;
124
        /* Check for Y change */
125
        if ((old & Y_CENTER) && (new & Y_UP))
126
                y_change = MOVED_UP;
127
        if ((old & Y_CENTER) && (new & Y_DOWN))
128
                y_change = MOVED_DOWN;
129
        if (!(old & Y_CENTER) && (new & Y_CENTER))
130
                y_change = MOVED_Y_CENTER;
131
        return trigger_change | button_change | x_change | y_change;
132
}
133

    
134
/**
135
 * Get the current status of the joystick.
136
 * This includes the X and Y axes and the buttons.
137
 */
138
int joystick_status (void) {
139
        int trigger_status = 0;
140
        int button_status = 0;
141
        int x_status = 0; 
142
        int y_status = 0;
143
        int xval, yval;
144
    
145
        if (analog_get8(AN0) > 200)
146
                trigger_status = TRIGGER_DOWN;
147
        else
148
                trigger_status = TRIGGER_UP;
149

    
150
        if (analog_get8(AN1) > 200)
151
                button_status = BUTTON_DOWN;
152
        else
153
                button_status = BUTTON_UP;
154

    
155
        xval = analog_get8(AN4); // actually AN6, AN7 for connection
156
        yval = analog_get8(AN5);
157

    
158
        if (xval > 150)
159
                x_status = X_LEFT;
160
        else if (xval < 75)
161
                x_status = X_RIGHT;
162
        else
163
                x_status = X_CENTER;
164

    
165
        if (yval > 150)
166
                y_status = Y_UP;
167
        else if (yval < 75)
168
                y_status = Y_DOWN;
169
        else
170
                y_status = Y_CENTER;
171

    
172

    
173
        return trigger_status | button_status | x_status | y_status;
174
}
175