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.
Diff: main.cpp
- Revision:
- 3:9fe0f8f806db
- Parent:
- 2:5d414d475e02
--- a/main.cpp Thu Oct 22 19:03:23 2020 +0000
+++ b/main.cpp Thu May 06 08:24:38 2021 +0000
@@ -2,35 +2,59 @@
#include "usbhid.h"
DigitalOut led(LED1);
+DigitalOut nCS(p11);
+DigitalOut jstkCS (p8);
+SPI jstk (p5,p6,p7);
+Serial pc(USBTX, USBRX);
-BusIn buttons(
- p5, p6, p7, p8, // PS3: Square, x, o, Triangle
- p9, p10, p11, p12, // PS3: L1, R1, L2, R2
- p13, p14, p15, p16 // PS3: SELECT, START, L3, R3
-);
+
-AnalogIn analog_x(p17); // PS3: Left Analog Stick X-axis
-AnalogIn analog_y(p18); // PS3: Left Analog Stick Y-axis
-AnalogIn analog_z(p19); // PS3: Right Analog Stick X-axis
-AnalogIn analog_rz(p20); // PS3: Right Analog Stick Y-axis
+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
-BusIn stick(
- p15, p12, p13, p16 // PS3: Up, Down, Left, Right
-);
+ *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) {
- signed char x = (analog_x.read_u16() >> 8) - 0x80;
- signed char y = (analog_y.read_u16() >> 8) - 0x80;
- signed char z = (analog_z.read_u16() >> 8) - 0x80;
- signed char rz = (analog_rz.read_u16() >> 8) - 0x80;
-
- joystick.joystick(0,stick.read() , x, y, z, rz);
-
- led = (buttons > 0 || stick > 0) ? 1 : 0;
-
- wait(0.01);
- }
+ 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);
+ }
}