robert@464: /* robert@464: * AnalogInput.cpp robert@464: * robert@464: * Created on: Oct 17, 2013 robert@464: * Author: Victor Zappi robert@464: */ robert@464: robert@464: robert@464: #include "AnalogInput.h" robert@464: robert@464: using namespace std; robert@464: robert@464: robert@464: AnalogInput::AnalogInput() robert@464: { robert@464: ActivateAnalogHnd = NULL; robert@464: activateAnalogPath = ""; robert@464: analogIsSet = false; robert@464: robert@464: AnalogInHnd = NULL; robert@464: analogInPath = ""; robert@464: helperNumFound = false; robert@464: robert@464: // support var for init robert@464: // these are fixed for BBB robert@464: startPath = "/sys/devices/bone_capemgr.*/slots"; robert@464: readPath = ""; robert@464: robert@464: buffer = (char*) malloc (sizeof(char)*lSize); // reading buffer robert@464: robert@464: verbose = false; robert@464: } robert@464: robert@464: robert@464: AnalogInput::~AnalogInput() robert@464: { robert@464: free(buffer); robert@464: } robert@464: robert@464: int AnalogInput::initAnalogInputs() robert@464: { robert@464: if(analogIsSet) robert@464: { robert@464: if(verbose) robert@464: cout << "Fine, but Analog Input already active..."<< endl; robert@464: return 0; robert@464: } robert@464: robert@464: // title robert@464: if(verbose) robert@464: cout << "Init Analog Input"<< endl; robert@464: robert@464: robert@464: // first: activate analog pins on cape manager robert@464: // cape-bone-iio > /sys/devices/bone_capemgr.*/slots robert@464: robert@464: // we have to look for the semi-random number the BBB has initialized the bone_capemgr with [value of *] robert@464: // to reach /slots and set cape-bone-iio. robert@464: // to do so, we use glob lib, which translates wildcards [*] into all the values found in paths robert@464: robert@464: robert@464: glob( startPath.c_str(), 0, NULL, &globbuf); robert@464: robert@464: if(globbuf.gl_pathc >0) robert@464: { robert@464: if (globbuf.gl_pathc == 1 ) robert@464: { robert@464: activateAnalogPath = globbuf.gl_pathv[0]; robert@464: robert@464: // check if file is existing robert@464: if((ActivateAnalogHnd = fopen(activateAnalogPath.c_str(), "r+")) != NULL) robert@464: { robert@464: // we found that current capemgr num robert@464: robert@464: fwrite("cape-bone-iio", sizeof(char), 13, ActivateAnalogHnd); // activate pins robert@464: robert@464: analogIsSet = true; robert@464: robert@464: if(verbose) robert@464: cout << "Analog Pins activated via cape-bone-iio at path " << activateAnalogPath << endl; robert@464: robert@464: fclose(ActivateAnalogHnd); // close file robert@464: } robert@464: } robert@464: //else robert@464: //printf("toomany", ); robert@464: } robert@464: robert@464: globfree(&globbuf); // self freeing robert@464: robert@464: robert@464: if(!analogIsSet) robert@464: { robert@464: cout << "cannot find bone_capemgr" << endl; robert@464: cout << "------Init failed------" << endl; robert@464: return 1; robert@464: } robert@464: robert@464: robert@464: // build read path robert@464: startPath = "/sys/devices/ocp.2/helper.*"; robert@464: robert@464: glob( startPath.c_str(), 0, NULL, &globbuf); robert@464: robert@464: if(globbuf.gl_pathc >0) robert@464: { robert@464: if (globbuf.gl_pathc == 1 ) robert@464: { robert@464: analogInPath = globbuf.gl_pathv[0] + (string)"/AIN"; robert@464: } robert@464: else robert@464: cout << "Too many analog inputs with this name! [I am puzzled...]" << endl; robert@464: } robert@464: else robert@464: cout << "Cannot find analog input dir...puzzled" << endl; robert@464: robert@464: robert@464: return 0; robert@464: } robert@464: robert@464: robert@464: int AnalogInput::read(int index) robert@464: { robert@464: // convert int index into string robert@464: stringstream ss; robert@464: ss << index; robert@464: robert@464: readPath = analogInPath + ss.str(); // create pin0 file path robert@464: robert@464: robert@464: // check if file is existing robert@464: if((AnalogInHnd = fopen(readPath.c_str(), "rb")) != NULL) robert@464: { robert@464: // we found that current helper num robert@464: robert@464: // prepare read buffer to reading robert@464: fseek (AnalogInHnd , 0 , SEEK_END); robert@464: lSize = ftell (AnalogInHnd); robert@464: rewind (AnalogInHnd); robert@464: robert@464: result = fread (buffer, 1, lSize, AnalogInHnd); robert@464: robert@464: fclose(AnalogInHnd); // close file robert@464: robert@464: helperNumFound = true; robert@464: robert@464: //cout << "Analog Pins can be read at path " << analogInPath << endl; robert@464: //cout << "Test reading of Pin0 gives: " << buffer << endl; robert@464: } robert@464: robert@464: if(!helperNumFound) robert@464: { robert@464: cout << "cannot find helper" << endl; robert@464: cout << "------Analog Read failed------" << endl; robert@464: return -1; robert@464: } robert@464: robert@464: return atoi(buffer); robert@464: robert@464: } robert@464: robert@464: robert@464: