-
Notifications
You must be signed in to change notification settings - Fork 0
/
light_aware.h
176 lines (136 loc) · 3.36 KB
/
light_aware.h
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
* File: light_aware.h
* Author: daniele
*
* Created on 3 gennaio 2014, 16.57
*/
#ifndef LIGHT_AWARE_H
#define LIGHT_AWARE_H
#include <pthread.h>
#include <queue>
#include <miosix/_examples/context_aware/adc.h>
#include <cstdio>
#include <inttypes.h>
#include <SignalProcessingAlgorithm.h>
#include <FFTAlgorithm.h>
#include <subscribe.h>
#include "context_aware.h"
using namespace std;
using namespace miosix;
typedef Gpio<GPIOB_BASE,0> adcIn;
/**
* Enum to set if it get sample with timer or not
*/
class ADCInit {
public:
/**
* ADCInit( TIMER or NOT_TIMER)
*/
enum ADCInit_
{
NO_TIMER = 0,
TIMER = 1
};
private:
ADCInit(); //Just a wrapper class, disallow creating instances
};
/**
* LightAware is class that given an FFT algorithm, take data from ADC
* and send it to the algorithm.
*
* @param algorithm
* @param adc_init
*/
class light_aware : public ContextAware{
public:
light_aware(SignalProcessing &algorithm, ADCInit::ADCInit_ adc_init, subscribe& sub);
~light_aware();
/**
* Says if is an indoor or outdoor environment
* @return true : outdoor, false : indoor
*/
bool isOutdoor();
/**
* Says the current light intensity
* @return light intensity
*/
virtual double getMeasure();
protected:
/**
* Thread that push adc value to the queue
* @return
*/
void *pushADCValue();
/**
* Thread that reads adc samples from queue
* and send it to the algorithm
* @return
*/
void *popToFFT();
/**
* Thread that push adc value to the queue
* with the timer library
* @return
*/
void *pushADCValueWithTimer();
/**
* Helper to allow pthread for pushADCValue
* @param context
* @return
*/
static void *pushADCValueHelper(void *context)
{
return ((light_aware *)context)->pushADCValue();
}
/**
* Helper to allow pthread for pushADCValueWithTimer
* @param context
* @return
*/
static void *pushADCValueWithTimerHelper(void *context){
return ((light_aware *)context)->pushADCValueWithTimer();
}
/**
* Helper to allow pthread for popToFFT
* @param context
* @return
*/
static void *popToFFTHelper(void *context)
{
return ((light_aware *)context)->popToFFT();
}
/**
* set the value that says if we are inside or outside
* @param value
*/
void setIsOutside(bool value);
/**
* get the value that says if we are inside or outside
* @param value
*/
void getIsOutside(bool *value);
private:
/**
* Initialize ADC
* @param type
*/
void initADC(ADCInit::ADCInit_ type);
Adc adc;
typedef Gpio<GPIOB_BASE,0> adcIn;
pthread_t consumer, producer;
SignalProcessing& _algorithm;
const unsigned int SAMPLES;
const unsigned int frequency;
queue<double> Queue;
bool isOutside;
bool prec;
bool firstTime;
pthread_mutex_t mutexQueue;
pthread_mutex_t mutexIsOutside;
pthread_mutex_t mutexAVG;
pthread_cond_t cond;
double *a_samples;
double avg;
const light_aware& operator= ( const light_aware& other );
};
#endif /* LIGHT_AWARE_H */