Hi Vlad,
Aaron has done a great job of identifying some of the problems. I just imported your code, made these changes, and added some more to make it allcompile ok. Here is the result:
PlayStationController
Note, just because it compiles, that doesn't mean it does the right thing!
Here are the main changes I made:
I moved gameByte() earlier in the main.cpp file so it is defined by the time it is used, and changed the return type from void to char.
There was a function (or macro) named _BV(n), which doesn't exist. A little google suggests this is a bit setting macro (probably "Bit Value" or something), but the logic is basically:
1 << n;
It doesn't seem to save much typing, but I guess people might like to wrap it up. So I just quickly added this to get that working:
int _BV(int bit) {
return 1 << bit;
} These sort of things are often defined as Macros, which are basically a text substitution by a C pre-processor before compiling. I'd encourage defining them as function, as it helps the compiler give you better error reports, type checking etc. Macros are like someone coming in, changing your code, then talking back to you in what it was translated to. People will tell you "it is not as efficient", which in some cases will be true, but in a lot of cases it won't make any difference and you can even encourage the compiler to "inline" functions meaning you get exactly the same result (I'll do a post about this sometime perhaps). But i'm getting of track here...
The printf() statements needed a little modification, from:
case 0:
pc.printf("case 0: ");
pc.printf(data0);
break;
to:
case 0:
pc.printf("case 0: %d\n", data0);
break;
Also, the program didn't have a main() function, and every C program needs one of these. My guess is this code is based on the arduino convention of a hidden main that calls setup then loop, so you need to add:
int main() {
startup();
while(1) {
loop();
}
}
There were probably a few tweaks more, but this was the main crux to get it to compile!
Now, you'll have to start looking at the logical bugs... :)
Simon
Hello,
I have mannaged to get a wireless guitar to work with the arduino using arduino code. It has worked great and i've started to port over the code for the mbed. I have done most of it but there are a few errors that I do not know how to get rid of.
Here is the code as I have it so far:
#include "mbed.h" DigitalOut PSclock(p21); DigitalIn PSdata(p22); DigitalIn PSack(p23); DigitalOut PScommand(p24); DigitalOut PSattention(p25); Serial pc(USBTX, USBRX); // tx, rx int temp, data0, data1, data2, data3, data4, data5, i ,debounceSelect; // enable interupts //sei(); // this loop continues to put PSx controller into analouge mode untill the // controller responds with 0x73 in the 2nd byte. // (PS2 controller responds with 0x73 when in analouge mode.) // the status LEDs will continue to count upwards untill a controller is found. // if everything is working correctly this should happen on the first pass of // this loop but occasionally errors occur and a 2nd or 3rd itteration happen. int chk_ana = 0, cnt = 0; void startup() { while (chk_ana != 0x73) { // put controller in config mode PScommand = 1; PSclock = 1; PSattention =0; gameByte(0x01); gameByte(0x43); gameByte(0x00); gameByte(0x01); gameByte(0x00); PScommand = 1; wait(0.000001); PSattention=1; wait(0.00001); // put controller in analog mode PScommand = 1; PSclock =1; PSattention =1;; gameByte(0x01); gameByte(0x44); gameByte(0x00); gameByte(0x01); gameByte(0x03); gameByte(0x00); gameByte(0x00); gameByte(0x00); gameByte(0x00); PScommand = 1; wait(0.000001); PSattention =1; wait(0.00001); // exit config mode PScommand = 1; PSclock = 1; PSattention = 1; gameByte(0x01); gameByte(0x43); gameByte(0x00); gameByte(0x00); gameByte(0x5A); gameByte(0x5A); gameByte(0x5A); gameByte(0x5A); gameByte(0x5A); PScommand = 1; wait(0.000001); PSattention = 1; wait(0.00001); // poll controller and check in analouge mode. PScommand = 1; PSclock=1; PSattention=0; gameByte(0x01); chk_ana = gameByte(0x42); // the 2nd byte to be returned from the controller should = 0x73 for "red" analouge controller. gameByte(0x00); gameByte(0x00); gameByte(0x00); gameByte(0x00); gameByte(0x00); gameByte(0x00); gameByte(0x00); PScommand = 1; wait(1); PSattention =1; wait(0.00001); // keep increasing counter to be dispalyed untill PSx controller confirms it's in analouge mode. pc.putc(cnt++); if (cnt > 254) { cnt=0; } } } // main program loop: void loop () { PScommand=1; PSclock=1; PSattention = 0; gameByte(0x01); // bite 0. header. temp = gameByte(0x42); // bite 1. header. (should possibly put test on this byte to detect unplugging of controller.) gameByte(0x00); // bite 2. header. data0 = gameByte(0x00); // bite 3. first data bite. data1 = gameByte(0x00); // bite 4. data2 = gameByte(0x00); // bite 5. data3 = gameByte(0x00); // bite 6. data4 = gameByte(0x00); // bite 7. data5 = gameByte(0x00); // bite 8. wait(0.000001); PScommand = 1; // close communication with PSx controller wait(0.000001); PSattentio=1; // all done. if (!(data0 & _BV(0)) && !debounceSelect) { // capture one unique press of the "select" button debounceSelect = 1; } else if ((data0 & _BV(0)) && debounceSelect) { if (i++ >= 5) i=0; debounceSelect = 0; } // this switch decides which data register to show on status LEDs depending on how many times // the "select" button on the PS2 controller has been pressed. switch (i) { case 0: pc.printf("case 0: "); pc.printf(data0); break; case 1: pc.printf("case 1: "); pc.printf(dta1); break; case 2: pc.printf("case 2: "); pc.printf(data2); break; case 3: pc.printf("case 3: "); pc.printf(data3); break; case 4: pc.printf("case 4: "); pc.printf(data4); break; case 5: pc.printf("case 5: "); pc.printf(data5); } } //void loop // PSx controller communication function. // send a byte on the command line and receive one on the data line. // needs Attention pin to have gone low before called to activate controller. void gameByte (char command) { int i ; wait(0.000001); short int data = 0x00; // clear data variable to save setting low bits later. for (i=0;i<8;i++) { if (command & _BV(i)) { PScommand=1; // bit bang "command" out on PScommand wire. } else { PScommand = 0; } PSclock=0; // CLOCK LOW delayMicroseconds(20); // wait for output to stabilise if ((PIND & _BV(PSdata)))_SFR_BYTE(data) |= _BV(i); // read PSdata pin and store //else cbi(data, i); PSclock=1; // CLOCK HIGH } PScommand = 1; wait(0.00002); ; // wait for ACK to pass. return(data); }PlayStationController
The problems I am having is with the last function called gameByte, there are a few variable that I have no idea what they would correspond to in mbed language and I was wondering if anyone has any idea of what to do to get it to compile.
Thanks so much, more information about the project is here: Second Life Rock Band