Move ADC code into individual boards where it belongs

Change-Id: Id8c2db1166c6e07caaf7d3ccc503bade4ec198f6
diff --git a/motors/big/medium_salsa.cc b/motors/big/medium_salsa.cc
index 0bd4489..80263cb 100644
--- a/motors/big/medium_salsa.cc
+++ b/motors/big/medium_salsa.cc
@@ -16,6 +16,79 @@
 namespace motors {
 namespace {
 
+struct MediumAdcReadings {
+  uint16_t motor_currents[3][2];
+  uint16_t motor_current_ref;
+  uint16_t input_voltage;
+};
+
+void AdcInitMedium() {
+  AdcInitCommon();
+
+  // M_CH2V ADC0_SE14
+  PORTC_PCR0 = PORT_PCR_MUX(0);
+
+  // M_CH0V ADC0_SE13
+  PORTB_PCR3 = PORT_PCR_MUX(0);
+
+  // M_CH1V ADC0_SE12
+  PORTB_PCR2 = PORT_PCR_MUX(0);
+
+  // M_CH0F ADC1_SE14
+  PORTB_PCR10 = PORT_PCR_MUX(0);
+
+  // M_CH1F ADC1_SE15
+  PORTB_PCR11 = PORT_PCR_MUX(0);
+
+  // M_VREF ADC0_SE18
+  PORTE_PCR25 = PORT_PCR_MUX(0);
+
+  // VIN ADC1_SE5B
+  PORTC_PCR9 = PORT_PCR_MUX(0);
+
+  // M_CH2F ADC1_SE17
+  PORTA_PCR17 = PORT_PCR_MUX(0);
+}
+
+MediumAdcReadings AdcReadMedium(const DisableInterrupts &) {
+  MediumAdcReadings r;
+
+  ADC1_SC1A = 14;
+  while (!(ADC1_SC1A & ADC_SC1_COCO)) {
+  }
+  ADC1_SC1A = 15;
+  r.motor_currents[0][0] = ADC1_RA;
+  while (!(ADC1_SC1A & ADC_SC1_COCO)) {
+  }
+  ADC1_SC1A = 17;
+  ADC0_SC1A = 18;
+  r.motor_currents[1][0] = ADC1_RA;
+  while (!(ADC1_SC1A & ADC_SC1_COCO)) {
+  }
+  ADC1_SC1A = 5;
+  r.motor_currents[2][0] = ADC1_RA;
+  while (!(ADC0_SC1A & ADC_SC1_COCO)) {
+  }
+  r.motor_current_ref = ADC0_RA;
+  while (!(ADC1_SC1A & ADC_SC1_COCO)) {
+  }
+  ADC1_SC1A = 14;
+  r.input_voltage = ADC1_RA;
+  while (!(ADC1_SC1A & ADC_SC1_COCO)) {
+  }
+  ADC1_SC1A = 15;
+  r.motor_currents[0][1] = ADC1_RA;
+  while (!(ADC1_SC1A & ADC_SC1_COCO)) {
+  }
+  ADC1_SC1A = 17;
+  r.motor_currents[1][1] = ADC1_RA;
+  while (!(ADC1_SC1A & ADC_SC1_COCO)) {
+  }
+  r.motor_currents[2][1] = ADC1_RA;
+
+  return r;
+}
+
 ::std::atomic<Motor *> global_motor{nullptr};
 
 extern "C" {
diff --git a/motors/button_board.cc b/motors/button_board.cc
index 6d5d11e..676331b 100644
--- a/motors/button_board.cc
+++ b/motors/button_board.cc
@@ -18,6 +18,46 @@
 namespace motors {
 namespace {
 
+struct JoystickAdcReadings {
+  uint16_t analog0, analog1, analog2, analog3;
+};
+
+void AdcInitJoystick() {
+  AdcInitCommon();
+
+  // ANALOG0 ADC0_SE5b
+  PORTD_PCR1 = PORT_PCR_MUX(0);
+  // ANALOG1 ADC0_SE14
+  PORTC_PCR0 = PORT_PCR_MUX(0);
+  // ANALOG2 ADC0_SE13
+  PORTB_PCR3 = PORT_PCR_MUX(0);
+  // ANALOG3 ADC0_SE12
+  PORTB_PCR2 = PORT_PCR_MUX(0);
+}
+
+JoystickAdcReadings AdcReadJoystick(const DisableInterrupts &) {
+  JoystickAdcReadings r;
+
+  ADC0_SC1A = 5;
+  while (!(ADC0_SC1A & ADC_SC1_COCO)) {
+  }
+  ADC0_SC1A = 14;
+  r.analog0 = ADC0_RA;
+  while (!(ADC0_SC1A & ADC_SC1_COCO)) {
+  }
+  ADC0_SC1A = 13;
+  r.analog1 = ADC0_RA;
+  while (!(ADC0_SC1A & ADC_SC1_COCO)) {
+  }
+  ADC0_SC1A = 12;
+  r.analog2 = ADC0_RA;
+  while (!(ADC0_SC1A & ADC_SC1_COCO)) {
+  }
+  r.analog3 = ADC0_RA;
+
+  return r;
+}
+
 ::std::atomic<teensy::AcmTty *> global_stdout{nullptr};
 
 // The HID report descriptor we use.
diff --git a/motors/motor.h b/motors/motor.h
index f657d13..ec96efb 100644
--- a/motors/motor.h
+++ b/motors/motor.h
@@ -131,31 +131,6 @@
   }
 
  private:
-  // Represents the ADC reading which is closest to an edge.
-  struct CloseAdcReading {
-    // Adds a new reading to the readings to balance or pushes the previous
-    // closest one there and saves off this one.
-    //
-    // Returns true if it saves off the new reading.
-    bool MaybeUse(int new_distance, const MediumAdcReadings &adc_readings,
-                  int phase, int sample, ReadingsToBalance *to_balance) {
-      if (new_distance < distance) {
-        if (distance != INT_MAX) {
-          to_balance->Add(index, value);
-        }
-        distance = new_distance;
-        value = adc_readings.motor_currents[phase][sample];
-        index = phase;
-        return true;
-      }
-      return false;
-    }
-
-    int distance = INT_MAX;
-    int32_t value = 0;
-    int index = 0;
-  };
-
   inline int counts_per_cycle() const {
     return BUS_CLOCK_FREQUENCY / SWITCHING_FREQUENCY / switching_divisor_;
   }
diff --git a/motors/peripheral/adc.cc b/motors/peripheral/adc.cc
index 6249392..756e166 100644
--- a/motors/peripheral/adc.cc
+++ b/motors/peripheral/adc.cc
@@ -44,6 +44,8 @@
     ADC##n##_SC3 = 0 /* Disable hardware averaging. */;                      \
   } while (0)
 
+}  // namespace
+
 void AdcInitCommon() {
   SIM_SCGC3 |= SIM_SCGC3_ADC1;
   SIM_SCGC6 |= SIM_SCGC6_ADC0;
@@ -52,223 +54,5 @@
   ADC_INIT_SINGLE(1);
 }
 
-}  // namespace
-
-void AdcInitMedium() {
-  AdcInitCommon();
-
-  // M_CH2V ADC0_SE14
-  PORTC_PCR0 = PORT_PCR_MUX(0);
-
-  // M_CH0V ADC0_SE13
-  PORTB_PCR3 = PORT_PCR_MUX(0);
-
-  // M_CH1V ADC0_SE12
-  PORTB_PCR2 = PORT_PCR_MUX(0);
-
-  // M_CH0F ADC1_SE14
-  PORTB_PCR10 = PORT_PCR_MUX(0);
-
-  // M_CH1F ADC1_SE15
-  PORTB_PCR11 = PORT_PCR_MUX(0);
-
-  // M_VREF ADC0_SE18
-  PORTE_PCR25 = PORT_PCR_MUX(0);
-
-  // VIN ADC1_SE5B
-  PORTC_PCR9 = PORT_PCR_MUX(0);
-
-  // M_CH2F ADC1_SE17
-  PORTA_PCR17 = PORT_PCR_MUX(0);
-}
-
-void AdcInitSmall() {
-  AdcInitCommon();
-
-  // M0_CH0F ADC1_SE17
-  PORTA_PCR17 = PORT_PCR_MUX(0);
-
-  // M0_CH1F ADC1_SE14
-  PORTB_PCR10 = PORT_PCR_MUX(0);
-
-  // M0_CH2F ADC1_SE15
-  PORTB_PCR11 = PORT_PCR_MUX(0);
-
-  // M0_ABS ADC0_SE5b
-  PORTD_PCR1 = PORT_PCR_MUX(0);
-
-  // M1_CH0F ADC0_SE13
-  PORTB_PCR3 = PORT_PCR_MUX(0);
-
-  // M1_CH1F ADC0_SE12
-  PORTB_PCR2 = PORT_PCR_MUX(0);
-
-  // M1_CH2F ADC0_SE14
-  PORTC_PCR0 = PORT_PCR_MUX(0);
-
-  // M1_ABS ADC0_SE17
-  PORTE_PCR24 = PORT_PCR_MUX(0);
-
-  // WHEEL_ABS ADC0_SE18
-  PORTE_PCR25 = PORT_PCR_MUX(0);
-
-  // VIN ADC1_SE5B
-  PORTC_PCR9 = PORT_PCR_MUX(0);
-}
-
-void AdcInitJoystick() {
-  AdcInitCommon();
-
-  // ANALOG0 ADC0_SE5b
-  PORTD_PCR1 = PORT_PCR_MUX(0);
-  // ANALOG1 ADC0_SE14
-  PORTC_PCR0 = PORT_PCR_MUX(0);
-  // ANALOG2 ADC0_SE13
-  PORTB_PCR3 = PORT_PCR_MUX(0);
-  // ANALOG3 ADC0_SE12
-  PORTB_PCR2 = PORT_PCR_MUX(0);
-}
-
-void AdcInitSimple() {
-  AdcInitCommon();
-
-  // ENC_SIN ADC0_SE23
-  // ENC_COS ADC1_SE23
-}
-
-MediumAdcReadings AdcReadMedium(const DisableInterrupts &) {
-  MediumAdcReadings r;
-
-  ADC1_SC1A = 14;
-  while (!(ADC1_SC1A & ADC_SC1_COCO)) {
-  }
-  ADC1_SC1A = 15;
-  r.motor_currents[0][0] = ADC1_RA;
-  while (!(ADC1_SC1A & ADC_SC1_COCO)) {
-  }
-  ADC1_SC1A = 17;
-  ADC0_SC1A = 18;
-  r.motor_currents[1][0] = ADC1_RA;
-  while (!(ADC1_SC1A & ADC_SC1_COCO)) {
-  }
-  ADC1_SC1A = 5;
-  r.motor_currents[2][0] = ADC1_RA;
-  while (!(ADC0_SC1A & ADC_SC1_COCO)) {
-  }
-  r.motor_current_ref = ADC0_RA;
-  while (!(ADC1_SC1A & ADC_SC1_COCO)) {
-  }
-  ADC1_SC1A = 14;
-  r.input_voltage = ADC1_RA;
-  while (!(ADC1_SC1A & ADC_SC1_COCO)) {
-  }
-  ADC1_SC1A = 15;
-  r.motor_currents[0][1] = ADC1_RA;
-  while (!(ADC1_SC1A & ADC_SC1_COCO)) {
-  }
-  ADC1_SC1A = 17;
-  r.motor_currents[1][1] = ADC1_RA;
-  while (!(ADC1_SC1A & ADC_SC1_COCO)) {
-  }
-  r.motor_currents[2][1] = ADC1_RA;
-
-  return r;
-}
-
-SmallAdcReadings AdcReadSmall0(const DisableInterrupts &) {
-  SmallAdcReadings r;
-
-  ADC1_SC1A = 17;
-  while (!(ADC1_SC1A & ADC_SC1_COCO)) {
-  }
-  ADC1_SC1A = 14;
-  r.currents[0] = ADC1_RA;
-  while (!(ADC1_SC1A & ADC_SC1_COCO)) {
-  }
-  ADC1_SC1A = 15;
-  r.currents[1] = ADC1_RA;
-  while (!(ADC1_SC1A & ADC_SC1_COCO)) {
-  }
-  r.currents[2] = ADC1_RA;
-
-  return r;
-}
-
-SmallAdcReadings AdcReadSmall1(const DisableInterrupts &) {
-  SmallAdcReadings r;
-
-  ADC0_SC1A = 13;
-  while (!(ADC0_SC1A & ADC_SC1_COCO)) {
-  }
-  ADC0_SC1A = 12;
-  r.currents[0] = ADC0_RA;
-  while (!(ADC0_SC1A & ADC_SC1_COCO)) {
-  }
-  ADC0_SC1A = 14;
-  r.currents[1] = ADC0_RA;
-  while (!(ADC0_SC1A & ADC_SC1_COCO)) {
-  }
-  r.currents[2] = ADC0_RA;
-
-  return r;
-}
-
-SmallInitReadings AdcReadSmallInit(const DisableInterrupts &) {
-  SmallInitReadings r;
-
-  ADC0_SC1A = 5;
-  while (!(ADC0_SC1A & ADC_SC1_COCO)) {
-  }
-  ADC0_SC1A = 17;
-  r.motor0_abs = ADC0_RA;
-  while (!(ADC0_SC1A & ADC_SC1_COCO)) {
-  }
-  ADC0_SC1A = 18;
-  r.motor1_abs = ADC0_RA;
-  while (!(ADC0_SC1A & ADC_SC1_COCO)) {
-  }
-  r.wheel_abs = ADC0_RA;
-
-  return r;
-}
-
-JoystickAdcReadings AdcReadJoystick(const DisableInterrupts &) {
-  JoystickAdcReadings r;
-
-  ADC0_SC1A = 5;
-  while (!(ADC0_SC1A & ADC_SC1_COCO)) {
-  }
-  ADC0_SC1A = 14;
-  r.analog0 = ADC0_RA;
-  while (!(ADC0_SC1A & ADC_SC1_COCO)) {
-  }
-  ADC0_SC1A = 13;
-  r.analog1 = ADC0_RA;
-  while (!(ADC0_SC1A & ADC_SC1_COCO)) {
-  }
-  ADC0_SC1A = 12;
-  r.analog2 = ADC0_RA;
-  while (!(ADC0_SC1A & ADC_SC1_COCO)) {
-  }
-  r.analog3 = ADC0_RA;
-
-  return r;
-}
-
-SimpleAdcReadings AdcReadSimple(const DisableInterrupts &) {
-  SimpleAdcReadings r;
-
-  ADC0_SC1A = 23;
-  ADC1_SC1A = 23;
-  while (!(ADC0_SC1A & ADC_SC1_COCO)) {
-  }
-  while (!(ADC1_SC1A & ADC_SC1_COCO)) {
-  }
-  r.sin = ADC0_RA;
-  r.cos = ADC1_RA;
-
-  return r;
-}
-
 }  // namespace motors
 }  // namespace frc971
