-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathController.h
More file actions
137 lines (117 loc) · 4.18 KB
/
Controller.h
File metadata and controls
137 lines (117 loc) · 4.18 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
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
/*
* Controller.h
*
* Created on: Jan 18, 2016
* Author: sabeyruw
*/
#ifndef CONTROLLER_H_
#define CONTROLLER_H_
#if defined(ENERGIA) && !defined(EMBEDDED_MODE)
#define EMBEDDED_MODE
#endif
//
#if defined(EMBEDDED_MODE)
#include "Energia.h"
#include "pins_energia.h"
#include "Wire.h"
#endif
//
#if !defined(EMBEDDED_MODE)
#include <iostream>
#endif
//
#include "RepresentationCloneable.h"
#include "Thread.h"
/**
* This framework creates a topologically sorted graph from computational and representational units
* created by the user. Since, the framework needs to be run on a MCU, the size (in bytes) of the
* framework is minimized. At most INT_MAX nodes can be allocated within the framework.
*/
class Controller
{
private:
class ModuleEntry
{
public:
const char* threadName;
const int threadPriority;
Node* moduleNode;
ModuleEntry(const char* threadName, const int& threadPriority, Node* moduleNode) : threadName(threadName), threadPriority(threadPriority), moduleNode(moduleNode) {}
};
class RepresentationEntry
{
public:
const char* providedModuleName;
Node* representationNode;
void (*update)(Node*, Node*);
RepresentationCloneable* representationCloneable;
RepresentationEntry(const char* providedModuleName, Node* representationNode, void (*update)(Node*, Node*), RepresentationCloneable* representationCloneable) :
providedModuleName(providedModuleName), representationNode(representationNode), update(update), representationCloneable(representationCloneable) {}
};
class ModuleRepresentationEntry
{
public:
const char* requiredModuleName;
const char* requiredRepresentationName;
ModuleRepresentationEntry(const char* requiredModuleName, const char* requiredRepresentationName):
requiredModuleName(requiredModuleName), requiredRepresentationName(requiredRepresentationName) {}
};
// For topological sort
bool threadsActivated;
bool errorState;
#if defined(EMBEDDED_MODE)
typedef String MyString;
#else
typedef std::string MyString;
#endif
MyString errorMsg;
typedef Vector<ModuleEntry*> ModuleVector;
typedef Vector<RepresentationEntry*> RepresentationVector;
typedef Vector<ModuleRepresentationEntry*> ModuleRepresentationVector;
typedef Vector<Thread*> ThreadVector;
ModuleVector moduleVector;
RepresentationVector representationVector;
ModuleRepresentationVector moduleRepresentationRequiredVector;
ModuleRepresentationVector moduleRepresentationUsedVector;
ThreadVector threadVector;
void initialize(const bool& threadsActivated);
/** Computational resources */
void computeGraph();
void sort();
static void threadAllocate(Thread* thread);
static void threadUpdate(Thread* thread);
#if !defined(EMBEDDED_MODE)
static void threadTransfer(Thread* thread);
static void threadLoop(Thread* thread);
void mainLoop();
void mainThreadLoop();
#endif
void closeThreads();
void errorHandler();
void purgeEntries();
void forcedExit(const MyString& errorMsg);
protected:
Controller();
~Controller();
Controller(Controller const&);
Controller& operator=(Controller const&);
static Controller* theInstance;
public:
static Controller& getInstance();
static void deleteInstance();
void addModule(const char* threadName, const int& threadPriority, Node* theInstance);
void providedRepresentation(const char* moduleName, Node* theInstance, void (*updateRepresentation)(Node* , Node* ), RepresentationCloneable* representationCloneable);
void requiredRepresentation(const char* moduleName, const char* representationName);
void usedRepresentation(const char* moduleName, const char* representationName);
Node* getRepresentation(const char* moduleName, const char* representationName);
#if !defined(EMBEDDED_MODE)
/* Entry points for the user */
void main(const bool& threadsActivated);
#endif
/*Give control to other threads*/
void setup(unsigned long baudRate, const bool& threadsActivated = false);
void loop();
/** verbose */
void verbose();
};
#endif /* CONTROLLER_H_ */