#include "gpio2nesc.h" #include "defines.h" #include #include #include #include "wiimote.h" #include // classic controller id const unsigned char classic_controller_id[6] = {0x00, 0x00, 0xA4, 0x20, 0x01, 0x01}; volatile int jx = 0, jy = 0, rx = 0, ry = 0, tl = 0, tr = 0; volatile uint8_t left = 0, right = 0, up = 0, down = 0, a = 0, b = 0, x = 0, y = 0, select = 0, start = 0, home = 0, l = 0, r = 0, zl = 0, zr = 0; // calibration data const unsigned char cal_data[32] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; void wiimote_query() { wdt_reset(); RED_ON; unsigned char but_dat[8]; // struct containing button data if (twi_reg[0xFE] == 1) // data format { but_dat[0] = 0b00000000; // RX<4:3> LX<5:0> but_dat[1] = 0b00000000; // RX<2:1> LY<5:0> but_dat[2] = 0b00000000; // RX<0> LT<4:3> RY<4:0> but_dat[3] = 0b00000000; // LT<2:0> RT<4:0> but_dat[4] = 0b11111111; // BDR BDD BLT B- BH B+ BRT 1 but_dat[5] = 0b11111111; // BZL BB BY BA BX BZR BDL BDU but_dat[6] = 0; but_dat[7] = 0; but_dat[0] |= (jx / 4 + 0x20) & 0x3F; but_dat[1] |= (jy / -4 + 0x20) & 0x3F; but_dat[0] |= ((rx / 8 + 0x10) & 0x18) << 3; but_dat[1] |= ((rx / 8 + 0x10) & 0x06) << 5; but_dat[2] |= ((rx / 8 + 0x10) & 0x01) << 7; but_dat[2] |= (ry / -8 + 0x10) & 0x1F; but_dat[2] |= ((tl / 8) & 0x18) << 2; but_dat[3] |= ((tl / 8) & 0x07) << 5; but_dat[3] |= (tr / 8) & 0x1F; if (right) but_dat[4] &= ~(1<<7); if (down) but_dat[4] &= ~(1<<6); if (l) but_dat[4] &= ~(1<<5); if (select) but_dat[4] &= ~(1<<4); if (home) but_dat[4] &= ~(1<<3); if (start) but_dat[4] &= ~(1<<2); if (r) but_dat[4] &= ~(1<<1); if (zl) but_dat[5] &= ~(1<<7); if (b) but_dat[5] &= ~(1<<6); if (y) but_dat[5] &= ~(1<<5); if (a) but_dat[5] &= ~(1<<4); if (x) but_dat[5] &= ~(1<<3); if (zr) but_dat[5] &= ~(1<<2); if (left) but_dat[5] &= ~(1<<1); if (up) but_dat[5] &= ~(1<<0); but_dat[6] = 0; but_dat[7] = 0; if (((but_dat[4] & 0xFE) == 0xFE) && (but_dat[5] == 0xFF)) GREEN_OFF; else GREEN_ON; wm_newaction(but_dat); } else if (twi_reg[0xFE] == 3) // data format { but_dat[0] = jx + 0x80; but_dat[1] = rx + 0x80; but_dat[2] = 0x7fl - jy; but_dat[3] = 0x7fl - ry; but_dat[4] = tl; but_dat[5] = tr; but_dat[6] = 0b11111111; // BDR BDD BLT B- BH B+ BRT 1 but_dat[7] = 0b11111111; // BZL BB BY BA BX BZR BDL BDU if (right) but_dat[6] &= ~(1<<7); if (down) but_dat[6] &= ~(1<<6); if (l) but_dat[6] &= ~(1<<5); if (select) but_dat[6] &= ~(1<<4); if (home) but_dat[6] &= ~(1<<3); if (start) but_dat[6] &= ~(1<<2); if (r) but_dat[6] &= ~(1<<1); if (zl) but_dat[7] &= ~(1<<7); if (b) but_dat[7] &= ~(1<<6); if (y) but_dat[7] &= ~(1<<5); if (a) but_dat[7] &= ~(1<<4); if (x) but_dat[7] &= ~(1<<3); if (zr) but_dat[7] &= ~(1<<2); if (left) but_dat[7] &= ~(1<<1); if (up) but_dat[7] &= ~(1<<0); if (((but_dat[6] & 0xFE) == 0xFE) && (but_dat[7] == 0xFF)) GREEN_OFF; else GREEN_ON; wm_newaction(but_dat); } } int main() { DDR(RED_LED_PORT) |= (1<