-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSimpleGPIO_thread.h
More file actions
executable file
·70 lines (58 loc) · 3.61 KB
/
SimpleGPIO_thread.h
File metadata and controls
executable file
·70 lines (58 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#ifndef SIMPLEGPIO_THREAD
#define SIMPLEGPIO_THREAD
#include <pulsedThread.h>
#include "GPIOlowlevel.h"
/* *********************** Forward declare functions used by thread so we can refer to them in constructors*************************/
void SimpleGPIO_Lo (void * taskData);
void SimpleGPIO_Hi (void * taskData);
int SimpleGPIO_Init (void * initDataP, void * &taskDataP);
int SimpleGPIO_setPinCallback (void * modData, taskParams * theTask);
int SimpleGPIO_setLevelCallBack (void * modData, taskParams * theTask);
void SimpleGPIO_delTask (void * taskData);
/* ******************** Initialization struct for SimpleGPIO *******************************
pin, polarity, and address base for memory mapped addresses to write to on HI and Lo */
typedef struct SimpleGPIOInitStruct{
int thePin; // pin to use for the GPIO output
int thePolarity; // polarity, 0 for low-to-high, 1 for high-to-low
volatile unsigned int * GPIOperiAddr; // base address needed when writing to registers for setting and unsetting
}SimpleGPIOInitStruct, *SimpleGPIOInitStructPtr;
/* ******************** Custom Data Struct for Simple GPIO***************************
memory mapped addresses to write to on HI and Lo, and GPIO pin bit */
typedef struct SimpleGPIOStruct{
unsigned int * GPIOperiHi; // address of register to write pin bit to on Hi
unsigned int * GPIOperiLo; // address of register to write pin bit to on Lo
unsigned int pinBit; // pin number translated to bit position in register
}SimpleGPIOStruct, *SimpleGPIOStructPtr;
/* ******************** Some simple functions to set GPIO high or lo without a thread ********************/
SimpleGPIOStructPtr newThreadlessGPIO (int thePin, int polarity); // returns a new SimpleGPIOStructPtr, ready to be used to control a single pin
void setThreadlessGPIO (SimpleGPIOStructPtr threadless, int level);
/* *********************SimpleGPIO_thread class extends pulsedThread ****************
Does pulses and trains of pulses on Raspberry Pi GPIO pins */
class SimpleGPIO_thread : public pulsedThread{
public:
/* constructors, similar to pulsedThread, one expects unsigned ints for pulse delay and duration times in microseconds and number of pulses */
SimpleGPIO_thread (int pinP, int polarityP, unsigned int delayUsecs, unsigned int durUsecs, unsigned int nPulses, void * initData, int (*initFunc)(void *, void * &), int accLevel , int &errCode) : pulsedThread (delayUsecs, durUsecs, nPulses, initData, initFunc, &SimpleGPIO_Lo, &SimpleGPIO_Hi, accLevel, errCode) {
pinNumber = pinP;
polarity = polarityP;
};
/* the other constructor expects floats for frequency, duty cycle, and train duration */
SimpleGPIO_thread (int pinP, int polarityP, float frequency, float dutyCycle, float trainDuration, void * initData, int (*initFunc)(void *, void * &), int accLevel , int &errCode): pulsedThread (frequency, dutyCycle, trainDuration, initData, initFunc, &SimpleGPIO_Lo, &SimpleGPIO_Hi, accLevel,errCode) {
pinNumber = pinP;
polarity = polarityP;
};
/* Static ThreadMakers make an initStruct and call a constructor with it, returning a pointer to a SimpleGPIO_thread */
static SimpleGPIO_thread * SimpleGPIO_threadMaker (int pin, int polarity, unsigned int delayUsecs, unsigned int durUsecs, unsigned int nPulses, int accuracyLevel);
static SimpleGPIO_thread * SimpleGPIO_threadMaker (int pin, int polarity, float frequency, float dutyCycle, float trainDuration, int accuracyLevel);
// destructor
~SimpleGPIO_thread ();
// utility functions
int setPin (int newPin, int isLocking);
int getPin (void);
int getPolarity (void);
int setLevel (int level, int isLocking);
// data members
protected:
int pinNumber;
int polarity;
};
#endif