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@0:ebc7a8e353d1, 2021-05-23 (annotated)
- Committer:
- quagga
- Date:
- Sun May 23 15:09:33 2021 +0000
- Revision:
- 0:ebc7a8e353d1
Emulate Gravis Stinger: pitch and roll and buttons A and B to serial data packet
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| quagga | 0:ebc7a8e353d1 | 1 | #include"MicroBit.h" |
| quagga | 0:ebc7a8e353d1 | 2 | |
| quagga | 0:ebc7a8e353d1 | 3 | MicroBit uBit; |
| quagga | 0:ebc7a8e353d1 | 4 | |
| quagga | 0:ebc7a8e353d1 | 5 | int main() |
| quagga | 0:ebc7a8e353d1 | 6 | { |
| quagga | 0:ebc7a8e353d1 | 7 | uBit.init(); |
| quagga | 0:ebc7a8e353d1 | 8 | //listen for stinger signal E5E5 |
| quagga | 0:ebc7a8e353d1 | 9 | ManagedString sting=" E5E5"; |
| quagga | 0:ebc7a8e353d1 | 10 | ManagedString readbuf=uBit.serial.read(5); |
| quagga | 0:ebc7a8e353d1 | 11 | if (readbuf==sting) |
| quagga | 0:ebc7a8e353d1 | 12 | { |
| quagga | 0:ebc7a8e353d1 | 13 | //Identify to inputattach driver |
| quagga | 0:ebc7a8e353d1 | 14 | uBit.serial.send("\r\n0600520058C272"); |
| quagga | 0:ebc7a8e353d1 | 15 | uBit.display.scroll("Stinger"); |
| quagga | 0:ebc7a8e353d1 | 16 | int x,y; |
| quagga | 0:ebc7a8e353d1 | 17 | unsigned char data[4]; |
| quagga | 0:ebc7a8e353d1 | 18 | while(1) |
| quagga | 0:ebc7a8e353d1 | 19 | { |
| quagga | 0:ebc7a8e353d1 | 20 | //packets [0 && 3] deal with buttons |
| quagga | 0:ebc7a8e353d1 | 21 | //packet [0,1,2] deal with joystick |
| quagga | 0:ebc7a8e353d1 | 22 | x=((uBit.accelerometer.getPitch()+180)*126/360); |
| quagga | 0:ebc7a8e353d1 | 23 | y=((uBit.accelerometer.getRoll()+180)*126/360); |
| quagga | 0:ebc7a8e353d1 | 24 | data[1]=0x3f-(x & 0x3f); |
| quagga | 0:ebc7a8e353d1 | 25 | data[2]=y & 0x3f; |
| quagga | 0:ebc7a8e353d1 | 26 | data[0]=((y & 0x40)>>5)+((x & 0x40)>>6); |
| quagga | 0:ebc7a8e353d1 | 27 | |
| quagga | 0:ebc7a8e353d1 | 28 | if (uBit.buttonA.isPressed()){data[0]+=0x20;} |
| quagga | 0:ebc7a8e353d1 | 29 | if (uBit.buttonB.isPressed()){data[0]+=0x10;} |
| quagga | 0:ebc7a8e353d1 | 30 | //send packet |
| quagga | 0:ebc7a8e353d1 | 31 | for (int i=0;i<4;i++){uBit.serial.sendChar(data[i]);} |
| quagga | 0:ebc7a8e353d1 | 32 | } |
| quagga | 0:ebc7a8e353d1 | 33 | } |
| quagga | 0:ebc7a8e353d1 | 34 | return 0; |
| quagga | 0:ebc7a8e353d1 | 35 | } |