1 #include <pololu/orangutan>
2 #include <stdio.h>
3 #include "assert.h"
4 
5 #define abs(x) ((x)<0?(-(x)):(x))
6 
7 OrangutanAnalog analog;
8 
test_analog()9 void test_analog()
10 {
11   // test that set/get mode works
12   analog.setMode(MODE_8_BIT);
13   printf("\nGet8BIT");
14   assert(MODE_8_BIT == OrangutanAnalog::getMode());
15 
16   analog.setMode(MODE_10_BIT);
17   printf("\nGet10BIT");
18   assert(MODE_10_BIT == OrangutanAnalog::getMode());
19 
20   // read the trimpot in 10 bit mode and compare it to 8 bit mode
21   int x1 = analog.read(7);
22 
23   analog.setMode(MODE_8_BIT);
24   delay_ms(1); // required for readings to stabilize
25 
26   int x2 = OrangutanAnalog::read(7);
27 
28   printf("\n8BIT10BIT %d %d",x1,x2);
29   assert( abs((x1>>2) - x2) < 10 );
30 
31   // make sure that the average reading is more stable than individual readings
32   analog.setMode(MODE_10_BIT);
33   unsigned char i;
34   int min = 1023, max = 0, avg_min = 1023, avg_max = 0;
35 
36   for(i=0;i<10;i++)
37   {
38     int x1 = analog.read(7);
39     int x2 = analog.readAverage(7,256);
40 
41     if(x1 > max) max = x1;
42     if(x1 < min) min = x1;
43 
44     if(x2 > avg_max) avg_max = x2;
45     if(x2 < avg_min) avg_min = x2;
46 
47     printf("\nAvgComp %03x %03x", x1, x2);
48     assert( abs(x1-x2) < 10);
49   }
50 
51   printf("\nAB%03x%03x%03x%03x",max,min,avg_max,avg_min);
52   assert( max - min >= avg_max - avg_min);
53 
54   // check that temp C and F return appropriate values in 10bit mode
55   analog.setMode(MODE_10_BIT);
56   x1 = analog.readAverage(6,100);
57 
58   int expect_temp_f = ((x1 * 18 + 2) >> 2) - 40;
59   int expect_temp_c = ((expect_temp_f - 320)*5+4)/9;
60   int temp_f = analog.readTemperatureF();
61   int temp_c = analog.readTemperatureC();
62 
63   printf("\nTF10 %d %d", expect_temp_f, temp_f);
64   assert( expect_temp_f == temp_f );
65 
66   printf("\nTC10 %d %d", expect_temp_c, temp_c);
67   assert( expect_temp_c == temp_c );
68 
69   // try temp in 8bit mode
70   analog.setMode(MODE_8_BIT);
71   OrangutanTime::delayMilliseconds(1); // required for readings to stabilize?
72   temp_f = analog.readTemperatureF();
73   temp_c = analog.readTemperatureC();
74 
75   printf("\nTF8 %d %d", expect_temp_f, temp_f);
76   assert( (expect_temp_f - temp_f) <= 20 );
77 
78   printf("\nTC8 %d %d", expect_temp_c, temp_c);
79   assert( abs(expect_temp_c - temp_c) <= 20 );
80 
81   // test background conversion
82   analog.setMode(MODE_10_BIT);
83   delay_ms(1); // required for readings to stabilize
84   x1 = analog.readAverage(6,100);
85 
86   analog.startConversion(6);
87 
88   while(analog.isConverting())
89     printf("\nConvert");
90 
91   x2 = analog.conversionResult();
92   printf("%d %d", x1, x2);
93   assert( abs(x1 - x2) < 10 );
94 
95   // make sure OrangutanAnalog::toMillivolts works in 8 and 10 bit mode
96   analog.setMode(MODE_10_BIT);
97 
98   x1 = 5000;
99   x2 = OrangutanAnalog::toMillivolts(1023);
100   printf("\nmV1 %d %d",x1,x2);
101   assert( x1 == x2 );
102 
103   x1 = 2498;
104   x2 = OrangutanAnalog::toMillivolts(511);
105   printf("\nmV2 %d %d",x1,x2);
106   assert( x1 == x2 );
107 
108   x1 = 0;
109   x2 = OrangutanAnalog::toMillivolts(0);
110   printf("\nmV3 %d %d",x1,x2);
111   assert( x1 == x2 );
112 
113   analog.setMode(MODE_8_BIT);
114 
115   x1 = 5000;
116   x2 = OrangutanAnalog::toMillivolts(255);
117   printf("\nmV4 %d %d",x1,x2);
118   assert( x1 == x2 );
119 
120   x1 = 2490;
121   x2 = OrangutanAnalog::toMillivolts(127);
122   printf("\nmV5 %d %d",x1,x2);
123   assert( x1 == x2 );
124 
125   x1 = 0;
126   x2 = OrangutanAnalog::toMillivolts(0);
127   printf("\nmV6 %d %d",x1,x2);
128   assert( x1 == x2 );
129 }
130