Diseño procedimental¶
En este apartado se referencian los algoritmos implementados en el sistema que son considerados de mayor relevancia.
Detección de la versión de la placa, para adaptar el número de pines ofrecidos al valor físico.
#include "board_revision.hpp"
#define CPUINFO_PATH "/proc/cpuinfo"
#define PI_VERSION_1 1
#define PI_VERSION_2 2
int revision(){
std::ifstream fcpuinfo(CPUINFO_PATH);
if(fcpuinfo.fail()){
return 3;
}
std::string aux_str;
std::string revision_str("Revision");
while(std::getline(fcpuinfo, aux_str)){
if(revision_str.compare(0, revision_str.length(), aux_str.substr(0, revision_str.length())) == 0){
if((aux_str.compare(aux_str.length() - 2, aux_str.length() -1, "2") == 0) || (aux_str.compare(aux_str.length() - 2, aux_str.length() - 1, "3") == 0)){
return 1;
}
return 2;
}
}
return 0;
}
Apertura de un pin mediante una llamada al comando gpio-admin
.
int gpio_admin(char * subcommand, int pin, char* pull){
char command[MAX_LEN];
snprintf(command, MAX_LEN, "gpio-admin %s %d\n", subcommand, pin);//, pull == NULL ? "" : pull);
FILE* f = popen(command, "r");
return pclose(f);
}
int Pin::open(){
gpio_admin("export", this->soc_pin_number, this->pull);
if(NULL == (this->file = fopen(this->pin_path("value"), "r+"))){
quick2wire_errno = PIN_ERR;
return 1;
}
return this->write("direction", this->direction);
/*if self._direction == In:
self._write("edge", self._interrupt if self._interrupt is not None else "none")
*/
}
Modificación del valor de un pin.
int Pin::set(int value){
if(this->closed()){
perror("The device is closed");
return -1;
}
if(strcmp(OUT, "out") != 0){
perror("The direction is not \"out\"");
return -2;
}
fseek(this->file, 0, SEEK_SET);
char buff[3];
snprintf(buff, 3, "%d", value);
fputs(buff, this->file);
fflush(this->file);
return value;
}