Statistics
| Revision:

root / trunk / code / projects / colonet / robot / joystick / discrete_control / joystick.c @ 874

History | View | Annotate | Download (4.12 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
int joystick_status (void);
33
int joystick_change (int old, int new);
34

    
35
int main (void) {
36
  dragonfly_init(SERIAL | USB | COMM | ORB | ANALOG);
37
        ADCSRA = _BV(ADEN) | _BV(ADPS2) | _BV(ADPS1) | _BV(ADPS0);
38
        xbee_init();
39

    
40
  orb_set_color(BLUE);
41
  
42
  int change;
43
  int last_status = 0; 
44
  int cur_status = 0;
45
  
46
  while (1) {
47
    last_status = cur_status;
48
    cur_status = joystick_status();
49
    change = joystick_change(last_status, cur_status);
50
    
51
    char buf[100];
52
    sprintf(buf, "status: %d\n", cur_status);
53
    usb_puts(buf);
54
    
55
    if (change & MOVED_UP) {
56
      orb_set_color(GREEN);
57
      xbee_putc('f');
58
    }
59
    if (change & MOVED_Y_CENTER) {
60
      orb_set_color(RED);
61
      xbee_putc('s');
62
    }
63
    if (change & MOVED_DOWN) {
64
      orb_set_color(BLUE);
65
      xbee_putc('b');
66
    }
67
    if (change & MOVED_LEFT) {
68
      orb_set_color(ORANGE);
69
      xbee_putc('l');
70
    }
71
    if (change & MOVED_X_CENTER) {
72
      orb_set_color(RED);
73
      xbee_putc('s');
74
    }
75
    if (change & MOVED_RIGHT) {
76
      orb_set_color(YELLOW);
77
      xbee_putc('r');
78
    }
79
    if (change & TRIGGER_PRESSED) {
80
      orb_set_color(PURPLE);
81
      xbee_putc('t');
82
    }
83
    if (change & BUTTON_PRESSED) {
84
      orb_set_color(YELLOW);
85
      xbee_putc('u');
86
    }
87
    delay_ms(100);
88
  }
89
  
90
}
91

    
92
int joystick_change (int old, int new) {
93
  int trigger_change = 0;
94
  int button_change = 0;
95
  int x_change = 0;
96
  int y_change = 0;
97
  /* Check for trigger change */
98
  if ((old & TRIGGER_DOWN) && (new & TRIGGER_UP))
99
    trigger_change = TRIGGER_RELEASED;
100
  if ((old & TRIGGER_UP) && (new & TRIGGER_DOWN)) 
101
    trigger_change = TRIGGER_PRESSED;
102
  /* Check for button change */
103
  if ((old & BUTTON_DOWN) && (new & BUTTON_UP))
104
    button_change = BUTTON_RELEASED;
105
  if ((old & BUTTON_UP) && (new & BUTTON_DOWN))
106
    button_change = BUTTON_PRESSED;
107
  /* Check for X change */
108
  if ((old & X_CENTER) && (new & X_RIGHT))
109
    x_change = MOVED_RIGHT;
110
  if ((old & X_CENTER) && (new & X_LEFT))
111
    x_change = MOVED_LEFT;
112
  if (!(old & X_CENTER) && (new & X_CENTER))
113
    x_change = MOVED_X_CENTER;
114
  /* Check for Y change */
115
  if ((old & Y_CENTER) && (new & Y_UP))
116
    y_change = MOVED_UP;
117
  if ((old & Y_CENTER) && (new & Y_DOWN))
118
    y_change = MOVED_DOWN;
119
  if (!(old & Y_CENTER) && (new & Y_CENTER))
120
    y_change = MOVED_Y_CENTER;
121
  return trigger_change | button_change | x_change | y_change;
122
}
123

    
124
/**
125
 * Get the current status of the joystick.
126
 * This includes the X and Y axes and the buttons.
127
 */
128
int joystick_status (void) {
129
        int trigger_status = 0;
130
        int button_status = 0;
131
        int x_status = 0; 
132
        int y_status = 0;
133
        int xval, yval;
134
    
135
  if (analog_get8(AN0) > 200)
136
    trigger_status = TRIGGER_DOWN;
137
  else
138
    trigger_status = TRIGGER_UP;
139
    
140
  if (analog_get8(AN1) > 200)
141
    button_status = BUTTON_DOWN;
142
  else
143
    button_status = BUTTON_UP;
144
  
145
  xval = analog_get8(AN4); // actually AN6, AN7 for connection
146
  yval = analog_get8(AN5);
147
  
148
  if (xval > 150)
149
    x_status = X_LEFT;
150
  else if (xval < 75)
151
    x_status = X_RIGHT;
152
  else
153
    x_status = X_CENTER;
154
  
155
  if (yval > 150)
156
    y_status = Y_UP;
157
  else if (yval < 75)
158
    y_status = Y_DOWN;
159
  else
160
    y_status = Y_CENTER;
161

    
162

    
163
  return trigger_status | button_status | x_status | y_status;
164
}