|
| 1 | +/** \file Pointing.cpp |
| 2 | + * \brief Implementation of the pointing devices of the CD-I. |
| 3 | + */ |
| 4 | + |
| 5 | +#include "Pointing.hpp" |
| 6 | +#include "common/utils.hpp" |
| 7 | + |
| 8 | +#include <algorithm> |
| 9 | +#include <print> |
| 10 | +#include <utility> |
| 11 | + |
| 12 | +namespace Pointing |
| 13 | +{ |
| 14 | + |
| 15 | +// size_t RelativeCoordinate::GetState(const std::span<uint8_t> packet) noexcept |
| 16 | +// { |
| 17 | +// std::unique_lock<std::mutex> lock{m_stateMutex}; |
| 18 | +// |
| 19 | +// const uint8_t button1 = static_cast<uint8_t>(m_lastState.btn1) << 5; |
| 20 | +// const uint8_t button2 = static_cast<uint8_t>(m_lastState.btn2) << 4; |
| 21 | +// |
| 22 | +// const uint8_t x = m_lastState.left ? -SPEED : (m_lastState.right ? SPEED : 0); |
| 23 | +// const uint8_t y = m_lastState.up ? -SPEED : (m_lastState.down ? SPEED : 0); |
| 24 | +// |
| 25 | +// std::println("{} {} {} {} {}", SPEED, button1, button2, x, y); |
| 26 | +// |
| 27 | +// packet[0] = 0x40 | button1 | button2 | (y >> 4 & 0x0C) | (x >> 6 & 3); |
| 28 | +// packet[1] = x & 0x3F; |
| 29 | +// packet[2] = y & 0x3F; |
| 30 | +// |
| 31 | +// return BYTES_PER_PACKET; |
| 32 | +// } |
| 33 | + |
| 34 | +// bool RelativeCoordinate::IncrementTime(const double ns) noexcept |
| 35 | +// { |
| 36 | +// // TODO: coroutine? |
| 37 | +// bool sendPacket = false; |
| 38 | +// std::lock_guard<std::mutex> lock{m_stateMutex}; |
| 39 | +// |
| 40 | +// if(!m_timeNs.has_value()) // Packet can be sent immediately. |
| 41 | +// { |
| 42 | +// sendPacket = m_state.HasChanged(m_lastState); |
| 43 | +// if(sendPacket) |
| 44 | +// m_timeNs = 0.0; // Start counting time. |
| 45 | +// } |
| 46 | +// else // m_timeNs has a value, waiting for packet delay. |
| 47 | +// { |
| 48 | +// *m_timeNs += ns; |
| 49 | +// |
| 50 | +// if(*m_timeNs >= PACKET_DELTA) |
| 51 | +// { |
| 52 | +// sendPacket = m_state.HasChanged(m_lastState); |
| 53 | +// if(sendPacket) |
| 54 | +// *m_timeNs -= PACKET_DELTA; // Consecutive packet, start counting again. |
| 55 | +// else |
| 56 | +// m_timeNs = std::nullopt; |
| 57 | +// } |
| 58 | +// } |
| 59 | +// |
| 60 | +// if(sendPacket) |
| 61 | +// { |
| 62 | +// m_lastState = m_state; // To be retrieved later by the Slave chip or driver. |
| 63 | +// m_state.ClearDirections(); |
| 64 | +// } |
| 65 | +// |
| 66 | +// return sendPacket; |
| 67 | +// } |
| 68 | + |
| 69 | +size_t Maneuvering::GetState(const std::span<uint8_t> packet) noexcept |
| 70 | +{ |
| 71 | + const int speed = GetMovementSpeed(); |
| 72 | + |
| 73 | + const uint8_t button1 = static_cast<uint8_t>(m_lastState.btn1); |
| 74 | + const uint8_t button2 = static_cast<uint8_t>(m_lastState.btn2); |
| 75 | + |
| 76 | + const uint8_t x = m_lastState.left ? -speed : (m_lastState.right ? speed : 0); |
| 77 | + const uint8_t y = m_lastState.up ? -speed : (m_lastState.down ? speed : 0); |
| 78 | + |
| 79 | + packet[0] = 0x40 | (button1 << 5) | (button2 << 4) | (y >> 4 & 0x0C) | (x >> 6 & 3); |
| 80 | + packet[1] = x & 0x3F; |
| 81 | + packet[2] = y & 0x3F; |
| 82 | + |
| 83 | + return Maneuvering::BYTES_PER_PACKET; |
| 84 | +} |
| 85 | + |
| 86 | +bool Maneuvering::IncrementTime(const double ns) noexcept |
| 87 | +{ |
| 88 | + // TODO: coroutine? |
| 89 | + bool sendPacket = false; |
| 90 | + std::lock_guard<std::mutex> lock{m_stateMutex}; |
| 91 | + |
| 92 | + if(!m_timeNs.has_value()) // Packet can be sent immediately. |
| 93 | + { |
| 94 | + sendPacket = m_state.HasAnyButtonChanged(m_lastState) || m_state.HasPadPressed(); |
| 95 | + if(sendPacket) |
| 96 | + { |
| 97 | + m_timeNs = 0.0; // Start counting time. |
| 98 | + m_consecutivePackets = 1; |
| 99 | + } |
| 100 | + } |
| 101 | + else // m_timeNs has a value, waiting for packet delay. |
| 102 | + { |
| 103 | + *m_timeNs += ns; |
| 104 | + |
| 105 | + if(*m_timeNs >= PACKET_DELTA) |
| 106 | + { |
| 107 | + sendPacket = m_state.HasAnyButtonChanged(m_lastState) || m_state.HasPadPressed(); |
| 108 | + if(sendPacket) // Consecutive packet, start counting again. |
| 109 | + { |
| 110 | + *m_timeNs -= PACKET_DELTA; |
| 111 | + if(m_state.HasPadPressed()) |
| 112 | + IncrementConsecutivePacket(); |
| 113 | + else |
| 114 | + m_consecutivePackets = 0; |
| 115 | + } |
| 116 | + else // No consecutive packet. |
| 117 | + { |
| 118 | + m_timeNs = std::nullopt; |
| 119 | + m_consecutivePackets = 0; |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + if(sendPacket) |
| 125 | + m_lastState = m_state; // To be retrieved later by the Slave chip or driver. |
| 126 | + |
| 127 | + return sendPacket; |
| 128 | +} |
| 129 | + |
| 130 | +int Maneuvering::GetMovementSpeed() const noexcept |
| 131 | +{ |
| 132 | + switch(m_speed) |
| 133 | + { |
| 134 | + case GamepadSpeed::N: return std::min(8, m_consecutivePackets + (m_consecutivePackets & 1)); |
| 135 | + case GamepadSpeed::I: return 1; |
| 136 | + case GamepadSpeed::II: return std::min(16, 2 * m_consecutivePackets); |
| 137 | + } |
| 138 | + std::unreachable(); |
| 139 | +} |
| 140 | + |
| 141 | +} // namespace Pointing |
0 commit comments