Added support for 2017 WPILib.

Change-Id: Ib22e31d0999b123c65498c31c654e3fa7bab4798
diff --git a/frc971/wpilib/buffered_pcm.cc b/frc971/wpilib/buffered_pcm.cc
index 35df795..2ddbfa7 100644
--- a/frc971/wpilib/buffered_pcm.cc
+++ b/frc971/wpilib/buffered_pcm.cc
@@ -22,8 +22,8 @@
 
 void BufferedPcm::Flush() {
   LOG(DEBUG, "sending solenoids 0x%" PRIx8 "\n", values_);
-#ifdef WPILIB2015
-  SolenoidBase::Set(values_, 0xFF);
+#ifdef WPILIB2017
+  SolenoidBase::SetAll(m_moduleNumber, values_);
 #else
   SolenoidBase::Set(values_, 0xFF, m_moduleNumber);
 #endif
diff --git a/frc971/wpilib/dma.cc b/frc971/wpilib/dma.cc
index ea9f64c..dbbfc56 100644
--- a/frc971/wpilib/dma.cc
+++ b/frc971/wpilib/dma.cc
@@ -8,6 +8,9 @@
 #include "DigitalSource.h"
 #include "AnalogInput.h"
 #include "Encoder.h"
+#ifdef WPILIB2017
+#include "HAL/HAL.h"
+#endif
 
 // Interface to the roboRIO FPGA's DMA features.
 
@@ -22,14 +25,13 @@
   };
 } t1Output;
 
-static const uint32_t kNumHeaders = 10;
+static const int32_t kNumHeaders = 10;
 
-#ifdef WPILIB2015
-static constexpr ssize_t kChannelSize[18] = {2, 2, 4, 4, 2, 2, 4, 4, 3, 3,
-                                             2, 1, 4, 4, 4, 4, 4, 4};
-#else
 static constexpr ssize_t kChannelSize[20] = {2, 2, 4, 4, 2, 2, 4, 4, 3, 3,
                                              2, 1, 4, 4, 4, 4, 4, 4, 4, 4};
+
+#ifndef WPILIB2017
+#define HAL_GetErrorMessage getHALErrorMessage
 #endif
 
 enum DMAOffsetConstants {
@@ -49,25 +51,17 @@
   kEnable_Counters_High = 13,
   kEnable_CounterTimers_Low = 14,
   kEnable_CounterTimers_High = 15,
-#ifdef WPILIB2015
-  kEnable_Encoders = 16,
-  kEnable_EncoderTimers = 17,
-#else
   kEnable_Encoders_Low = 16,
   kEnable_Encoders_High = 17,
   kEnable_EncoderTimers_Low = 18,
   kEnable_EncoderTimers_High = 19,
-#endif
 };
 
 DMA::DMA() {
   tRioStatusCode status = 0;
   tdma_config_ = tDMA::create(&status);
   tdma_config_->writeConfig_ExternalClock(false, &status);
-  wpi_setErrorWithContext(status, getHALErrorMessage(status));
-#ifdef WPILIB2015
-  NiFpga_WriteU32(0x10000, 0x1832c, 0x0);
-#endif
+  wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
   if (status != 0) {
     return;
   }
@@ -85,7 +79,7 @@
 void DMA::SetPause(bool pause) {
   tRioStatusCode status = 0;
   tdma_config_->writeConfig_Pause(pause, &status);
-  wpi_setErrorWithContext(status, getHALErrorMessage(status));
+  wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
 }
 
 void DMA::SetRate(uint32_t cycles) {
@@ -94,7 +88,7 @@
   }
   tRioStatusCode status = 0;
   tdma_config_->writeRate(cycles, &status);
-  wpi_setErrorWithContext(status, getHALErrorMessage(status));
+  wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
 }
 
 void DMA::Add(Encoder *encoder) {
@@ -107,17 +101,6 @@
   }
   const int index = encoder->GetFPGAIndex();
 
-#ifdef WPILIB2015
-  if (index < 4) {
-    // TODO(austin): Encoder uses a Counter for 1x or 2x; quad for 4x...
-    tdma_config_->writeConfig_Enable_Encoders(true, &status);
-  } else {
-    wpi_setErrorWithContext(
-        NiFpga_Status_InvalidParameter,
-        "FPGA encoder index is not in the 4 that get logged.");
-    return;
-  }
-#else
   if (index < 4) {
     // TODO(austin): Encoder uses a Counter for 1x or 2x; quad for 4x...
     tdma_config_->writeConfig_Enable_Encoders_Low(true, &status);
@@ -130,9 +113,8 @@
         "FPGA encoder index is not in the 4 that get logged.");
     return;
   }
-#endif
 
-  wpi_setErrorWithContext(status, getHALErrorMessage(status));
+  wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
 }
 
 void DMA::Add(DigitalSource * /*input*/) {
@@ -145,7 +127,7 @@
   }
 
   tdma_config_->writeConfig_Enable_DI(true, &status);
