Guest User

Untitled

a guest
Jul 31st, 2015
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.22 KB | None | 0 0
  1. #include "c3analog.h"
  2.  
  3. #define C3ANA_ANALASIS
  4. #undef C3ANA_ANALASIS
  5.  
  6. C3Analog::C3Analog(Data *data)
  7. {
  8. this->data = data;
  9.  
  10. adc_context = NULL;
  11. adcChamberPress = NULL;
  12. adcPumpPress = NULL;
  13. adc_buffer = NULL;
  14.  
  15. if (NULL == (adc_context = iio_create_default_context()))
  16. {
  17. return;
  18. }
  19. numDevices = iio_context_get_devices_count(adc_context);
  20.  
  21. if (0 == numDevices)
  22. {
  23. return;
  24. }
  25.  
  26. if (NULL == (adc_device = iio_context_find_device(adc_context, "TI-am335x-adc")))
  27. {
  28. return;
  29. }
  30.  
  31. numChannels = iio_device_get_channels_count(adc_device);
  32.  
  33. adcChamberPress = iio_device_find_channel(adc_device, "voltage4", false);
  34. adcPumpPress = iio_device_find_channel(adc_device, "voltage5", false);
  35. adcSpdPots = iio_device_find_channel(adc_device, "voltage6", false);
  36.  
  37.  
  38. iio_channel_enable(adcChamberPress);
  39. iio_channel_enable(adcPumpPress);
  40. iio_channel_enable(adcSpdPots);
  41.  
  42. adc_buffer = iio_device_create_buffer(adc_device, BUFFER_SIZE, false);
  43.  
  44. // disable buffer for now, so Touch Screen works
  45. write_sysfs_int("enable", "/sys/bus/iio/devices/iio:device0/buffer", 0);
  46.  
  47. if (NULL == adc_buffer)
  48. {
  49. return;
  50. }
  51.  
  52. tenHz = 0;
  53.  
  54. amuxSelect = 0;
  55.  
  56. adcChamberPress_avg_last = 0;
  57. adcPumpPress_avg_last = 0;
  58. adcVertSpd_avg_last = 0;
  59. adcVacSpd_avg_last = 0;
  60.  
  61. timer = new QTimer(this);
  62.  
  63. connect(timer, SIGNAL(timeout()), this, SLOT(processAnalog()));
  64.  
  65. timer->start(100); // Run timer at 0.1 second
  66. }
  67.  
  68. C3Analog::~C3Analog()
  69. {
  70. if (NULL != adc_buffer)
  71. {
  72. iio_buffer_destroy(adc_buffer);
  73. }
  74.  
  75. if (NULL != adcChamberPress)
  76. {
  77. iio_channel_disable(adcChamberPress);
  78. }
  79.  
  80. if (NULL != adcPumpPress)
  81. {
  82. iio_channel_disable(adcPumpPress);
  83. }
  84.  
  85. if (NULL != adcSpdPots)
  86. {
  87. iio_channel_disable(adcSpdPots);
  88. }
  89.  
  90. if (NULL != adc_context)
  91. {
  92. iio_context_destroy(adc_context);
  93. }
  94. }
  95.  
  96. void C3Analog::processAnalog()
  97. {
  98. tenHz++;
  99.  
  100. switch(tenHz)
  101. {
  102. case 1:
  103. case 2:
  104. case 3:
  105. break;
  106.  
  107. case 4:
  108. // turn the buffer/ADC sampling On
  109. write_sysfs_int("enable", "/sys/bus/iio/devices/iio:device0/buffer", 1);
  110.  
  111. data->refMainScreen()->setIndicator2(true);
  112. break;
  113.  
  114. case 5:
  115. sampleChannels(); // process the samples from the ADC
  116.  
  117. // turn the buffer/ADC sampling Off, so Touch Screen works
  118. write_sysfs_int("enable", "/sys/bus/iio/devices/iio:device0/buffer", 0);
  119.  
  120. amuxSelect = 1; // select Vertical speed pot
  121. // data->refGpio()->setAmux(amuxSelect); // select Vertical Speed pot
  122.  
  123. data->refMainScreen()->setIndicator2(false);
  124. break;
  125.  
  126. case 6:
  127. case 7:
  128. case 8:
  129. break;
  130.  
  131. case 9:
  132. // data->refGpio()->setAmux(amuxSelect); // select Vertical Speed pot
  133. // turn the buffer/ADC sampling On
  134. write_sysfs_int("enable", "/sys/bus/iio/devices/iio:device0/buffer", 1);
  135.  
  136. data->refMainScreen()->setIndicator3(true);
  137. break;
  138.  
  139. case 10:
  140. sampleChannels(); // process the samples from the ADC
  141.  
  142. // turn the buffer/ADC sampling Off, so Touch Screen works
  143. write_sysfs_int("enable", "/sys/bus/iio/devices/iio:device0/buffer", 0);
  144.  
  145. tenHz = 0;
  146.  
  147. if ((adcVertSpd_avg_last != adcVertSpd_avg) || (adcVacSpd_avg_last != adcVacSpd_avg))
  148. {
  149. data->refDiagValve()->setVacSpd((quint8)(adcVertSpd_avg >> 4));
  150. data->refDiagMicrotome()->setVertSpd((quint8)(adcVacSpd_avg >> 4));
  151.  
  152. data->refControllerComms()->sendVacVertSpd((quint8)(adcVacSpd_avg >> 4), (quint8)(adcVertSpd_avg >> 4));
  153. }
  154.  
  155. adcChamberPress_avg_last = adcChamberPress_avg;
  156. adcPumpPress_avg_last = adcPumpPress_avg;
  157. adcVertSpd_avg_last = adcVertSpd_avg;
  158. adcVacSpd_avg_last = adcVacSpd_avg;
  159. amuxSelect = 0; // select Vacuum speed pot
  160. // data->refGpio()->setAmux(amuxSelect); // select Vacuum Speed pot
  161.  
  162. data->refMainScreen()->setIndicator3(false);
  163. break;
  164.  
  165. default:
  166. tenHz = 0;
  167. break;
  168. }
  169. }
  170.  
  171. void C3Analog::sampleChannels()
  172. {
  173. int numRxBytes = iio_buffer_refill(adc_buffer);
  174.  
  175. if (0 == numRxBytes)
  176. {
  177. LOG_INFO("No samples to process\n");
  178.  
  179. exit(-1);
  180. }
  181.  
  182. int samples = 0;
  183.  
  184. adcChamberPress_avg = 0;
  185. adcPumpPress_avg = 0;
  186. adcChan6_avg = 0;
  187.  
  188. int buffer_inc = iio_buffer_step(adc_buffer);
  189. char *end = (char *)iio_buffer_end(adc_buffer);
  190. char *data = (char *)iio_buffer_first(adc_buffer, adcChamberPress);
  191.  
  192. for (; data < end; data += buffer_inc)
  193. {
  194. adcChamberPress_avg += ((int16_t *)data)[0];
  195. adcPumpPress_avg += ((int16_t *)data)[1];
  196. adcChan6_avg += ((int16_t *)data)[2];
  197.  
  198. samples++;
  199. }
  200.  
  201. adcChamberPress_avg /= samples;
  202. adcPumpPress_avg /= samples;
  203. adcChan6_avg /= samples;
  204.  
  205. if (1 == amuxSelect)
  206. {
  207. adcVacSpd_avg = adcChan6_avg;
  208. }
  209. else
  210. {
  211. adcVertSpd_avg = adcChan6_avg;
  212. }
  213.  
  214. }
Advertisement
Add Comment
Please, Sign In to add comment