Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 1 | #include "frc971/wpilib/buffered_pcm.h" |
| 2 | |
| 3 | #include <inttypes.h> |
| 4 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 5 | #include "aos/logging/logging.h" |
Austin Schuh | f6b9463 | 2019-02-02 22:11:27 -0800 | [diff] [blame] | 6 | #include "hal/HAL.h" |
| 7 | #include "hal/Ports.h" |
| 8 | #include "hal/Solenoid.h" |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 9 | |
| 10 | namespace frc971 { |
| 11 | namespace wpilib { |
| 12 | |
Austin Schuh | 547cf8a | 2017-11-23 13:21:16 -0800 | [diff] [blame] | 13 | BufferedPcm::BufferedPcm(int module) : module_(module) { |
| 14 | for (int i = 0; i < 8; ++i) { |
| 15 | int32_t status = 0; |
| 16 | solenoid_handles_[i] = |
| 17 | HAL_InitializeSolenoidPort(HAL_GetPortWithModule(module_, i), &status); |
| 18 | if (status != 0) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 19 | AOS_LOG(FATAL, "Status was %d\n", status); |
Austin Schuh | 547cf8a | 2017-11-23 13:21:16 -0800 | [diff] [blame] | 20 | } |
| 21 | } |
| 22 | } |
| 23 | |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 24 | ::std::unique_ptr<BufferedSolenoid> BufferedPcm::MakeSolenoid(int number) { |
| 25 | return ::std::unique_ptr<BufferedSolenoid>( |
| 26 | new BufferedSolenoid(number, this)); |
| 27 | } |
| 28 | |
sabina | f558432 | 2017-09-23 18:37:19 -0700 | [diff] [blame] | 29 | void BufferedPcm::DoSet(int number, bool value) { |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 30 | if (value) { |
| 31 | values_ |= 1 << number; |
| 32 | } else { |
| 33 | values_ &= ~(1 << number); |
| 34 | } |
| 35 | } |
| 36 | |
Austin Schuh | 547cf8a | 2017-11-23 13:21:16 -0800 | [diff] [blame] | 37 | int32_t BufferedPcm::GetAll() { |
| 38 | int32_t status = 0; |
| 39 | int32_t result = HAL_GetAllSolenoids(module_, &status); |
| 40 | if (status != 0) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 41 | AOS_LOG(ERROR, "Failed to flush, %d\n", status); |
Austin Schuh | 547cf8a | 2017-11-23 13:21:16 -0800 | [diff] [blame] | 42 | return 0; |
| 43 | } |
| 44 | return result; |
| 45 | } |
| 46 | |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 47 | void BufferedPcm::Flush() { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 48 | AOS_LOG(DEBUG, "sending solenoids 0x%" PRIx8 "\n", values_); |
Austin Schuh | 547cf8a | 2017-11-23 13:21:16 -0800 | [diff] [blame] | 49 | int32_t status = 0; |
| 50 | HAL_SetAllSolenoids(module_, static_cast<int>(values_), &status); |
| 51 | if (status != 0) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 52 | AOS_LOG(ERROR, "Failed to flush, %d\n", status); |
Austin Schuh | 547cf8a | 2017-11-23 13:21:16 -0800 | [diff] [blame] | 53 | } |
Brian Silverman | d8f403a | 2014-12-13 19:12:04 -0500 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | } // namespace wpilib |
| 57 | } // namespace frc971 |