Zoltan Hudak / mbedPi
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Thread.cpp Source File

Thread.cpp

00001 /*
00002  *  Copyright (C) 2016 Zoltan Hudak
00003  *  hudakz@outlook.com
00004  *
00005  *  Parts Copyright (C) Libelium Comunicaciones Distribuidas S.L.
00006  *  http://www.libelium.com
00007  *
00008  *  This program is free software: you can redistribute it and/or modify
00009  *  it under the terms of the GNU General Public License as published by
00010  *  the Free Software Foundation, either version 3 of the License, or
00011  *  (at your option) any later version.
00012  *
00013  *  This program is distributed in the hope that it will be useful,
00014  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  *  GNU General Public License for more details.
00017  *
00018  *  You should have received a copy of the GNU General Public License
00019  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00020  *
00021  *  Version 1.0
00022  */
00023 #include "Thread.h"
00024 /*$off*/
00025 
00026 pthread_t  idThread2;
00027 pthread_t  idThread3;
00028 pthread_t  idThread4;
00029 pthread_t  idThread5;
00030 pthread_t  idThread6;
00031 pthread_t  idThread7;
00032 pthread_t  idThread8;
00033 pthread_t  idThread9;
00034 pthread_t  idThread10;
00035 pthread_t  idThread11;
00036 pthread_t  idThread12;
00037 pthread_t  idThread13;
00038 pthread_t  idThread14;
00039 pthread_t  idThread15;
00040 pthread_t  idThread16;
00041 pthread_t  idThread17;
00042 pthread_t  idThread18;
00043 pthread_t  idThread19;
00044 pthread_t  idThread20;
00045 pthread_t  idThread21;
00046 pthread_t  idThread22;
00047 pthread_t  idThread23;
00048 pthread_t  idThread24;
00049 pthread_t  idThread25;
00050 pthread_t  idThread26;
00051 pthread_t  idThread27;
00052 
00053 pthread_t*   idThreads[26] =
00054 {
00055    &idThread2,
00056    &idThread3,
00057    &idThread4,
00058    &idThread5,
00059    &idThread6,
00060    &idThread7,
00061    &idThread8,
00062    &idThread9,
00063    &idThread10,
00064    &idThread11,
00065    &idThread12,
00066    &idThread13,
00067    &idThread14,
00068    &idThread15,
00069    &idThread16,
00070    &idThread17,
00071    &idThread18,
00072    &idThread19,
00073    &idThread20,
00074    &idThread21,
00075    &idThread22,
00076    &idThread23,
00077    &idThread24,
00078    &idThread25,
00079    &idThread26,
00080    &idThread27
00081 };
00082 
00083 /*$on*/
00084 
00085 /* This is the function that will be running in a thread if
00086  * attachInterrupt() is called */
00087 void* threadFunction(void* args)
00088 {
00089     ThreadArg*      arguments = (ThreadArg*)args;
00090     int             pin = arguments->pin;
00091 
00092     int             GPIO_FN_MAXLEN = 32;
00093     int             RDBUF_LEN = 5;
00094 
00095     char            fn[GPIO_FN_MAXLEN];
00096     int             fd, ret;
00097     struct pollfd   pfd;
00098     char            rdbuf[RDBUF_LEN];
00099 
00100     memset(rdbuf, 0x00, RDBUF_LEN);
00101     memset(fn, 0x00, GPIO_FN_MAXLEN);
00102 
00103     snprintf(fn, GPIO_FN_MAXLEN - 1, "/sys/class/gpio/gpio%d/value", pin);
00104     fd = open(fn, O_RDONLY);
00105     if (fd < 0) {
00106         perror(fn);
00107         exit(1);
00108     }
00109 
00110     pfd.fd = fd;
00111     pfd.events = POLLPRI;
00112 
00113     ret = unistd::read(fd, rdbuf, RDBUF_LEN - 1);
00114     if (ret < 0) {
00115         perror("Error reading interrupt file\n");
00116         exit(1);
00117     }
00118 
00119     while (1) {
00120         memset(rdbuf, 0x00, RDBUF_LEN);
00121         unistd::lseek(fd, 0, SEEK_SET);
00122         ret = poll(&pfd, 1, -1);
00123         if (ret < 0) {
00124             perror("Error waiting for interrupt\n");
00125             unistd::close(fd);
00126             exit(1);
00127         }
00128 
00129         if (ret == 0) {
00130             printf("Timeout\n");
00131             continue;
00132         }
00133 
00134         ret = unistd::read(fd, rdbuf, RDBUF_LEN - 1);
00135         if (ret < 0) {
00136             perror("Error reading interrupt file\n");
00137             exit(1);
00138         }
00139 
00140         //Interrupt. We call user function.
00141         arguments->func();
00142     }
00143 }
00144 
00145 /**
00146  * @brief
00147  * @note
00148  * @param
00149  * @retval
00150  */
00151 pthread_t* getThreadIdFromPin(int pin)
00152 {
00153     int i = pin - 2;
00154     if ((0 <= i) && (i <= 25))
00155         return idThreads[i];
00156     else return NULL;
00157 }