-  wpi_setErrorWithContext(status, getHALErrorMessage(status));
+  wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
 }
 
 void DMA::Add(AnalogInput *input) {
@@ -162,7 +144,7 @@
   } else {
     tdma_config_->writeConfig_Enable_AI0_High(true, &status);
   }
-  wpi_setErrorWithContext(status, getHALErrorMessage(status));
+  wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
 }
 
 void DMA::SetExternalTrigger(DigitalSource *input, bool rising, bool falling) {
@@ -190,13 +172,13 @@
   if (status == 0) {
     if (!is_external_clock) {
       tdma_config_->writeConfig_ExternalClock(true, &status);
-      wpi_setErrorWithContext(status, getHALErrorMessage(status));
+      wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
       if (status != 0) {
         return;
       }
     }
   } else {
-    wpi_setErrorWithContext(status, getHALErrorMessage(status));
+    wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
     return;
   }
 
@@ -206,7 +188,12 @@
   new_trigger.RisingEdge = rising;
   new_trigger.ExternalClockSource_AnalogTrigger = false;
   unsigned char module = 0;
-  unsigned char channel = input->GetChannelForRouting();
+  uint32_t channel =
+#ifdef WPILIB2017
+      input->GetChannel();
+#else
+      input->GetChannelForRouting();
+#endif
   if (channel >= kNumHeaders) {
     module = 1;
     channel -= kNumHeaders;
@@ -218,29 +205,12 @@
   new_trigger.ExternalClockSource_Channel = channel;
 
 // Configures the trigger to be external, not off the FPGA clock.
-#ifndef WPILIB2015
   tdma_config_->writeExternalTriggers(channel_index / 4, channel_index % 4,
                                       new_trigger, &status);
   if (status != 0) {
-    wpi_setErrorWithContext(status, getHALErrorMessage(status));
+    wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
     return;
   }
-#else
-  uint32_t current_triggers;
-  tRioStatusCode register_status =
-      NiFpga_ReadU32(0x10000, 0x1832c, &current_triggers);
-  if (register_status != 0) {
-    wpi_setErrorWithContext(register_status, getHALErrorMessage(status));
-    return;
-  }
-  current_triggers = (current_triggers & ~(0xff << (channel_index * 8))) |
-                     (new_trigger.value << (channel_index * 8));
-  register_status = NiFpga_WriteU32(0x10000, 0x1832c, current_triggers);
-  if (register_status != 0) {
-    wpi_setErrorWithContext(register_status, getHALErrorMessage(status));
-    return;
-  }
-#endif
 }
 
 DMA::ReadStatus DMA::Read(DMASample *sample, uint32_t timeout_ms,
@@ -269,7 +239,7 @@
   } else if (status == NiFpga_Status_FifoTimeout) {
     return STATUS_TIMEOUT;
   } else {
-    wpi_setErrorWithContext(status, getHALErrorMessage(status));
+    wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
     return STATUS_ERROR;
   }
 }
@@ -286,7 +256,7 @@
 void DMA::Start(size_t queue_depth) {
   tRioStatusCode status = 0;
   tconfig_ = tdma_config_->readConfig(&status);
-  wpi_setErrorWithContext(status, getHALErrorMessage(status));
+  wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
   if (status != 0) {
     return;
   }
@@ -317,15 +287,10 @@
     SET_SIZE(Enable_Counters_High);
     SET_SIZE(Enable_CounterTimers_Low);
     SET_SIZE(Enable_CounterTimers_High);
-#ifdef WPILIB2015
-    SET_SIZE(Enable_Encoders);
-    SET_SIZE(Enable_EncoderTimers);
-#else
     SET_SIZE(Enable_Encoders_Low);
     SET_SIZE(Enable_Encoders_High);
     SET_SIZE(Enable_EncoderTimers_Low);
     SET_SIZE(Enable_EncoderTimers_High);
-#endif
 #undef SET_SIZE
     capture_size_ = accum_size + 1;
   }
@@ -333,23 +298,23 @@
   manager_.reset(
       new nFPGA::tDMAManager(0, queue_depth * capture_size_, &status));
 
-  wpi_setErrorWithContext(status, getHALErrorMessage(status));
+  wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
   if (status != 0) {
     return;
   }
   // Start, stop, start to clear the buffer.
   manager_->start(&status);
-  wpi_setErrorWithContext(status, getHALErrorMessage(status));
+  wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
   if (status != 0) {
     return;
   }
   manager_->stop(&status);
-  wpi_setErrorWithContext(status, getHALErrorMessage(status));
+  wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
   if (status != 0) {
     return;
   }
   manager_->start(&status);