diff --git a/motors/peripheral/adc.h b/motors/peripheral/adc.h
index 677fa18..9ca5506 100644
--- a/motors/peripheral/adc.h
+++ b/motors/peripheral/adc.h
@@ -5,47 +5,10 @@
 
 #include "motors/util.h"
 
-// TODO(Brian): Avoid cramming all the code for each specific application into a
-// single file like this.
-
 namespace frc971 {
 namespace motors {
 
-struct MediumAdcReadings {
-  uint16_t motor_currents[3][2];
-  uint16_t motor_current_ref;
-  uint16_t input_voltage;
-};
-
-struct SmallAdcReadings {
-  uint16_t currents[3];
-};
-
-struct SmallInitReadings {
-  uint16_t motor0_abs;
-  uint16_t motor1_abs;
-  uint16_t wheel_abs;
-};
-
-struct JoystickAdcReadings {
-  uint16_t analog0, analog1, analog2, analog3;
-};
-
-struct SimpleAdcReadings {
-  uint16_t sin, cos;
-};
-
-void AdcInitMedium();
-void AdcInitSmall();
-void AdcInitJoystick();
-void AdcInitSimple();
-
-MediumAdcReadings AdcReadMedium(const DisableInterrupts &);
-SmallAdcReadings AdcReadSmall0(const DisableInterrupts &);
-SmallAdcReadings AdcReadSmall1(const DisableInterrupts &);
-SmallInitReadings AdcReadSmallInit(const DisableInterrupts &);
-JoystickAdcReadings AdcReadJoystick(const DisableInterrupts &);
-SimpleAdcReadings AdcReadSimple(const DisableInterrupts &);
+void AdcInitCommon();
 
 }  // namespace motors
 }  // namespace frc971
