Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
- Committer:
- elhadji
- Date:
- 2021-05-06
- Revision:
- 3:9fe0f8f806db
- Parent:
- 2:5d414d475e02
File content as of revision 3:9fe0f8f806db:
#include "mbed.h"
#include "usbhid.h"
DigitalOut led(LED1);
DigitalOut nCS(p11);
DigitalOut jstkCS (p8);
SPI jstk (p5,p6,p7);
Serial pc(USBTX, USBRX);
void JSTK2_init(){
jstk.format(8,0) ; // format 8 bits Mode0
jstk.frequency(1000000);
jstkCS=1;
wait_us(25);
}
//read X axis, Y axis and button states
void JSTK2_read(int* X, int* Y, char* B) {
unsigned char data[5];
jstkCS=0;
wait_us(15); // see doc: wait 15us after activation of CS line
for (short i = 0; i < 5; i++) { // get 5 bytes of data
data[i] = jstk.write(0);
wait_us(10); // see doc: wait 10us after sending each data
}
jstkCS=1; // deactivation of CS line
*X = (((unsigned int)data[1]) << 8) | ((unsigned int)data[0]); //recunstruct 10-bit X value
*Y = (((unsigned int)data[3]) << 8) | ((unsigned int)data[2]); //recunstruct 10-bit Y value
//Offset pour avoir + ou - suivant le sens du mouvement
*X -= 512;
*Y -= 512;
*B = data[4]; //0: button, 1: trigger, 2: joystick
}
USBJoystick joystick;
int xv,yv;
char bv;
int main() {
nCS=1;
JSTK2_init();
while(1) {
JSTK2_read(&xv,&yv,&bv);
//pc.printf("%5d %5d %03x\r\n",xv,yv,(char*)bv);
//JSTK2_light(xv,yv);
signed char x = xv/4;
signed char y = yv/4;
signed char z = 0;
signed char rz =0;
unsigned short buttons = bv&0x03;
led = buttons;
joystick.joystick(0,buttons, x, y, z, rz);
wait(0.05);
}
}