-  wpi_setErrorWithContext(status, getHALErrorMessage(status));
+  wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
   if (status != 0) {
     return;
   }
@@ -372,36 +337,30 @@
   if (offset(kEnable_DI) == -1) {
     wpi_setStaticErrorWithContext(
         dma_, NiFpga_Status_ResourceNotFound,
-        getHALErrorMessage(NiFpga_Status_ResourceNotFound));
+        HAL_GetErrorMessage(NiFpga_Status_ResourceNotFound));
     return false;
   }
-  if (input->GetChannelForRouting() < kNumHeaders) {
-    return (read_buffer_[offset(kEnable_DI)] >> input->GetChannelForRouting()) &
-           0x1;
+  const uint32_t channel =
+#ifdef WPILIB2017
+      input->GetChannel();
+#else
+      input->GetChannelForRouting();
+#endif
+  if (channel < kNumHeaders) {
+    return (read_buffer_[offset(kEnable_DI)] >> channel) & 0x1;
   } else {
-    return (read_buffer_[offset(kEnable_DI)] >>
-            (input->GetChannelForRouting() + 6)) &
-           0x1;
+    return (read_buffer_[offset(kEnable_DI)] >> (channel + 6)) & 0x1;
   }
 }
 
 int32_t DMASample::GetRaw(Encoder *input) const {
   int index = input->GetFPGAIndex();
   uint32_t dmaWord = 0;
-#ifdef WPILIB2015
-  if (index >= 4 || offset(kEnable_Encoders) == -1) {
-    wpi_setStaticErrorWithContext(
-        dma_, NiFpga_Status_ResourceNotFound,
-        getHALErrorMessage(NiFpga_Status_ResourceNotFound));
-    return -1;
-  }
-  dmaWord = read_buffer_[offset(kEnable_Encoders) + index];
-#else
   if (index < 4) {
     if (offset(kEnable_Encoders_Low) == -1) {
       wpi_setStaticErrorWithContext(
           dma_, NiFpga_Status_ResourceNotFound,
-          getHALErrorMessage(NiFpga_Status_ResourceNotFound));
+          HAL_GetErrorMessage(NiFpga_Status_ResourceNotFound));
       return -1;
     }
     dmaWord = read_buffer_[offset(kEnable_Encoders_Low) + index];
@@ -409,17 +368,16 @@
     if (offset(kEnable_Encoders_High) == -1) {
       wpi_setStaticErrorWithContext(
           dma_, NiFpga_Status_ResourceNotFound,
-          getHALErrorMessage(NiFpga_Status_ResourceNotFound));
+          HAL_GetErrorMessage(NiFpga_Status_ResourceNotFound));
       return -1;
     }
     dmaWord = read_buffer_[offset(kEnable_Encoders_High) + (index - 4)];
   } else {
     wpi_setStaticErrorWithContext(
         dma_, NiFpga_Status_ResourceNotFound,
-        getHALErrorMessage(NiFpga_Status_ResourceNotFound));
+        HAL_GetErrorMessage(NiFpga_Status_ResourceNotFound));
     return 0;
   }
-#endif
 
   int32_t result = 0;
 
@@ -448,7 +406,7 @@
     if (offset(kEnable_AI0_Low) == -1) {
       wpi_setStaticErrorWithContext(
           dma_, NiFpga_Status_ResourceNotFound,
-          getHALErrorMessage(NiFpga_Status_ResourceNotFound));
+          HAL_GetErrorMessage(NiFpga_Status_ResourceNotFound));
       return 0xffff;
     }
     dmaWord = read_buffer_[offset(kEnable_AI0_Low) + channel / 2];