diff --git a/motors/pistol_grip/controller.cc b/motors/pistol_grip/controller.cc
index 28e1503..9d8a1c8 100644
--- a/motors/pistol_grip/controller.cc
+++ b/motors/pistol_grip/controller.cc
@@ -34,6 +34,107 @@
 using ::frc971::control_loops::drivetrain::MakeIntegralHapticWheelPlant;
 using ::frc971::control_loops::drivetrain::MakeIntegralHapticWheelObserver;
 
+struct SmallAdcReadings {
+  uint16_t currents[3];
+};
+
+struct SmallInitReadings {
+  uint16_t motor0_abs;
+  uint16_t motor1_abs;
+  uint16_t wheel_abs;
+};
+
+void AdcInitSmall() {
+  AdcInitCommon();
+
+  // M0_CH0F ADC1_SE17
+  PORTA_PCR17 = PORT_PCR_MUX(0);
+
+  // M0_CH1F ADC1_SE14
+  PORTB_PCR10 = PORT_PCR_MUX(0);
+
+  // M0_CH2F ADC1_SE15
+  PORTB_PCR11 = PORT_PCR_MUX(0);
+
+  // M0_ABS ADC0_SE5b
+  PORTD_PCR1 = PORT_PCR_MUX(0);
+
+  // M1_CH0F ADC0_SE13
+  PORTB_PCR3 = PORT_PCR_MUX(0);
+
+  // M1_CH1F ADC0_SE12
+  PORTB_PCR2 = PORT_PCR_MUX(0);
+
+  // M1_CH2F ADC0_SE14
+  PORTC_PCR0 = PORT_PCR_MUX(0);
+
+  // M1_ABS ADC0_SE17
+  PORTE_PCR24 = PORT_PCR_MUX(0);
+
+  // WHEEL_ABS ADC0_SE18
+  PORTE_PCR25 = PORT_PCR_MUX(0);
+
+  // VIN ADC1_SE5B
+  PORTC_PCR9 = PORT_PCR_MUX(0);
+}
+
+SmallAdcReadings AdcReadSmall0(const DisableInterrupts &) {
+  SmallAdcReadings r;
+
+  ADC1_SC1A = 17;
+  while (!(ADC1_SC1A & ADC_SC1_COCO)) {
+  }
+  ADC1_SC1A = 14;
+  r.currents[0] = ADC1_RA;
+  while (!(ADC1_SC1A & ADC_SC1_COCO)) {
+  }
+  ADC1_SC1A = 15;
+  r.currents[1] = ADC1_RA;
+  while (!(ADC1_SC1A & ADC_SC1_COCO)) {
+  }
+  r.currents[2] = ADC1_RA;
+
+  return r;
+}
+
+SmallAdcReadings AdcReadSmall1(const DisableInterrupts &) {
+  SmallAdcReadings r;
+
+  ADC0_SC1A = 13;
+  while (!(ADC0_SC1A & ADC_SC1_COCO)) {
+  }
+  ADC0_SC1A = 12;
+  r.currents[0] = ADC0_RA;
+  while (!(ADC0_SC1A & ADC_SC1_COCO)) {
+  }
+  ADC0_SC1A = 14;
+  r.currents[1] = ADC0_RA;
+  while (!(ADC0_SC1A & ADC_SC1_COCO)) {
+  }
+  r.currents[2] = ADC0_RA;
+
+  return r;
+}
+
+SmallInitReadings AdcReadSmallInit(const DisableInterrupts &) {
+  SmallInitReadings r;
+
+  ADC0_SC1A = 5;
+  while (!(ADC0_SC1A & ADC_SC1_COCO)) {
+  }
+  ADC0_SC1A = 17;
+  r.motor0_abs = ADC0_RA;
+  while (!(ADC0_SC1A & ADC_SC1_COCO)) {
+  }
+  ADC0_SC1A = 18;
+  r.motor1_abs = ADC0_RA;
+  while (!(ADC0_SC1A & ADC_SC1_COCO)) {
+  }
+  r.wheel_abs = ADC0_RA;
+
+  return r;
+}
+
 constexpr float kHapticWheelCurrentLimit = static_cast<float>(
     ::frc971::control_loops::drivetrain::kHapticWheelCurrentLimit);
 constexpr float kHapticTriggerCurrentLimit = static_cast<float>(
diff --git a/motors/simple_receiver.cc b/motors/simple_receiver.cc
index 49f5ecb..20ca298 100644
--- a/motors/simple_receiver.cc
+++ b/motors/simple_receiver.cc
@@ -29,6 +29,32 @@
 using ::frc971::control_loops::DrivetrainQueue_Output;
 using ::motors::seems_reasonable::Spring;
 
+struct SimpleAdcReadings {
+  uint16_t sin, cos;
+};
+
+void AdcInitSimple() {
+  AdcInitCommon();
+
+  // ENC_SIN ADC0_SE23
+  // ENC_COS ADC1_SE23
+}
+
+SimpleAdcReadings AdcReadSimple(const DisableInterrupts &) {
+  SimpleAdcReadings r;
+
+  ADC0_SC1A = 23;
+  ADC1_SC1A = 23;
+  while (!(ADC0_SC1A & ADC_SC1_COCO)) {
+  }
+  while (!(ADC1_SC1A & ADC_SC1_COCO)) {
+  }
+  r.sin = ADC0_RA;
+  r.cos = ADC1_RA;
+
+  return r;
+}
+
 const ShifterHallEffect kThreeStateDriveShifter{0.0, 0.0, 0.25, 0.75};
 
 const DrivetrainConfig<float> &GetDrivetrainConfig() {