Brian Silverman | fff569f | 2013-04-19 21:00:55 -0700 | [diff] [blame] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) FIRST 2008. All Rights Reserved. */ |
| 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
| 4 | /* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */ |
| 5 | /*----------------------------------------------------------------------------*/ |
| 6 | |
| 7 | #include "Global.h" |
| 8 | #include "Utility.h" |
| 9 | |
| 10 | Global *Global::instance; |
| 11 | ReentrantSemaphore Global::instance_lock; |
| 12 | |
| 13 | Global *Global::GetInstance() { |
| 14 | Synchronized sync(instance_lock); |
| 15 | if (instance == NULL) { |
| 16 | instance = new Global(); |
| 17 | } |
| 18 | return instance; |
| 19 | } |
| 20 | |
| 21 | Global::Global() { |
| 22 | tRioStatusCode status = NiFpga_Status_Success; |
| 23 | global_.reset(tGlobal::create(&status)); |
| 24 | wpi_setError(status); |
| 25 | |
| 26 | AddToSingletonList(); |
| 27 | } |
| 28 | |
| 29 | Global::~Global() { |
| 30 | Synchronized sync(instance_lock); |
| 31 | instance = NULL; |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Return the FPGA Version number. |
| 36 | * For now, expect this to be competition year. |
| 37 | * @return FPGA Version number. |
| 38 | */ |
| 39 | UINT16 Global::GetFPGAVersion() |
| 40 | { |
| 41 | tRioStatusCode status = NiFpga_Status_Success; |
| 42 | UINT16 version = global_->readVersion(&status); |
| 43 | wpi_setError(status); |
| 44 | return version; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Return the FPGA Revision number. |
| 49 | * The format of the revision is 3 numbers. |
| 50 | * The 12 most significant bits are the Major Revision. |
| 51 | * the next 8 bits are the Minor Revision. |
| 52 | * The 12 least significant bits are the Build Number. |
| 53 | * @return FPGA Revision number. |
| 54 | */ |
| 55 | UINT32 Global::GetFPGARevision() |
| 56 | { |
| 57 | tRioStatusCode status = NiFpga_Status_Success; |
| 58 | UINT32 revision = global_->readRevision(&status); |
| 59 | wpi_setError(status); |
| 60 | return revision; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Read the microsecond-resolution timer on the FPGA. |
| 65 | * |
| 66 | * @return The current time in microseconds according to the FPGA (since FPGA reset). |
| 67 | */ |
| 68 | UINT32 Global::GetFPGATime() |
| 69 | { |
| 70 | tRioStatusCode status = NiFpga_Status_Success; |
| 71 | UINT32 time = global_->readLocalTime(&status); |
| 72 | wpi_setError(status); |
| 73 | return time; |
| 74 | } |
| 75 | |
| 76 | // RT hardware access functions exported from ni_emb.out |
| 77 | extern "C" |
| 78 | { |
| 79 | INT32 UserSwitchInput(INT32 nSwitch); |
| 80 | INT32 LedInput(INT32 led); |
| 81 | INT32 LedOutput(INT32 led, INT32 value); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Read the value of the USER1 DIP switch on the cRIO. |
| 86 | */ |
| 87 | INT32 Global::GetRIOUserSwitch() |
| 88 | { |
| 89 | INT32 switchValue = UserSwitchInput(0); |
| 90 | wpi_assert(switchValue >= 0); |
| 91 | return switchValue > 0; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Set the state of the USER1 status LED on the cRIO. |
| 96 | */ |
| 97 | void Global::SetRIOUserLED(UINT32 state) |
| 98 | { |
| 99 | LedOutput(0, state > 0); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Get the current state of the USER1 status LED on the cRIO. |
| 104 | * @return The curent state of the USER1 LED. |
| 105 | */ |
| 106 | INT32 Global::GetRIOUserLED() |
| 107 | { |
| 108 | return LedInput(0); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Toggle the state of the USER1 status LED on the cRIO. |
| 113 | * @return The new state of the USER1 LED. |
| 114 | */ |
| 115 | INT32 Global::ToggleRIOUserLED() |
| 116 | { |
| 117 | Synchronized sync(led_toggle_lock_); |
| 118 | INT32 ledState = !GetRIOUserLED(); |
| 119 | SetRIOUserLED(ledState); |
| 120 | return ledState; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Set the state of the FPGA status LED on the cRIO. |
| 125 | */ |
| 126 | void Global::SetRIO_FPGA_LED(UINT32 state) |
| 127 | { |
| 128 | tRioStatusCode status = NiFpga_Status_Success; |
| 129 | global_->writeFPGA_LED(state, &status); |
| 130 | wpi_setError(status); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Get the current state of the FPGA status LED on the cRIO. |
| 135 | * @return The curent state of the FPGA LED. |
| 136 | */ |
| 137 | INT32 Global::GetRIO_FPGA_LED() |
| 138 | { |
| 139 | tRioStatusCode status = NiFpga_Status_Success; |
| 140 | bool ledValue = global_->readFPGA_LED(&status); |
| 141 | wpi_setError(status); |
| 142 | return ledValue; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Toggle the state of the FPGA status LED on the cRIO. |
| 147 | * @return The new state of the FPGA LED. |
| 148 | */ |
| 149 | INT32 Global::ToggleRIO_FPGA_LED() |
| 150 | { |
| 151 | Synchronized sync(led_toggle_lock_); |
| 152 | INT32 ledState = !GetRIO_FPGA_LED(); |
| 153 | SetRIO_FPGA_LED(ledState); |
| 154 | return ledState; |
| 155 | } |