Satellite Observers Workbench. NOT yet complete, just published for forum posters to \"cherry pick\" pieces of code as requiered as an example.

Dependencies:   mbed

Committer:
AjK
Date:
Mon Oct 11 10:34:55 2010 +0000
Revision:
0:0a841b89d614
Totally Alpha quality as this project isn\t completed. Just publishing it as it answers many questions asked in the forums

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AjK 0:0a841b89d614 1 /****************************************************************************
AjK 0:0a841b89d614 2 * Copyright 2010 Andy Kirkham, Stellar Technologies Ltd
AjK 0:0a841b89d614 3 *
AjK 0:0a841b89d614 4 * This file is part of the Satellite Observers Workbench (SOWB).
AjK 0:0a841b89d614 5 *
AjK 0:0a841b89d614 6 * SOWB is free software: you can redistribute it and/or modify
AjK 0:0a841b89d614 7 * it under the terms of the GNU General Public License as published by
AjK 0:0a841b89d614 8 * the Free Software Foundation, either version 3 of the License, or
AjK 0:0a841b89d614 9 * (at your option) any later version.
AjK 0:0a841b89d614 10 *
AjK 0:0a841b89d614 11 * SOWB is distributed in the hope that it will be useful,
AjK 0:0a841b89d614 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
AjK 0:0a841b89d614 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
AjK 0:0a841b89d614 14 * GNU General Public License for more details.
AjK 0:0a841b89d614 15 *
AjK 0:0a841b89d614 16 * You should have received a copy of the GNU General Public License
AjK 0:0a841b89d614 17 * along with SOWB. If not, see <http://www.gnu.org/licenses/>.
AjK 0:0a841b89d614 18 *
AjK 0:0a841b89d614 19 * $Id: main.cpp 5 2010-07-12 20:51:11Z ajk $
AjK 0:0a841b89d614 20 *
AjK 0:0a841b89d614 21 ***************************************************************************/
AjK 0:0a841b89d614 22
AjK 0:0a841b89d614 23 #ifndef TH_XBOX360GAMEPAD_C
AjK 0:0a841b89d614 24 #define TH_XBOX360GAMEPAD_C
AjK 0:0a841b89d614 25 #endif
AjK 0:0a841b89d614 26
AjK 0:0a841b89d614 27 #include "mbed.h"
AjK 0:0a841b89d614 28 #include "usbeh.h"
AjK 0:0a841b89d614 29 #include "usbeh_endpoint.h"
AjK 0:0a841b89d614 30 #include "usbeh_device.h"
AjK 0:0a841b89d614 31 #include "usbeh_controller.h"
AjK 0:0a841b89d614 32 #include "usbeh_api.h"
AjK 0:0a841b89d614 33 #include "xbox360gamepad.h"
AjK 0:0a841b89d614 34 #include "th_xbox360gamepad.h"
AjK 0:0a841b89d614 35
AjK 0:0a841b89d614 36 #include "main.h"
AjK 0:0a841b89d614 37 #include "debug.h"
AjK 0:0a841b89d614 38
AjK 0:0a841b89d614 39
AjK 0:0a841b89d614 40 const char *button_text[] = { "","LS","RS","XBOX","Unused","A","B","X","Y","DPAD UP","DPAD DOWN","DPAD LEFT","DPAG RIGHT","START","BACK","LEFT HAT","RIGHT HAT" };
AjK 0:0a841b89d614 41
AjK 0:0a841b89d614 42 /* Define globals to hold Xbox360 stick data. */
AjK 0:0a841b89d614 43 XBOX360_STICK *stick;
AjK 0:0a841b89d614 44 XBOX360_STICK stick_left_previous;
AjK 0:0a841b89d614 45 XBOX360_STICK stick_right_previous;
AjK 0:0a841b89d614 46 unsigned char trigger_left = 0, trigger_left_last = 0;
AjK 0:0a841b89d614 47 unsigned char trigger_right = 0, trigger_right_last = 0;
AjK 0:0a841b89d614 48
AjK 0:0a841b89d614 49 void th_xbox360gamepad_init(void) {
AjK 0:0a841b89d614 50 stick = xbox360gamepad_get_stick_left();
AjK 0:0a841b89d614 51 stick_left_previous.x = stick->x;
AjK 0:0a841b89d614 52 stick_left_previous.y = stick->y;
AjK 0:0a841b89d614 53 stick = xbox360gamepad_get_stick_right();
AjK 0:0a841b89d614 54 stick_right_previous.x = stick->x;
AjK 0:0a841b89d614 55 stick_right_previous.y = stick->y;
AjK 0:0a841b89d614 56 }
AjK 0:0a841b89d614 57
AjK 0:0a841b89d614 58 void th_xbox360gamepad(void) {
AjK 0:0a841b89d614 59 unsigned char button;
AjK 0:0a841b89d614 60 if ((button = xbox360gamepad_get_button()) != 0) {
AjK 0:0a841b89d614 61 if (button > 0) {
AjK 0:0a841b89d614 62 debug_printf("Button ");
AjK 0:0a841b89d614 63 if (button > (BUTT_RIGHT_HAT_PRESS + 16)) {
AjK 0:0a841b89d614 64 debug_printf("%s held\r\n", button_text[button - 32]);
AjK 0:0a841b89d614 65 }
AjK 0:0a841b89d614 66 else if (button > BUTT_RIGHT_HAT_PRESS) {
AjK 0:0a841b89d614 67 debug_printf("%s released\r\n", button_text[button - 16]);
AjK 0:0a841b89d614 68 }
AjK 0:0a841b89d614 69 else {
AjK 0:0a841b89d614 70 debug_printf("%s pressed\r\n", button_text[button]);
AjK 0:0a841b89d614 71 switch (button) {
AjK 0:0a841b89d614 72 case BUTT_A_PRESS:
AjK 0:0a841b89d614 73 xbox360gamepad_led(LED_1_FLASH_THEN_ON);
AjK 0:0a841b89d614 74 break;
AjK 0:0a841b89d614 75 case BUTT_B_PRESS:
AjK 0:0a841b89d614 76 xbox360gamepad_led(LED_2_FLASH_THEN_ON);
AjK 0:0a841b89d614 77 break;
AjK 0:0a841b89d614 78 case BUTT_X_PRESS:
AjK 0:0a841b89d614 79 xbox360gamepad_led(LED_3_FLASH_THEN_ON);
AjK 0:0a841b89d614 80 break;
AjK 0:0a841b89d614 81 case BUTT_Y_PRESS:
AjK 0:0a841b89d614 82 xbox360gamepad_led(LED_4_FLASH_THEN_ON);
AjK 0:0a841b89d614 83 break;
AjK 0:0a841b89d614 84 }
AjK 0:0a841b89d614 85 }
AjK 0:0a841b89d614 86 }
AjK 0:0a841b89d614 87 }
AjK 0:0a841b89d614 88
AjK 0:0a841b89d614 89 if ((trigger_left = xbox360gamepad_get_trigger_left()) != trigger_left_last) {
AjK 0:0a841b89d614 90 debug_printf("Left trigger: %d\r\n", trigger_left);
AjK 0:0a841b89d614 91 trigger_left_last = trigger_left;
AjK 0:0a841b89d614 92 }
AjK 0:0a841b89d614 93
AjK 0:0a841b89d614 94 if ((trigger_right = xbox360gamepad_get_trigger_right()) != trigger_right_last) {
AjK 0:0a841b89d614 95 debug_printf("Right trigger: %d\r\n", trigger_right);
AjK 0:0a841b89d614 96 trigger_right_last = trigger_right;
AjK 0:0a841b89d614 97 }
AjK 0:0a841b89d614 98
AjK 0:0a841b89d614 99 unsigned char xbox360gamepad_get_trigger_right(void);
AjK 0:0a841b89d614 100
AjK 0:0a841b89d614 101 stick = xbox360gamepad_get_stick_left();
AjK 0:0a841b89d614 102 if (stick->x/STICK_DIVISOR != stick_left_previous.x/STICK_DIVISOR || stick->y/STICK_DIVISOR != stick_left_previous.y/STICK_DIVISOR) {
AjK 0:0a841b89d614 103 stick_left_previous.x = stick->x;
AjK 0:0a841b89d614 104 stick_left_previous.y = stick->y;
AjK 0:0a841b89d614 105 // Don't bother printing for now, the sticks are too sensitive!
AjK 0:0a841b89d614 106 int x = stick->x/STICK_DIVISOR, y = stick->y/STICK_DIVISOR;
AjK 0:0a841b89d614 107 if (1 || (x > 10 || x < -10) || (y > 10 || y < -10) ) {
AjK 0:0a841b89d614 108 debug_printf("New LEFT stick position x = %d y = %d\r\n", stick->x/STICK_DIVISOR, stick->y/STICK_DIVISOR);
AjK 0:0a841b89d614 109 }
AjK 0:0a841b89d614 110 }
AjK 0:0a841b89d614 111
AjK 0:0a841b89d614 112 stick = xbox360gamepad_get_stick_right();
AjK 0:0a841b89d614 113 if (stick->x/STICK_DIVISOR != stick_right_previous.x/STICK_DIVISOR || stick->y/STICK_DIVISOR != stick_right_previous.y/STICK_DIVISOR) {
AjK 0:0a841b89d614 114 stick_right_previous.x = stick->x;
AjK 0:0a841b89d614 115 stick_right_previous.y = stick->y;
AjK 0:0a841b89d614 116 // Don't bother printing for now, the sticks are too sensitive!
AjK 0:0a841b89d614 117 int x = stick->x/STICK_DIVISOR, y = stick->y/STICK_DIVISOR;
AjK 0:0a841b89d614 118 if (1 || (x > 10 || x < -10) || (y > 10 || y < -10) ) {
AjK 0:0a841b89d614 119 debug_printf("New RIGHT stick position x = %d y = %d\r\n", stick->x/STICK_DIVISOR, stick->y/STICK_DIVISOR);
AjK 0:0a841b89d614 120 }
AjK 0:0a841b89d614 121 }
AjK 0:0a841b89d614 122 }
AjK 0:0a841b89d614 123