MARIOS SATANAS / Mbed 2 deprecated USBJoystick_mod

Dependencies:   USBDevice mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "USBJoystick.h"
00003 
00004 USBJoystick joystick;
00005 // Variables for Heartbeat and Status monitoring
00006 DigitalOut myled1(LED1);
00007 DigitalOut myled2(LED2);
00008 DigitalOut myled3(LED3);
00009 DigitalOut heartbeatLED(LED4);
00010 #define IN_UP PC_0
00011 #define IN_RIGHT PC_1
00012 #define IN_DOWN PB_0
00013 #define IN_LEFT PA_4
00014 
00015 #define UP 0x01
00016 #define RIGHT 0x02
00017 #define DOWN 0x04
00018 #define LEFT 0x08
00019 #ifdef TARGET_STM32F4
00020 BusIn hatin(PC_0, PC_1, PB_0, PA_4);
00021 BusIn buttonin(PH_0, PA_15, PB_7, PH_1, PC_3, PC_2, PC_15, PC_14, PA_13, PA_14);
00022 DigitalIn psbutton(PC_12);
00023 #endif
00024 #ifdef TARGET_KL25Z
00025 BusIn hatin(PTC1, PTC2, PTB3, PTB2);
00026 BusIn buttonin(PTE30, PTE29, PTE23, PTE22, PTE21, PTE20, PTE5, PTE4, PTE3, PTE2);
00027 DigitalIn psbutton(PTB8);
00028 DigitalOut extraGround(PTD3);
00029 #endif
00030 
00031 //Ticker heartbeat;
00032 
00033 // Heartbeat monitor
00034 /*
00035 void pulse() {
00036   heartbeatLED = !heartbeatLED;
00037 }
00038 
00039 void heartbeat_start() {
00040   heartbeat.attach(&pulse, 0.5);
00041 }
00042 
00043 void heartbeat_stop() {
00044   heartbeat.detach();
00045 }
00046 */
00047 
00048 int main() {
00049     hatin.mode(PullUp);
00050     buttonin.mode(PullUp);
00051     psbutton.mode(PullUp);
00052     #ifdef TARGET_KL25Z
00053     extraGround.write(0);
00054     #endif
00055     heartbeatLED = 0;
00056     #define UPDATEINTERVAL 0.002
00057     int i = 0;
00058     int16_t throttle = 0;
00059     int16_t rudder = 0;    
00060     int16_t x = 0;
00061     int16_t y = 0;
00062 
00063     int16_t button = 0;    
00064     int8_t hat = 0;
00065     int8_t hat_reverse = 0;    
00066     int8_t isUp = 0;
00067     int8_t isDown = 0;
00068     int8_t isLeft = 0;
00069     int8_t isRight = 0;
00070     int16_t ps = 0;
00071     
00072     //heartbeat_start();
00073 
00074     while (1) {
00075 
00076         throttle = 0;
00077         rudder = 0;
00078         x = 0;
00079         y = 0;
00080         
00081         button = 0;
00082         button = ( ~buttonin) & 0x03ff;
00083         ps = (psbutton)? 0 : 1;
00084         button |= ( ps << 12);
00085         hat_reverse = (~hatin) & 0x0F ;        
00086         isUp = isDown = isLeft = isRight = 0;
00087         if (hat_reverse & DOWN){
00088             isDown = 1;
00089         }
00090         if (hat_reverse & UP){
00091             isUp =1;
00092             isDown = 0;
00093         }
00094         
00095         if (hat_reverse & RIGHT){
00096             isRight = 1;
00097         }
00098         if (hat_reverse & LEFT){
00099             isLeft = 1;
00100         }
00101         if (isLeft && isRight){
00102             isLeft = 0;
00103             isRight = 0;
00104         }
00105         
00106         if (isUp && isLeft) {
00107             hat = 7;
00108         } else if (isUp && isRight){
00109             hat = 1;
00110         } else if (isDown && isLeft){
00111             hat = 5;
00112         } else if (isDown && isRight){
00113             hat = 3;
00114         } else if (isUp){
00115             hat = 0;
00116         } else if (isDown){
00117             hat = 4;
00118         } else if (isRight){
00119             hat = 2;
00120         } else if (isLeft){
00121             hat = 6;
00122         } else {
00123             hat = 9;
00124         }
00125         
00126         joystick.update(throttle, rudder, x, y, button, hat);
00127         i = (i + 1) % 500 ;
00128         if (i == 0){
00129               heartbeatLED = !heartbeatLED;                
00130         }      
00131         wait(UPDATEINTERVAL);
00132     }
00133                      
00134 }