@@ -456,14 +414,14 @@
     if (offset(kEnable_AI0_High) == -1) {
       wpi_setStaticErrorWithContext(
           dma_, NiFpga_Status_ResourceNotFound,
-          getHALErrorMessage(NiFpga_Status_ResourceNotFound));
+          HAL_GetErrorMessage(NiFpga_Status_ResourceNotFound));
       return 0xffff;
     }
     dmaWord = read_buffer_[offset(kEnable_AI0_High) + (channel - 4) / 2];
   } else {
     wpi_setStaticErrorWithContext(
         dma_, NiFpga_Status_ResourceNotFound,
-        getHALErrorMessage(NiFpga_Status_ResourceNotFound));
+        HAL_GetErrorMessage(NiFpga_Status_ResourceNotFound));
     return 0xffff;
   }
   if (channel % 2) {
diff --git a/frc971/wpilib/dma.h b/frc971/wpilib/dma.h
index 970497c..e07ab36 100644
--- a/frc971/wpilib/dma.h
+++ b/frc971/wpilib/dma.h
@@ -9,13 +9,30 @@
 #include <array>
 #include <memory>
 
+#ifdef WPILIB2017
+#include "HAL/ChipObject.h"
+#else
 #include "ChipObject.h"
+#endif
 #include "ErrorBase.h"
 
 class DMA;
+#ifdef WPILIB2017
+namespace frc {
 class DigitalSource;
 class AnalogInput;
 class Encoder;
+}  // namespace frc
+#else
+class DigitalSource;
+class AnalogInput;
+class Encoder;
+namespace frc {
+using ::DigitalSource;
+using ::AnalogInput;
+using ::Encoder;
+}  // namespace frc
+#endif
 
 // A POD class which stores the data from a DMA sample and provides safe ways to
 // access it.
diff --git a/frc971/wpilib/interrupt_edge_counting.cc b/frc971/wpilib/interrupt_edge_counting.cc
index 5c10b89..ec6f627 100644
--- a/frc971/wpilib/interrupt_edge_counting.cc
+++ b/frc971/wpilib/interrupt_edge_counting.cc
@@ -21,7 +21,11 @@
 
 void EdgeCounter::operator()() {
   ::aos::SetCurrentThreadName("EdgeCounter_" +
+#ifdef WPILIB2017
+                              ::std::to_string(input_->GetChannel()));
+#else
                               ::std::to_string(input_->GetChannelForRouting()));
+#endif
 
   input_->RequestInterrupts();
   input_->SetUpSourceEdge(true, true);
@@ -55,7 +59,11 @@
       current_value_ = hall_value;
     } else {
       LOG(WARNING, "Detected spurious edge on %d.  Dropping it.\n",
+#ifdef WPILIB2017
+          input_->GetChannel());
+#else
           input_->GetChannelForRouting());
+#endif
     }
   }
 }
diff --git a/frc971/wpilib/joystick_sender.cc b/frc971/wpilib/joystick_sender.cc
index 4942eaa..b4866e9 100644
--- a/frc971/wpilib/joystick_sender.cc
+++ b/frc971/wpilib/joystick_sender.cc
@@ -6,7 +6,11 @@
 #include "aos/common/logging/queue_logging.h"
 
 #include "DriverStation.h"
+#ifdef WPILIB2017
+#include "HAL/HAL.h"
+#else
 #include "HAL/HAL.hpp"
+#endif
 
 namespace frc971 {
 namespace wpilib {
@@ -27,8 +31,13 @@
     ds->WaitForData();
     auto new_state = ::aos::joystick_state.MakeMessage();
 
+#ifdef WPILIB2017
+    HAL_ControlWord control_word;
+    HAL_GetControlWord(&control_word);
+#else
     HALControlWord control_word;
     HALGetControlWord(&control_word);
+#endif
     new_state->test_mode = control_word.test;
     new_state->fms_attached = control_word.fmsAttached;
     new_state->enabled = control_word.enabled;
diff --git a/frc971/wpilib/wpilib_interface.cc b/frc971/wpilib/wpilib_interface.cc
index 016f52f..af72f7d 100644
--- a/frc971/wpilib/wpilib_interface.cc
+++ b/frc971/wpilib/wpilib_interface.cc
@@ -7,15 +7,25 @@
 #include "ControllerPower.h"
 #undef ERROR
 
+#ifndef WPILIB2017
+namespace frc {
+using ::DriverStation;
+}  // namespace frc
+#endif
+
 namespace frc971 {
 namespace wpilib {
 
-void SendRobotState(int32_t my_pid, DriverStation *ds) {
+void SendRobotState(int32_t my_pid, frc::DriverStation *ds) {
   auto new_state = ::aos::robot_state.MakeMessage();
 
   new_state->reader_pid = my_pid;
   new_state->outputs_enabled = ds->IsSysActive();
+#ifdef WPILIB2017
+  new_state->browned_out = ds->IsBrownedOut();
+#else
   new_state->browned_out = ds->IsSysBrownedOut();
+#endif
 
   new_state->is_3v3_active = ControllerPower::GetEnabled3V3();
   new_state->is_5v_active = ControllerPower::GetEnabled5V();
diff --git a/frc971/wpilib/wpilib_interface.h b/frc971/wpilib/wpilib_interface.h
index 216bf09..1c55cc7 100644
--- a/frc971/wpilib/wpilib_interface.h
+++ b/frc971/wpilib/wpilib_interface.h
@@ -3,13 +3,22 @@
 
 #include <stdint.h>
 
+#ifdef WPILIB2017
+namespace frc {
 class DriverStation;
+}  // namespace frc
+#else
+class DriverStation;
+namespace frc {
+using ::DriverStation;
+}  // namespace frc
+#endif
 
 namespace frc971 {
 namespace wpilib {
 
 // Sends out a message on ::aos::robot_state.
-void SendRobotState(int32_t my_pid, DriverStation *ds);
+void SendRobotState(int32_t my_pid, ::frc::DriverStation *ds);
 
 }  // namespace wpilib
 }  // namespace frc971