|
| 1 | +// Copyright 2024 Khalil Estell |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include <libhal-input/gamepad/nunchuck.hpp> |
| 16 | +#include <libhal-util/i2c.hpp> |
| 17 | + |
| 18 | +namespace hal::input { |
| 19 | +nunchuck::nunchuck(hal::i2c& p_i2c) |
| 20 | + : m_i2c(&p_i2c) |
| 21 | +{ |
| 22 | + // handshake byte (0x40) is needed to initialize |
| 23 | + hal::write( |
| 24 | + *m_i2c, 0x52, std::array<hal::byte, 1>{ 0x40 }, hal::never_timeout()); |
| 25 | +} |
| 26 | + |
| 27 | +nunchuck::gamepad_data nunchuck::read() |
| 28 | +{ |
| 29 | + gamepad_data controller{}; |
| 30 | + hal::write_then_read(*m_i2c, |
| 31 | + 0x52, |
| 32 | + std::array<hal::byte, 1>{ 0x00 }, |
| 33 | + controller.data, |
| 34 | + hal::never_timeout()); |
| 35 | + return controller; |
| 36 | +} |
| 37 | + |
| 38 | +std::uint8_t nunchuck::gamepad_data::joystick_x() |
| 39 | +{ |
| 40 | + return data[0]; |
| 41 | +} |
| 42 | + |
| 43 | +std::uint8_t nunchuck::gamepad_data::joystick_y() |
| 44 | +{ |
| 45 | + return data[1]; |
| 46 | +} |
| 47 | + |
| 48 | +std::uint16_t nunchuck::gamepad_data::accelerometer_x() |
| 49 | +{ |
| 50 | + // Accelerometer data is 10-bits split up with upper bits having a whole byte |
| 51 | + // to themselves and the lower 2 bits are contained in data[5]. |
| 52 | + |
| 53 | + // x-axis upper bits are in data[2] |
| 54 | + // Put the upper bits into a 16-bit int and left shift over 2 to make room for |
| 55 | + // lower bits. |
| 56 | + std::uint16_t x_upper_bits = data[2] << 2; |
| 57 | + // x-axis lower bits are bit 2 and 3 of data[5]. |
| 58 | + // Used a bit mask with & operator to get only these 2 bits, then shift them |
| 59 | + // all the way to the right to be combined with upper bits. |
| 60 | + std::uint16_t x_lower_bits = (data[5] & 0b0000000000001100) >> 2; |
| 61 | + // Combine upper and lower bits by using an | operator |
| 62 | + return x_upper_bits | x_lower_bits; |
| 63 | +} |
| 64 | + |
| 65 | +std::uint16_t nunchuck::gamepad_data::accelerometer_y() |
| 66 | +{ |
| 67 | + // y-axis upper bits are in data[3] |
| 68 | + std::uint16_t y_upper_bits = data[3] << 2; |
| 69 | + // y-axis lower bits are bit 4 and 5 of data[5]. |
| 70 | + std::uint16_t y_lower_bits = (data[5] & 0b0000000000110000) >> 4; |
| 71 | + return y_upper_bits | y_lower_bits; |
| 72 | +} |
| 73 | + |
| 74 | +std::uint16_t nunchuck::gamepad_data::accelerometer_z() |
| 75 | +{ |
| 76 | + // z-axis upper bits are in data[4] |
| 77 | + std::uint16_t z_upper_bits = data[4] << 2; |
| 78 | + // z-axis lower bits are bit 6 and 7 of data[5]. |
| 79 | + std::uint16_t z_lower_bits = (data[5] & 0b0000000011000000) >> 6; |
| 80 | + return z_upper_bits | z_lower_bits; |
| 81 | +} |
| 82 | + |
| 83 | +bool nunchuck::gamepad_data::z_button() |
| 84 | +{ |
| 85 | + // z button state is bit 0 of data[5]. |
| 86 | + // Originally returns 0 when pressed, inverted to return true when pressed. |
| 87 | + return !(data[5] & 0b00000001); |
| 88 | +} |
| 89 | + |
| 90 | +bool nunchuck::gamepad_data::c_button() |
| 91 | +{ |
| 92 | + // c button state is bit 1 of data[5]. |
| 93 | + return !(data[5] & 0b0000010); |
| 94 | +} |
| 95 | +} // namespace hal::input |
0 commit comments