Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "c3analog.h"
- #define C3ANA_ANALASIS
- #undef C3ANA_ANALASIS
- C3Analog::C3Analog(Data *data)
- {
- this->data = data;
- adc_context = NULL;
- adcChamberPress = NULL;
- adcPumpPress = NULL;
- adc_buffer = NULL;
- if (NULL == (adc_context = iio_create_default_context()))
- {
- return;
- }
- numDevices = iio_context_get_devices_count(adc_context);
- if (0 == numDevices)
- {
- return;
- }
- if (NULL == (adc_device = iio_context_find_device(adc_context, "TI-am335x-adc")))
- {
- return;
- }
- numChannels = iio_device_get_channels_count(adc_device);
- adcChamberPress = iio_device_find_channel(adc_device, "voltage4", false);
- adcPumpPress = iio_device_find_channel(adc_device, "voltage5", false);
- adcSpdPots = iio_device_find_channel(adc_device, "voltage6", false);
- iio_channel_enable(adcChamberPress);
- iio_channel_enable(adcPumpPress);
- iio_channel_enable(adcSpdPots);
- adc_buffer = iio_device_create_buffer(adc_device, BUFFER_SIZE, false);
- // disable buffer for now, so Touch Screen works
- write_sysfs_int("enable", "/sys/bus/iio/devices/iio:device0/buffer", 0);
- if (NULL == adc_buffer)
- {
- return;
- }
- tenHz = 0;
- amuxSelect = 0;
- adcChamberPress_avg_last = 0;
- adcPumpPress_avg_last = 0;
- adcVertSpd_avg_last = 0;
- adcVacSpd_avg_last = 0;
- timer = new QTimer(this);
- connect(timer, SIGNAL(timeout()), this, SLOT(processAnalog()));
- timer->start(100); // Run timer at 0.1 second
- }
- C3Analog::~C3Analog()
- {
- if (NULL != adc_buffer)
- {
- iio_buffer_destroy(adc_buffer);
- }
- if (NULL != adcChamberPress)
- {
- iio_channel_disable(adcChamberPress);
- }
- if (NULL != adcPumpPress)
- {
- iio_channel_disable(adcPumpPress);
- }
- if (NULL != adcSpdPots)
- {
- iio_channel_disable(adcSpdPots);
- }
- if (NULL != adc_context)
- {
- iio_context_destroy(adc_context);
- }
- }
- void C3Analog::processAnalog()
- {
- tenHz++;
- switch(tenHz)
- {
- case 1:
- case 2:
- case 3:
- break;
- case 4:
- // turn the buffer/ADC sampling On
- write_sysfs_int("enable", "/sys/bus/iio/devices/iio:device0/buffer", 1);
- data->refMainScreen()->setIndicator2(true);
- break;
- case 5:
- sampleChannels(); // process the samples from the ADC
- // turn the buffer/ADC sampling Off, so Touch Screen works
- write_sysfs_int("enable", "/sys/bus/iio/devices/iio:device0/buffer", 0);
- amuxSelect = 1; // select Vertical speed pot
- // data->refGpio()->setAmux(amuxSelect); // select Vertical Speed pot
- data->refMainScreen()->setIndicator2(false);
- break;
- case 6:
- case 7:
- case 8:
- break;
- case 9:
- // data->refGpio()->setAmux(amuxSelect); // select Vertical Speed pot
- // turn the buffer/ADC sampling On
- write_sysfs_int("enable", "/sys/bus/iio/devices/iio:device0/buffer", 1);
- data->refMainScreen()->setIndicator3(true);
- break;
- case 10:
- sampleChannels(); // process the samples from the ADC
- // turn the buffer/ADC sampling Off, so Touch Screen works
- write_sysfs_int("enable", "/sys/bus/iio/devices/iio:device0/buffer", 0);
- tenHz = 0;
- if ((adcVertSpd_avg_last != adcVertSpd_avg) || (adcVacSpd_avg_last != adcVacSpd_avg))
- {
- data->refDiagValve()->setVacSpd((quint8)(adcVertSpd_avg >> 4));
- data->refDiagMicrotome()->setVertSpd((quint8)(adcVacSpd_avg >> 4));
- data->refControllerComms()->sendVacVertSpd((quint8)(adcVacSpd_avg >> 4), (quint8)(adcVertSpd_avg >> 4));
- }
- adcChamberPress_avg_last = adcChamberPress_avg;
- adcPumpPress_avg_last = adcPumpPress_avg;
- adcVertSpd_avg_last = adcVertSpd_avg;
- adcVacSpd_avg_last = adcVacSpd_avg;
- amuxSelect = 0; // select Vacuum speed pot
- // data->refGpio()->setAmux(amuxSelect); // select Vacuum Speed pot
- data->refMainScreen()->setIndicator3(false);
- break;
- default:
- tenHz = 0;
- break;
- }
- }
- void C3Analog::sampleChannels()
- {
- int numRxBytes = iio_buffer_refill(adc_buffer);
- if (0 == numRxBytes)
- {
- LOG_INFO("No samples to process\n");
- exit(-1);
- }
- int samples = 0;
- adcChamberPress_avg = 0;
- adcPumpPress_avg = 0;
- adcChan6_avg = 0;
- int buffer_inc = iio_buffer_step(adc_buffer);
- char *end = (char *)iio_buffer_end(adc_buffer);
- char *data = (char *)iio_buffer_first(adc_buffer, adcChamberPress);
- for (; data < end; data += buffer_inc)
- {
- adcChamberPress_avg += ((int16_t *)data)[0];
- adcPumpPress_avg += ((int16_t *)data)[1];
- adcChan6_avg += ((int16_t *)data)[2];
- samples++;
- }
- adcChamberPress_avg /= samples;
- adcPumpPress_avg /= samples;
- adcChan6_avg /= samples;
- if (1 == amuxSelect)
- {
- adcVacSpd_avg = adcChan6_avg;
- }
- else
- {
- adcVertSpd_avg = adcChan6_avg;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment