ElHadji MBAYE / Mbed 2 deprecated USBJoystick2_POPS

Dependencies:   mbed

Committer:
elhadji
Date:
Thu May 06 08:24:38 2021 +0000
Revision:
3:9fe0f8f806db
Parent:
2:5d414d475e02
prog projet APP5 POPS

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bricklife 0:d450e82033a1 1 #include "mbed.h"
bricklife 0:d450e82033a1 2 #include "usbhid.h"
bricklife 0:d450e82033a1 3
bricklife 0:d450e82033a1 4 DigitalOut led(LED1);
elhadji 3:9fe0f8f806db 5 DigitalOut nCS(p11);
elhadji 3:9fe0f8f806db 6 DigitalOut jstkCS (p8);
elhadji 3:9fe0f8f806db 7 SPI jstk (p5,p6,p7);
elhadji 3:9fe0f8f806db 8 Serial pc(USBTX, USBRX);
bricklife 0:d450e82033a1 9
elhadji 3:9fe0f8f806db 10
bricklife 0:d450e82033a1 11
elhadji 3:9fe0f8f806db 12 void JSTK2_init(){
elhadji 3:9fe0f8f806db 13 jstk.format(8,0) ; // format 8 bits Mode0
elhadji 3:9fe0f8f806db 14 jstk.frequency(1000000);
elhadji 3:9fe0f8f806db 15 jstkCS=1;
elhadji 3:9fe0f8f806db 16 wait_us(25);
elhadji 3:9fe0f8f806db 17 }
elhadji 3:9fe0f8f806db 18 //read X axis, Y axis and button states
elhadji 3:9fe0f8f806db 19 void JSTK2_read(int* X, int* Y, char* B) {
elhadji 3:9fe0f8f806db 20 unsigned char data[5];
elhadji 3:9fe0f8f806db 21 jstkCS=0;
elhadji 3:9fe0f8f806db 22 wait_us(15); // see doc: wait 15us after activation of CS line
elhadji 3:9fe0f8f806db 23 for (short i = 0; i < 5; i++) { // get 5 bytes of data
elhadji 3:9fe0f8f806db 24 data[i] = jstk.write(0);
elhadji 3:9fe0f8f806db 25 wait_us(10); // see doc: wait 10us after sending each data
elhadji 3:9fe0f8f806db 26 }
elhadji 3:9fe0f8f806db 27 jstkCS=1; // deactivation of CS line
bricklife 0:d450e82033a1 28
elhadji 3:9fe0f8f806db 29 *X = (((unsigned int)data[1]) << 8) | ((unsigned int)data[0]); //recunstruct 10-bit X value
elhadji 3:9fe0f8f806db 30 *Y = (((unsigned int)data[3]) << 8) | ((unsigned int)data[2]); //recunstruct 10-bit Y value
elhadji 3:9fe0f8f806db 31
elhadji 3:9fe0f8f806db 32 //Offset pour avoir + ou - suivant le sens du mouvement
elhadji 3:9fe0f8f806db 33 *X -= 512;
elhadji 3:9fe0f8f806db 34 *Y -= 512;
elhadji 3:9fe0f8f806db 35
elhadji 3:9fe0f8f806db 36 *B = data[4]; //0: button, 1: trigger, 2: joystick
elhadji 3:9fe0f8f806db 37 }
bricklife 0:d450e82033a1 38
bricklife 0:d450e82033a1 39 USBJoystick joystick;
bricklife 0:d450e82033a1 40
elhadji 3:9fe0f8f806db 41 int xv,yv;
elhadji 3:9fe0f8f806db 42 char bv;
bricklife 0:d450e82033a1 43 int main() {
elhadji 3:9fe0f8f806db 44 nCS=1;
elhadji 3:9fe0f8f806db 45 JSTK2_init();
bricklife 0:d450e82033a1 46 while(1) {
elhadji 3:9fe0f8f806db 47 JSTK2_read(&xv,&yv,&bv);
elhadji 3:9fe0f8f806db 48 //pc.printf("%5d %5d %03x\r\n",xv,yv,(char*)bv);
elhadji 3:9fe0f8f806db 49 //JSTK2_light(xv,yv);
elhadji 3:9fe0f8f806db 50
elhadji 3:9fe0f8f806db 51 signed char x = xv/4;
elhadji 3:9fe0f8f806db 52 signed char y = yv/4;
elhadji 3:9fe0f8f806db 53 signed char z = 0;
elhadji 3:9fe0f8f806db 54 signed char rz =0;
elhadji 3:9fe0f8f806db 55 unsigned short buttons = bv&0x03;
elhadji 3:9fe0f8f806db 56 led = buttons;
elhadji 3:9fe0f8f806db 57 joystick.joystick(0,buttons, x, y, z, rz);
elhadji 3:9fe0f8f806db 58 wait(0.05);
elhadji 3:9fe0f8f806db 59 }
bricklife 0:d450e82033a1 60 }