annotate examples/10-Instruments/d-box/AnalogInput.cpp @ 556:ce391098f321 prerelease tip

THIS PROJECT HAS MOVED TO https://github.com/BelaPlatform/bela
author Giulio Moro <giuliomoro@yahoo.it>
date Sat, 25 Jun 2016 20:21:00 +0100
parents 8fcfbfb32aa0
children
rev   line source
robert@464 1 /*
robert@464 2 * AnalogInput.cpp
robert@464 3 *
robert@464 4 * Created on: Oct 17, 2013
robert@464 5 * Author: Victor Zappi
robert@464 6 */
robert@464 7
robert@464 8
robert@464 9 #include "AnalogInput.h"
robert@464 10
robert@464 11 using namespace std;
robert@464 12
robert@464 13
robert@464 14 AnalogInput::AnalogInput()
robert@464 15 {
robert@464 16 ActivateAnalogHnd = NULL;
robert@464 17 activateAnalogPath = "";
robert@464 18 analogIsSet = false;
robert@464 19
robert@464 20 AnalogInHnd = NULL;
robert@464 21 analogInPath = "";
robert@464 22 helperNumFound = false;
robert@464 23
robert@464 24 // support var for init
robert@464 25 // these are fixed for BBB
robert@464 26 startPath = "/sys/devices/bone_capemgr.*/slots";
robert@464 27 readPath = "";
robert@464 28
robert@464 29 buffer = (char*) malloc (sizeof(char)*lSize); // reading buffer
robert@464 30
robert@464 31 verbose = false;
robert@464 32 }
robert@464 33
robert@464 34
robert@464 35 AnalogInput::~AnalogInput()
robert@464 36 {
robert@464 37 free(buffer);
robert@464 38 }
robert@464 39
robert@464 40 int AnalogInput::initAnalogInputs()
robert@464 41 {
robert@464 42 if(analogIsSet)
robert@464 43 {
robert@464 44 if(verbose)
robert@464 45 cout << "Fine, but Analog Input already active..."<< endl;
robert@464 46 return 0;
robert@464 47 }
robert@464 48
robert@464 49 // title
robert@464 50 if(verbose)
robert@464 51 cout << "Init Analog Input"<< endl;
robert@464 52
robert@464 53
robert@464 54 // first: activate analog pins on cape manager
robert@464 55 // cape-bone-iio > /sys/devices/bone_capemgr.*/slots
robert@464 56
robert@464 57 // we have to look for the semi-random number the BBB has initialized the bone_capemgr with [value of *]
robert@464 58 // to reach /slots and set cape-bone-iio.
robert@464 59 // to do so, we use glob lib, which translates wildcards [*] into all the values found in paths
robert@464 60
robert@464 61
robert@464 62 glob( startPath.c_str(), 0, NULL, &globbuf);
robert@464 63
robert@464 64 if(globbuf.gl_pathc >0)
robert@464 65 {
robert@464 66 if (globbuf.gl_pathc == 1 )
robert@464 67 {
robert@464 68 activateAnalogPath = globbuf.gl_pathv[0];
robert@464 69
robert@464 70 // check if file is existing
robert@464 71 if((ActivateAnalogHnd = fopen(activateAnalogPath.c_str(), "r+")) != NULL)
robert@464 72 {
robert@464 73 // we found that current capemgr num
robert@464 74
robert@464 75 fwrite("cape-bone-iio", sizeof(char), 13, ActivateAnalogHnd); // activate pins
robert@464 76
robert@464 77 analogIsSet = true;
robert@464 78
robert@464 79 if(verbose)
robert@464 80 cout << "Analog Pins activated via cape-bone-iio at path " << activateAnalogPath << endl;
robert@464 81
robert@464 82 fclose(ActivateAnalogHnd); // close file
robert@464 83 }
robert@464 84 }
robert@464 85 //else
robert@464 86 //printf("toomany", );
robert@464 87 }
robert@464 88
robert@464 89 globfree(&globbuf); // self freeing
robert@464 90
robert@464 91
robert@464 92 if(!analogIsSet)
robert@464 93 {
robert@464 94 cout << "cannot find bone_capemgr" << endl;
robert@464 95 cout << "------Init failed------" << endl;
robert@464 96 return 1;
robert@464 97 }
robert@464 98
robert@464 99
robert@464 100 // build read path
robert@464 101 startPath = "/sys/devices/ocp.2/helper.*";
robert@464 102
robert@464 103 glob( startPath.c_str(), 0, NULL, &globbuf);
robert@464 104
robert@464 105 if(globbuf.gl_pathc >0)
robert@464 106 {
robert@464 107 if (globbuf.gl_pathc == 1 )
robert@464 108 {
robert@464 109 analogInPath = globbuf.gl_pathv[0] + (string)"/AIN";
robert@464 110 }
robert@464 111 else
robert@464 112 cout << "Too many analog inputs with this name! [I am puzzled...]" << endl;
robert@464 113 }
robert@464 114 else
robert@464 115 cout << "Cannot find analog input dir...puzzled" << endl;
robert@464 116
robert@464 117
robert@464 118 return 0;
robert@464 119 }
robert@464 120
robert@464 121
robert@464 122 int AnalogInput::read(int index)
robert@464 123 {
robert@464 124 // convert int index into string
robert@464 125 stringstream ss;
robert@464 126 ss << index;
robert@464 127
robert@464 128 readPath = analogInPath + ss.str(); // create pin0 file path
robert@464 129
robert@464 130
robert@464 131 // check if file is existing
robert@464 132 if((AnalogInHnd = fopen(readPath.c_str(), "rb")) != NULL)
robert@464 133 {
robert@464 134 // we found that current helper num
robert@464 135
robert@464 136 // prepare read buffer to reading
robert@464 137 fseek (AnalogInHnd , 0 , SEEK_END);
robert@464 138 lSize = ftell (AnalogInHnd);
robert@464 139 rewind (AnalogInHnd);
robert@464 140
robert@464 141 result = fread (buffer, 1, lSize, AnalogInHnd);
robert@464 142
robert@464 143 fclose(AnalogInHnd); // close file
robert@464 144
robert@464 145 helperNumFound = true;
robert@464 146
robert@464 147 //cout << "Analog Pins can be read at path " << analogInPath << endl;
robert@464 148 //cout << "Test reading of Pin0 gives: " << buffer << endl;
robert@464 149 }
robert@464 150
robert@464 151 if(!helperNumFound)
robert@464 152 {
robert@464 153 cout << "cannot find helper" << endl;
robert@464 154 cout << "------Analog Read failed------" << endl;
robert@464 155 return -1;
robert@464 156 }
robert@464 157
robert@464 158 return atoi(buffer);
robert@464 159
robert@464 160 }
robert@464 161
robert@464 162
robert@464 163