-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathiInterruptable.h
102 lines (90 loc) · 3.28 KB
/
iInterruptable.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
/*
* iInterruptable.h
*
* Created on: 23 авг. 2016 г.
* Author: tihonov
*/
#ifndef INTERRUPT_IINTERRUPTABLE_H_
#define INTERRUPT_IINTERRUPTABLE_H_
#include "interruptmanager.h"
#include <array>
//******************************************************************************************
//Interface provides interrupt functional to any class
//******************************************************************************************
class iInterruptable
{
public:
iInterruptable(){};
virtual ~iInterruptable(){};
virtual void interruptHandle(){};
};
//******************************************************************************************
//Static template class of interrupt
//use case
//InterruptSubject<TIM4_IRQn,6> timIrqSubject;
//timIrqSubject.init();
//DebugString debugstring1("debug1"); // class extends interface iInterruptable
//DebugString debugstring2("debug2");
//timIrqSubject.AttachInterrupt(&debugstring1);
//timIrqSubject.AttachInterrupt(&debugstring2);
//******************************************************************************************
template <IRQn_Type irqN, uint8_t maxInterrupts>
class InterruptSubject{
private:
static void SetVector(){ INTERRUPTMANAGER::addHandler(interruptSubjectHandle, irqN); };
static std::array <iInterruptable*, maxInterrupts> ArrayOfInterruptableClasses;
public:
InterruptSubject(){}
static void interruptSubjectHandle();
static void init(){ SetVector(); };
static void AttachInterrupt(iInterruptable * pInterrupt);
static void DetatchInterrupt(iInterruptable * pInterrupt);
};
//******************************************************************************************
//Template definitions of static variables
//******************************************************************************************
template <IRQn_Type irqN, uint8_t maxInterrupts>
std::array<iInterruptable*, maxInterrupts> InterruptSubject<irqN, maxInterrupts>::ArrayOfInterruptableClasses = { NULL };
//******************************************************************************************
//Template definitions of public functions
//******************************************************************************************
template <IRQn_Type irqN, uint8_t maxInterrupts>
void InterruptSubject<irqN, maxInterrupts>::interruptSubjectHandle()
{
for (size_t i = 0; i < ArrayOfInterruptableClasses.max_size(); i++){
if (ArrayOfInterruptableClasses[i] != NULL) ArrayOfInterruptableClasses[i]->interruptHandle();
};
}
template <IRQn_Type irqN, uint8_t maxInterrupts>
void InterruptSubject<irqN, maxInterrupts>::AttachInterrupt(iInterruptable * pInterrupt)
{
uint8_t i = 0;
bool attached = false;
while (!attached)
{
if (ArrayOfInterruptableClasses[i] == NULL)
{
ArrayOfInterruptableClasses[i] = pInterrupt;
attached = true;
}
i++;
if (i == ArrayOfInterruptableClasses.max_size()) attached = true;
}
}
template <IRQn_Type irqN, uint8_t maxInterrupts>
void InterruptSubject<irqN, maxInterrupts>::DetatchInterrupt(iInterruptable * pInterrupt)
{
uint8_t i = 0;
bool detached = false;
while (!detached)
{
if (ArrayOfInterruptableClasses[i] == pInterrupt)
{
ArrayOfInterruptableClasses[i] = NULL;
detached = true;
}
i++;
if (i == ArrayOfInterruptableClasses.max_size()) detached = true;
}
}
#endif /* INTERRUPT_IINTERRUPTABLE_H_ */