Initial stab at the new WPILib.

Change-Id: Id04cc07649959566deb5b4fa637267072a5191ca
diff --git a/frc971/wpilib/dma.cc b/frc971/wpilib/dma.cc
index dbbfc56..8dca72c 100644
--- a/frc971/wpilib/dma.cc
+++ b/frc971/wpilib/dma.cc
@@ -8,9 +8,7 @@
 #include "DigitalSource.h"
 #include "AnalogInput.h"
 #include "Encoder.h"
-#ifdef WPILIB2017
 #include "HAL/HAL.h"
-#endif
 
 // Interface to the roboRIO FPGA's DMA features.
 
@@ -30,10 +28,6 @@
 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 {
   kEnable_AI0_Low = 0,
   kEnable_AI0_High = 1,
@@ -188,12 +182,7 @@
   new_trigger.RisingEdge = rising;
   new_trigger.ExternalClockSource_AnalogTrigger = false;
   unsigned char module = 0;
-  uint32_t channel =
-#ifdef WPILIB2017
-      input->GetChannel();
-#else
-      input->GetChannelForRouting();
-#endif
+  uint32_t channel = input->GetChannel();
   if (channel >= kNumHeaders) {
     module = 1;
     channel -= kNumHeaders;
@@ -228,6 +217,7 @@
   sample->dma_ = this;
   manager_->read(sample->read_buffer_, capture_size_, timeout_ms,
                  &remainingBytes, &status);
+  sample->CalculateTimestamp();
 
   // TODO(jerry): Do this only if status == 0?
   *remaining_out = remainingBytes / capture_size_;
@@ -296,7 +286,7 @@
   }
 
   manager_.reset(
-      new nFPGA::tDMAManager(0, queue_depth * capture_size_, &status));
+      new nFPGA::tDMAManager(1, queue_depth * capture_size_, &status));
 
   wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
   if (status != 0) {
@@ -323,10 +313,23 @@
 
 static_assert(::std::is_pod<DMASample>::value, "DMASample needs to be POD");
 
-ssize_t DMASample::offset(int index) const { return dma_->channel_offsets_[index]; }
+ssize_t DMASample::offset(int index) const {
+  return dma_->channel_offsets_[index];
+}
 
-uint32_t DMASample::GetTime() const {
-  return read_buffer_[dma_->capture_size_ - 1];
+void DMASample::CalculateTimestamp() {
+  uint32_t lower_sample = read_buffer_[dma_->capture_size_ - 1];
+#if WPILIB2018
+  int32_t status = 0;
+  fpga_timestamp_ = HAL_ExpandFPGATime(lower_sample, &status);
+  assert(status == 0);
+#else
+  fpga_timestamp_ = lower_sample;
+#endif
+}
+
+uint64_t DMASample::GetTime() const {
+  return fpga_timestamp_;
 }
 
 double DMASample::GetTimestamp() const {
@@ -340,12 +343,7 @@
         HAL_GetErrorMessage(NiFpga_Status_ResourceNotFound));
     return false;
   }
-  const uint32_t channel =
-#ifdef WPILIB2017
-      input->GetChannel();
-#else
-      input->GetChannelForRouting();
-#endif
+  const uint32_t channel = input->GetChannel();
   if (channel < kNumHeaders) {
     return (read_buffer_[offset(kEnable_DI)] >> channel) & 0x1;
   } else {
diff --git a/frc971/wpilib/dma.h b/frc971/wpilib/dma.h
index e07ab36..0869b0d 100644
--- a/frc971/wpilib/dma.h
+++ b/frc971/wpilib/dma.h
@@ -9,7 +9,7 @@
 #include <array>
 #include <memory>
 
-#ifdef WPILIB2017
+#if defined(WPILIB2017) || defined(WPILIB2018)
 #include "HAL/ChipObject.h"
 #else
 #include "ChipObject.h"
@@ -17,7 +17,7 @@
 #include "ErrorBase.h"
 
 class DMA;
-#ifdef WPILIB2017
+#if defined(WPILIB2017) || defined(WPILIB2018)
 namespace frc {
 class DigitalSource;
 class AnalogInput;
@@ -43,7 +43,7 @@
   // Returns the FPGA timestamp of the sample in seconds.
   double GetTimestamp() const;
   // Returns the FPGA timestamp of the sample in microseconds.
-  uint32_t GetTime() const;
+  uint64_t GetTime() const;
 
   // All Get methods either return the requested value, or set the Error.
 
@@ -61,6 +61,8 @@
  private:
   friend DMA;
 
+  void CalculateTimestamp();
+
   // Returns the offset of the sample type in the buffer, or -1 if it isn't in
   // the sample.
   ssize_t offset(int index) const;
@@ -69,6 +71,7 @@
   // into WPILib.
 
   DMA *dma_;
+  uint64_t fpga_timestamp_;
   uint32_t read_buffer_[64];
 };
 
diff --git a/frc971/wpilib/dma_edge_counting.cc b/frc971/wpilib/dma_edge_counting.cc
index afb880f..8487ce9 100644
--- a/frc971/wpilib/dma_edge_counting.cc
+++ b/frc971/wpilib/dma_edge_counting.cc
@@ -17,11 +17,9 @@
 
   if (!previous_value && ExtractValue(sample)) {
     pos_edge_count_++;
-    pos_edge_time_ = sample.GetTimestamp();
     pos_last_encoder_ = sample.GetRaw(encoder_);
   } else if (previous_value && !ExtractValue(sample)) {
     neg_edge_count_++;
-    neg_edge_time_ = sample.GetTimestamp();
     neg_last_encoder_ = sample.GetRaw(encoder_);
   }
 }
diff --git a/frc971/wpilib/dma_edge_counting.h b/frc971/wpilib/dma_edge_counting.h
index 64722ca..c71e7ae 100644
--- a/frc971/wpilib/dma_edge_counting.h
+++ b/frc971/wpilib/dma_edge_counting.h
@@ -90,12 +90,10 @@
 
   // Values related to the positive edge.
   int pos_edge_count_ = 0;
-  double pos_edge_time_ = 0.0;
   int pos_last_encoder_ = 0;
 
   // Values related to the negative edge.
   int neg_edge_count_ = 0;
-  double neg_edge_time_ = 0.0;
   int neg_last_encoder_ = 0;
 
   bool polled_value_ = false;
diff --git a/frc971/wpilib/interrupt_edge_counting.cc b/frc971/wpilib/interrupt_edge_counting.cc
index ec6f627..3f81501 100644
--- a/frc971/wpilib/interrupt_edge_counting.cc
+++ b/frc971/wpilib/interrupt_edge_counting.cc
@@ -21,11 +21,7 @@
 
 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);
@@ -59,11 +55,7 @@
       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 a490e67..7d406d0 100644
--- a/frc971/wpilib/joystick_sender.cc
+++ b/frc971/wpilib/joystick_sender.cc
@@ -6,7 +6,7 @@
 #include "aos/common/logging/queue_logging.h"
 
 #include "DriverStation.h"
-#ifdef WPILIB2017
+#if defined(WPILIB2017) || defined(WPILIB2018)
 #include "HAL/HAL.h"
 #else
 #include "HAL/HAL.hpp"
@@ -31,7 +31,7 @@
     ds->WaitForData();
     auto new_state = ::aos::joystick_state.MakeMessage();
 
-#ifdef WPILIB2017
+#if defined(WPILIB2017) || defined(WPILIB2018)
     HAL_ControlWord control_word;
     HAL_GetControlWord(&control_word);
 #else
diff --git a/frc971/wpilib/wpilib_interface.cc b/frc971/wpilib/wpilib_interface.cc
index af72f7d..c886907 100644
--- a/frc971/wpilib/wpilib_interface.cc
+++ b/frc971/wpilib/wpilib_interface.cc
@@ -3,37 +3,27 @@
 #include "aos/common/messages/robot_state.q.h"
 #include "aos/common/logging/queue_logging.h"
 
-#include "DriverStation.h"
-#include "ControllerPower.h"
-#undef ERROR
-
-#ifndef WPILIB2017
-namespace frc {
-using ::DriverStation;
-}  // namespace frc
-#endif
+#include <HAL/HAL.h>
 
 namespace frc971 {
 namespace wpilib {
 
-void SendRobotState(int32_t my_pid, frc::DriverStation *ds) {
+void SendRobotState(int32_t my_pid) {
   auto new_state = ::aos::robot_state.MakeMessage();
 
+  int32_t status = 0;
+
   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->outputs_enabled = HAL_GetSystemActive(&status);
+  new_state->browned_out = HAL_GetBrownedOut(&status);
 
-  new_state->is_3v3_active = ControllerPower::GetEnabled3V3();
-  new_state->is_5v_active = ControllerPower::GetEnabled5V();
-  new_state->voltage_3v3 = ControllerPower::GetVoltage3V3();
-  new_state->voltage_5v = ControllerPower::GetVoltage5V();
+  new_state->is_3v3_active = HAL_GetUserActive3V3(&status);
+  new_state->is_5v_active = HAL_GetUserActive5V(&status);
+  new_state->voltage_3v3 = HAL_GetUserVoltage3V3(&status);
+  new_state->voltage_5v = HAL_GetUserVoltage5V(&status);
 
-  new_state->voltage_roborio_in = ControllerPower::GetInputVoltage();
-  new_state->voltage_battery = ds->GetBatteryVoltage();
+  new_state->voltage_roborio_in = HAL_GetVinVoltage(&status);
+  new_state->voltage_battery = HAL_GetVinVoltage(&status);
 
   LOG_STRUCT(DEBUG, "robot_state", *new_state);
 
diff --git a/frc971/wpilib/wpilib_interface.h b/frc971/wpilib/wpilib_interface.h
index 1c55cc7..100c67e 100644
--- a/frc971/wpilib/wpilib_interface.h
+++ b/frc971/wpilib/wpilib_interface.h
@@ -3,22 +3,11 @@
 
 #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, ::frc::DriverStation *ds);
+void SendRobotState(int32_t my_pid);
 
 }  // namespace wpilib
 }  // namespace frc971