From 75f9ea345d45523c93ddf5a535c4d5b013f381cb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Sergi=20Hern=C3=A1ndez?= <shernand@iri.upc.edu>
Date: Sun, 3 Apr 2016 11:33:26 +0000
Subject: [PATCH] Added the necessary API to access the ADC module.

---
 src/darwin_robot.cpp        | 66 +++++++++++++++++++++++++++++++++++++
 src/darwin_robot.h          | 14 ++++++--
 src/examples/CMakeLists.txt |  5 +++
 3 files changed, 83 insertions(+), 2 deletions(-)

diff --git a/src/darwin_robot.cpp b/src/darwin_robot.cpp
index a3ad4e5..02c857f 100644
--- a/src/darwin_robot.cpp
+++ b/src/darwin_robot.cpp
@@ -193,6 +193,72 @@ bool CDarwinRobot::is_button_pressed(pushbutton_t pushbutton)
     throw CDarwinRobotException(_HERE_,"Invalid robot device");
 }
 
+// ADC interface
+void CDarwinRobot::adc_enable(void)
+{
+  if(this->robot_device!=NULL)
+    this->robot_device->write_byte_register(DARWIN_ADC_CNTRL,ADC_START);
+  else
+    throw CDarwinRobotException(_HERE_,"Invalid robot device");
+}
+
+void CDarwinRobot::adc_set_period(unsigned char period_ms)
+{
+  if(this->robot_device!=NULL)
+    this->robot_device->write_byte_register(DARWIN_ADC_PERIOD,period_ms);
+  else
+    throw CDarwinRobotException(_HERE_,"Invalid robot device");
+}
+double CDarwinRobot::adc_get_value(adc_t adc)
+{
+  unsigned short int value;
+
+  if(this->robot_device!=NULL)
+  {
+    this->robot_device->read_word_register(DARWIN_ADC_CH1_L+((int)adc)*2,&value);
+
+    return ((double)value)/((double)(1<<12));
+  }
+  else
+    throw CDarwinRobotException(_HERE_,"Invalid robot device");
+}
+
+double CDarwinRobot::adc_get_temperature(void)
+{
+  unsigned short int value;
+
+  if(this->robot_device!=NULL)
+  {
+    this->robot_device->read_word_register(DARWIN_ADC_TEMP_L,&value);
+
+    return ((double)value)/((double)(1<<10));
+  }
+  else
+    throw CDarwinRobotException(_HERE_,"Invalid robot device");
+}
+
+double CDarwinRobot::adc_get_vrefint(void)
+{
+  unsigned short int value;
+
+  if(this->robot_device!=NULL)
+  {
+    this->robot_device->read_word_register(DARWIN_ADC_VREF_L,&value);
+
+    return ((double)value)/((double)(1<<12));
+  }
+  else
+    throw CDarwinRobotException(_HERE_,"Invalid robot device");
+}
+
+void CDarwinRobot::adc_disable(void)
+{
+  if(this->robot_device!=NULL)
+    this->robot_device->write_byte_register(DARWIN_ADC_CNTRL,ADC_STOP);
+  else
+    throw CDarwinRobotException(_HERE_,"Invalid robot device");
+}
+
 // motion manager interface
 /*void CDarwinRobot::mm_set_period(double period_ms)
 {
diff --git a/src/darwin_robot.h b/src/darwin_robot.h
index 3bb810a..ee2cf12 100644
--- a/src/darwin_robot.h
+++ b/src/darwin_robot.h
@@ -31,6 +31,11 @@ typedef struct
 
 typedef enum {START_PB=0,MODE_PB=1} pushbutton_t;
 
+#define           ADC_NUM_CHANNELS      16
+
+typedef enum {ADC_CH1=0,ADC_CH2=1,ADC_CH3=2,ADC_CH4=3,ADC_CH5=4,ADC_CH6=5,ADC_CH7=6,ADC_CH8=7,
+              ADC_CH9=8,ADC_CH10=9,ADC_CH11=10,ADC_CH12=11,ADC_CH13=12,ADC_CH14=13,ADC_CH16=15,ADC_CH18=17} adc_t;
+
 class CDarwinRobot
 {
   private:
@@ -50,6 +55,13 @@ class CDarwinRobot
     void gpio_aux1_color(double red, double green, double blue);
     void gpio_aux2_color(double red, double green, double blue);
     bool is_button_pressed(pushbutton_t pushbutton);
+    // ADC interface
+    void adc_enable(void);
+    void adc_set_period(unsigned char period_ms);
+    double adc_get_value(adc_t adc);
+    double adc_get_temperature(void);
+    double adc_get_vrefint(void);
+    void adc_disable(void);
     // motion manager interface
     void mm_set_period(double period_ms);
     double mm_get_period(void);
@@ -89,8 +101,6 @@ class CDarwinRobot
     bool imu_is_gyro_detected(void);
     void imu_get_accel_data(short int *x,short int *y,short int *z);
     void imu_get_gyro_data(short int *x,short int *y,short int *z);
-    // adc interface
-    void adc_get_data(void);
     // walking interface
     void walking_set_speeds(double fw_step,double side_step,double turn);
     void walking_start(void);
diff --git a/src/examples/CMakeLists.txt b/src/examples/CMakeLists.txt
index 72a43c1..eafec7f 100644
--- a/src/examples/CMakeLists.txt
+++ b/src/examples/CMakeLists.txt
@@ -8,6 +8,11 @@ ADD_EXECUTABLE(darwin_gpio_test darwin_gpio_test.cpp)
 # link necessary libraries
 TARGET_LINK_LIBRARIES(darwin_gpio_test darwin_robot)
 
+# create an example application
+ADD_EXECUTABLE(darwin_adc_test darwin_adc_test.cpp)
+# link necessary libraries
+TARGET_LINK_LIBRARIES(darwin_adc_test darwin_robot)
+
 # create an example application
 #ADD_EXECUTABLE(darwin_joint_motion_test darwin_joint_motion_test.cpp)
 # link necessary libraries
-- 
GitLab