|
| 1 | +#[derive(Clone)] |
| 2 | +enum JoystickType { |
| 3 | + Unknown = -1, |
| 4 | +} |
| 5 | + |
| 6 | +#[derive(Clone)] |
| 7 | +struct Joystick { |
| 8 | + stick_type: JoystickType, |
| 9 | + buttons: Vec<bool>, |
| 10 | + axes: Vec<i8>, |
| 11 | + povs: Vec<i16>, |
| 12 | +} |
| 13 | + |
| 14 | +impl Joystick { |
| 15 | + fn new(num_buttons: u8, num_axes: u8, num_povs: u8) -> Self { |
| 16 | + Joystick { |
| 17 | + stick_type: JoystickType::Unknown, |
| 18 | + buttons: vec![false; num_buttons as usize], |
| 19 | + axes: vec![0; num_axes as usize], |
| 20 | + povs: vec![-1; num_povs as usize], |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + fn num_buttons(&self) -> u8 { |
| 25 | + self.buttons.len() as u8 |
| 26 | + } |
| 27 | + |
| 28 | + fn num_axes(&self) -> u8 { |
| 29 | + self.axes.len() as u8 |
| 30 | + } |
| 31 | + |
| 32 | + fn num_povs(&self) -> u8 { |
| 33 | + self.povs.len() as u8 |
| 34 | + } |
| 35 | + |
| 36 | + fn set_button(&mut self, index: u8, pressed: bool) -> Result<(), ()> { |
| 37 | + if self.buttons.len() as u8 >= index { |
| 38 | + Err(()) |
| 39 | + } else { |
| 40 | + self.buttons[index as usize] = pressed; |
| 41 | + Ok(()) |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + fn set_axis(&mut self, index: u8, value: i8) -> Result<(), ()> { |
| 46 | + if self.axes.len() as u8 >= index { |
| 47 | + Err(()) |
| 48 | + } else { |
| 49 | + self.axes[index as usize] = value; |
| 50 | + Ok(()) |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + fn set_pov(&mut self, index: u8, value: i16) -> Result<(), ()> { |
| 55 | + if self.povs.len() as u8 >= index { |
| 56 | + Err(()) |
| 57 | + } else { |
| 58 | + self.povs[index as usize] = value; |
| 59 | + Ok(()) |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + fn to_tag(&self) -> Vec<u8> { |
| 64 | + let mut tag: Vec<u8> = vec![0; 4]; |
| 65 | + |
| 66 | + tag.push(self.axes.len() as u8); |
| 67 | + for axis in &self.axes { |
| 68 | + tag.push(*axis as u8); // this might work |
| 69 | + } |
| 70 | + |
| 71 | + tag.push(self.buttons.len() as u8); |
| 72 | + let mut index = 7; |
| 73 | + let mut byte: u8 = 0; |
| 74 | + for button in &self.buttons { |
| 75 | + if *button { |
| 76 | + byte |= 1 << index; |
| 77 | + } |
| 78 | + index -= 1; |
| 79 | + if index < 0 { |
| 80 | + tag.push(byte); |
| 81 | + byte = 0; |
| 82 | + index = 7; |
| 83 | + } |
| 84 | + } |
| 85 | + if index != 7 { |
| 86 | + tag.push(byte); |
| 87 | + } |
| 88 | + |
| 89 | + tag.push(self.povs.len() as u8); |
| 90 | + for pov in &self.povs { |
| 91 | + tag.push(((pov >> 8) & 0xff as i16) as u8); |
| 92 | + tag.push((pov & 0xff) as u8); |
| 93 | + } |
| 94 | + |
| 95 | + tag |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +enum RobotMode { |
| 100 | + Teleop = 0, |
| 101 | + Test = 1, |
| 102 | + Auto = 2, |
| 103 | +} |
| 104 | + |
| 105 | +enum Alliance { |
| 106 | + Red(u8), |
| 107 | + Blue(u8), |
| 108 | +} |
| 109 | + |
| 110 | +impl Alliance { |
| 111 | + // TODO check bounds on both methods |
| 112 | + |
| 113 | + fn to_position_u8(&self) -> u8 { |
| 114 | + match self { |
| 115 | + Alliance::Red(pos) => pos - 1, |
| 116 | + Alliance::Blue(pos) => pos + 2, |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + fn from_position_u8(pos: u8) -> Self { |
| 121 | + if pos < 3 { |
| 122 | + Alliance::Red(pos + 1) |
| 123 | + } else { |
| 124 | + Alliance::Blue(pos % 3 + 1) |
| 125 | + } |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +enum MatchType { |
| 130 | + None = 0, |
| 131 | + Practice = 1, |
| 132 | + Qualification = 2, |
| 133 | + Elimination = 3, |
| 134 | +} |
| 135 | + |
| 136 | +struct DriverStation { |
| 137 | + joysticks: Vec<Option<Joystick>>, |
| 138 | + estop: bool, |
| 139 | + enabled: bool, |
| 140 | + mode: RobotMode, |
| 141 | + alliance: Alliance, |
| 142 | + game_data: String, |
| 143 | + competition: String, |
| 144 | +} |
| 145 | + |
| 146 | +impl DriverStation { |
| 147 | + fn new() -> Self { |
| 148 | + DriverStation { |
| 149 | + joysticks: vec![None; 6], |
| 150 | + estop: false, |
| 151 | + enabled: false, |
| 152 | + mode: RobotMode::Teleop, |
| 153 | + alliance: Alliance::Red(1), |
| 154 | + game_data: String::from("rrr"), |
| 155 | + competition: String::from("unknown"), |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments