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.
Fork of Smoothie by
Wiichuck.h
00001 /* 00002 This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl). 00003 Smoothie is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. 00004 Smoothie is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. 00005 You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>. 00006 */ 00007 #ifndef WIICHUCK_H 00008 #define WIICHUCK_H 00009 #include "mbed.h" // mbed.h lib 00010 00011 using namespace std; 00012 #include <vector> 00013 #include <string> 00014 #include <cstdio> 00015 #include <cstdarg> 00016 00017 #define DEVICE_NUNCHUCK 0x0000 00018 #define DEVICE_CLASSIC 0x0101 00019 #define DEVICE_INACT_WMP 0x0005 00020 #define DEVICE_WMP 0x0405 00021 #define DEVICE_WMP_NUNCHUCK 0x0505 00022 #define DEVICE_WMP_CLASSIC 0x0705 00023 00024 #define WIICHUCK 0xA4 00025 00026 class Wiichuck { 00027 public: 00028 Wiichuck(PinName sda, PinName scl, int frequency = 10000){ 00029 this->i2c_mine = true; 00030 00031 this->i2c = new mbed::I2C(sda, scl); 00032 this->i2c->frequency(frequency); 00033 } 00034 00035 Wiichuck(I2C* i2c){ 00036 this->i2c_mine = false; 00037 00038 this->i2c = i2c; 00039 } 00040 00041 ~Wiichuck(){ 00042 if(this->i2c_mine) 00043 delete this->i2c; 00044 } 00045 00046 void init_device(){ 00047 char cmd[2]; 00048 data_ready = false; 00049 00050 cmd[0] = 0xF0; // first two writes are a magic init sequence 00051 cmd[1] = 0x55; 00052 this->i2c->write(WIICHUCK, cmd, 2); 00053 wait_ms(10); 00054 cmd[0] = 0xFB; 00055 cmd[1] = 0x00; 00056 this->i2c->write(WIICHUCK, cmd, 2); 00057 wait_ms(10); 00058 cmd[0] = 0xFA; // read out the device type 00059 this->i2c->write(WIICHUCK, cmd, 1, false); 00060 int res = this->i2c->read(WIICHUCK, data_buf, 6); 00061 cmd[0] = 0x00; // request first sensor readings 00062 this->i2c->write(WIICHUCK, cmd, 1); 00063 if(res == 0 && data_buf[2] == 0xA4){ 00064 device_type = (data_buf[4] << 8) + data_buf[5]; 00065 }else{ 00066 device_type = -1; 00067 } 00068 } 00069 00070 void poll_device() { 00071 data_ready = false; 00072 if(device_type < 0) { 00073 init_device(); 00074 wait_ms(100); 00075 } 00076 // if there is a connected device read it and if it responds right parse the data 00077 if(device_type >= 0 && read_device() == 0) { 00078 switch(device_type) { 00079 case DEVICE_NUNCHUCK: parse_nunchuck(); break; 00080 case DEVICE_CLASSIC: parse_classic(); break; 00081 case DEVICE_INACT_WMP: init_wmp(); break; 00082 case DEVICE_WMP: parse_wmp(); break; 00083 default: 00084 break; 00085 } 00086 } 00087 } 00088 00089 int read_device() { 00090 char cmd = 0x00; 00091 int res = this->i2c->read(WIICHUCK, data_buf, 6); // read sensors 00092 this->i2c->write(WIICHUCK, &cmd, 1); // request next sensor readings 00093 return res; 00094 } 00095 00096 void parse_nunchuck() { 00097 SX = data_buf[0]; 00098 SY = data_buf[1]; 00099 AX = (data_buf[2] << 2) + ((data_buf[5] >> 2) & 0x03); 00100 AY = (data_buf[3] << 2) + ((data_buf[5] >> 4) & 0x03); 00101 AZ = (data_buf[5] << 2) + ((data_buf[5] >> 6) & 0x03); 00102 BC = (data_buf[5] >> 1) & 0x01; 00103 BZ = data_buf[5] & 0x01; 00104 data_ready = true; 00105 } 00106 00107 void parse_classic() { 00108 LX = data_buf[0] << 2; 00109 LY = data_buf[1] << 2; 00110 RX = (data_buf[0] & 0xC0) + ((data_buf[1] & 0xC0) >> 2) + ((data_buf[2] & 0x80) >> 4); 00111 RY = data_buf[2] << 3; 00112 LT = ((data_buf[2] & 0x60) << 1) + ((data_buf[3] & 0xE0) >> 2); 00113 RT = data_buf[3] << 3; 00114 BDU = data_buf[5] & 0x01; 00115 BDD = (data_buf[4] >> 6) & 0x01; 00116 BDL = (data_buf[5] >> 1) & 0x01; 00117 BDR = (data_buf[4] >> 7) & 0x01; 00118 BLT = (data_buf[4] >> 5) & 0x01; 00119 BRT = (data_buf[4] >> 1) & 0x01; 00120 BH = (data_buf[4] >> 3) & 0x01; 00121 BP = (data_buf[4] >> 2) & 0x01; 00122 BM = (data_buf[4] >> 4) & 0x01; 00123 BA = (data_buf[5] >> 4) & 0x01; 00124 BB = (data_buf[5] >> 6) & 0x01; 00125 BX = (data_buf[5] >> 3) & 0x01; 00126 BY = (data_buf[5] >> 5) & 0x01; 00127 BZL = (data_buf[5] >> 7) & 0x01; 00128 BZR = (data_buf[5] >> 2) & 0x01; 00129 data_ready = true; 00130 } 00131 00132 void init_wmp() { 00133 } 00134 00135 void activate_wmp() { 00136 } 00137 00138 void deactivate_wmp() { 00139 } 00140 00141 void parse_wmp() { 00142 } 00143 00144 char* get_raw() { 00145 return data_buf; 00146 } 00147 00148 // nunchuck input state variables 00149 char SX,SY; // 8-bit joystick 00150 short AX,AY,AZ; // 10-bit accelerometer 00151 bool BC,BZ; // buttons 00152 00153 // classic input state variables 00154 char LX,LY,RX,RY,LT,RT; // 6-bit left joystick, 5-bit right joystick and triggers 00155 bool BDU,BDD,BDL,BDR; // d-pad buttons 00156 bool BLT,BRT; // digital click of triggers 00157 bool BH,BP,BM; // home, plus, minus buttons 00158 bool BA,BB,BX,BY,BZL,BZR; // buttons 00159 00160 bool i2c_mine; 00161 bool data_ready = false; 00162 char data_buf[6]; 00163 int device_type = -1; 00164 mbed::I2C* i2c; 00165 }; 00166 00167 00168 #endif // WIICHUCK_H 00169
Generated on Tue Jul 12 2022 20:09:03 by
1.7.2
