diff options
author | Gentoo <installgentoo@endianness.com> | 2020-10-14 17:05:45 +1100 |
---|---|---|
committer | Gentoo <installgentoo@endianness.com> | 2020-10-14 17:05:45 +1100 |
commit | 093da86246cda0aca6b395c42b5bd20633cc2190 (patch) | |
tree | df54b9ae961ec860b76a41718d0b69b09546fada | |
download | wiimote-093da86246cda0aca6b395c42b5bd20633cc2190.tar.gz wiimote-093da86246cda0aca6b395c42b5bd20633cc2190.tar.bz2 wiimote-093da86246cda0aca6b395c42b5bd20633cc2190.zip |
inital commit
-rw-r--r-- | Makefile | 15 | ||||
-rw-r--r-- | buttons.h | 43 | ||||
-rw-r--r-- | main.c | 158 |
3 files changed, 216 insertions, 0 deletions
diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..5b8f2a0 --- /dev/null +++ b/Makefile @@ -0,0 +1,15 @@ +CFLAGS=-O3 -march=native -pipe +LIBS=-lbluetooth + +OBJ = main.o +HEADERS = buttons.h + +%.o: %.c $(DEPS) $(HEADERS) + gcc -c -o $@ $< $(CFLAGS) $(LIBS) + +wiimote: $(OBJ) + gcc -o $@ $^ $(CFLAGS) $(LIBS) + +clean: + rm wiimote + rm main.o diff --git a/buttons.h b/buttons.h new file mode 100644 index 0000000..d7d85a9 --- /dev/null +++ b/buttons.h @@ -0,0 +1,43 @@ +#ifndef BUTTONS_H +#define BUTTONS_H + +/* +The Wii Remote has 11 buttons that are used as regular input devices: A, B (trigger), a 4-directional D-Pad, +, -, Home, 1, and 2. +These are reported as bits in a two-byte bitmask. These are the assignments, in big-endian order: + +Bit Mask First Byte Second Byte +0 0x01 D-Pad Left Two +1 0x02 D-Pad Right One +2 0x04 D-Pad Down B +3 0x08 D-Pad Up A +4 0x10 Plus Minus +5 0x20 Other uses Other uses +6 0x40 Other uses Other uses +7 0x80 Unknown Home +*/ + +//first byte +#define DPAD_LEFT 0x01 +#define DPAD_RIGHT 0x02 +#define DPAD_DOWN 0x04 +#define DPAD_UP 0x08 +#define PLUS 0x10 +#define OTHER0 0x20 +#define OTHER1 0x40 +#define UNKNOWN 0x80 + +//second byte +#define TWO 0x01 +#define ONE 0x02 +#define B 0x04 +#define A 0x08 +#define MINUS 0x10 +#define OTHER2 0x20 +#define OTHER3 0x40 +#define HOME 0x80 + + + + + +#endif @@ -0,0 +1,158 @@ +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <string.h> +#include <stdbool.h> + +#include <sys/socket.h> +#include <bluetooth/bluetooth.h> +#include <bluetooth/l2cap.h> + +#include "buttons.h" + +int controlSock, dataSock; + +#define MAX 40 + +struct state + { + bool a; + bool b; + bool plus; + bool minus; + bool home; + bool one; + bool two; + bool dpadL; + bool dpadR; + bool dpadU; + bool dpadD; + + bool rumble; + }; + +struct state newState; + + +void connectBluetooth(char server[]) + { + int status; + + //Establishing a HID connection with the Wii Remote can be done on PSM 0x11 for the control pipe using the Bluetooth L2CAP protocol. + struct sockaddr_l2 control = {0}; + controlSock = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP); + + // set the connection parameters (who to connect to) + control.l2_family = AF_BLUETOOTH; + control.l2_psm = htobs(0x11); + str2ba(server,&control.l2_bdaddr); + + status = connect(controlSock, (struct sockaddr *)&control, sizeof(control));//while the control pipe isn't used, we still gotta open it + if(status < 0){fprintf(stderr,"Failed to connect to control pipe!: "); perror(""); exit(1);} + + //Establishing a HID connection with the Wii Remote can be done on PSM 0x13 for the data pipe using the Bluetooth L2CAP protocol. + struct sockaddr_l2 data = {0}; + dataSock = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP); + + // set the connection parameters (who to connect to) + data.l2_family = AF_BLUETOOTH; + data.l2_psm = htobs(0x13); + str2ba(server,&data.l2_bdaddr); + + status = connect(dataSock, (struct sockaddr *)&data, sizeof(data)); + if(status < 0){fprintf(stderr,"Failed to connect to data pipe!: "); perror(""); exit(1);} + } + +void sdp() + { + //todo + } + +void initButtons(struct state *buttons) + { + buttons->a = false; + buttons->b = false; + buttons->plus = false; + buttons->minus = false; + buttons->one = false; + buttons->two = false; + buttons->dpadL = false; + buttons->dpadR = false; + buttons->dpadU = false; + buttons->dpadD = false; + + buttons->rumble = false;//not a button but... + } + +int main(int argc, char **argv) +{ + connectBluetooth("44:23:24:65:23:52");//todo: working function, syncing and SDP + + //When using a Wii Remote, all input reports are prepended with 0xa1 and all output reports are prepended with 0xa2 + //Output reports are sent over the data pipe, which is also used to read input reports + + //input reports are reports from the Wii remote and output reports are reports to the Wii remote + + + struct state *buttons; + buttons = &newState; + initButtons(buttons); + + buttons->a = true;//lets assume that the A button is held down + + + //first up is the assumption that the Wii sends the status reporting mode + char buff[MAX+1]; + int size = read(dataSock, buff, MAX); + + switch (size) + { + case 3: + //(a2) 15 00 + //This will request the status report (and turn off rumble) + if (buff[0] == 0xa2 && buff[1] == 0x15 && buff[2] == 0) + { + buttons->rumble = false; + //reply: (a1) 20 BB BB LF 00 00 VV + buff[0] = 0xa1; + buff[1] = 0x20; + buff[2] = ((buttons->dpadL) ? DPAD_LEFT:0) | ((buttons->dpadL) ? DPAD_RIGHT:0) | ((buttons->dpadD) ? DPAD_DOWN : 0) | ((buttons->dpadU) ? DPAD_UP : 0) | + ((buttons->plus) ? PLUS : 0) | (OTHER0 & 0) | (OTHER1 & 0) | (UNKNOWN & 0); + buff[3] = ((buttons->two) ? TWO : 0) | ((buttons->one) ? ONE : 0) | ((buttons->b) ? B : 0) | ((buttons->b) ? A : 0) | ((buttons->minus) ? MINUS : 0) + | (OTHER2 & 0) | (OTHER3 & 0) | ((buttons->home) ? HOME : 0); + buff[4] = 0x10;//just report that LED 1 is lit for LF + buff[5] = 0; + buff[6] = 0; + buff[7] = 100;//Set the battery level to 100 + + int result = write(dataSock, buff,8); + if (result < 8){fprintf(stderr,"Write failed or didn't write enough"); exit(1);} + } + else + { + fprintf(stderr,"Unexpected: %hi%hi%hi",buff[0],buff[1],buff[2]); + } + break; + + + default: + fprintf(stderr,"Unimplemented report\n"); + break; + } + + + + + + + + /* + if (status == 0) + { + status = write(s, "Install Gentoo!\n", 16); + } + */ + +// close(controlSock); +// close(dataSock); +} |