-
Notifications
You must be signed in to change notification settings - Fork 0
/
PowerCabinet.c
224 lines (168 loc) · 5.72 KB
/
PowerCabinet.c
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#define G_LOG_USE_STRUCTURED
#include <stdio.h>
#include <gtk/gtk.h>
#include "Wiring.h"
#include "PowerCabinet.h"
#include "fsm.h"
static gboolean SuppliesOn = FALSE;
struct fsm PowerFSM;
// Power switching event handlers
static int doBatOnPress(__attribute__((unused)) int state,
__attribute__((unused)) int event,
__attribute__((unused)) void *data)
{
PowerFSM.nextEvent = CHARGER_CONNECTED;
return -1;
}
static int doBatOffPress(__attribute__((unused)) int state,
__attribute__((unused)) int event,
__attribute__((unused)) void *data)
{
PowerFSM.nextEvent = CHARGER_DISCONNECTED;
return -1;
}
static int doCompOnPress(__attribute__((unused)) int state,
__attribute__((unused)) int event,
__attribute__((unused)) void *data)
{
//wiring(SUPPLIES_ON,1);
PowerFSM.nextEvent = SUPPLIES_ON;
return -1;
}
static int doCompOffPress(__attribute__((unused)) int state,
__attribute__((unused)) int event,
__attribute__((unused)) void *data)
{
//wiring(SUPPLIES_OFF,0);
PowerFSM.nextEvent = SUPPLIES_OFF;
return -1;
}
static int doSuppliesOn(__attribute__((unused)) int state,
__attribute__((unused)) int event,
__attribute__((unused)) void *data)
{
wiring(SUPPLIES_ON,1);
SuppliesOn = TRUE;
//PowerFSM.nextEvent = SUPPLIES_ON;
return -1;
}
static int doSuppliesOff(int state,
__attribute__((unused)) int event,
__attribute__((unused)) void *data)
{
wiring(SUPPLIES_OFF,0);
SuppliesOn = FALSE;
if((state == 11) || (state == 13))
PowerFSM.nextEvent = CHARGER_DISCONNECTED;
return -1;
}
static int doChrgrConnect(__attribute__((unused)) int state,
__attribute__((unused)) int event,
__attribute__((unused)) void *data)
{
wiring(CHARGER_CONNECTED,1);
return -1;
}
static int doChrgrDisconnect(int state,
__attribute__((unused)) int event,
__attribute__((unused)) void *data)
{
wiring(CHARGER_DISCONNECTED,0);
if(state == 8)
PowerFSM.nextEvent = SUPPLIES_OFF;
return -1;
}
static int doMainsOff(__attribute__((unused)) int state,
__attribute__((unused)) int event,
__attribute__((unused)) void *data)
{
PowerFSM.nextEvent = CHARGER_DISCONNECTED ;
return -1;
}
// Power switching sequencer FSM
struct fsmtable PowerTable[] = {
{ 0, MAINS_SUPPLY_ON, 1, NULL }, // Should start fan noise
{ 1, MAINS_SUPPLY_OFF, 0, NULL }, // Should stop fan noise
{ 1, BATTERY_ON_PRESSED, 2, doBatOnPress },
{ 2, CHARGER_CONNECTED, 3, doChrgrConnect }, // Set charger op to Vbat
{ 3, COMPUTER_ON_PRESSED, 4, doCompOnPress },
{ 3, BATTERY_OFF_PRESSED, 7, doBatOffPress },
{ 3, MAINS_SUPPLY_OFF, 10, doMainsOff },
{ 4, SUPPLIES_ON, 5, doSuppliesOn },
{ 5, COMPUTER_OFF_PRESSED, 6, doCompOffPress },
{ 5, BATTERY_OFF_PRESSED, 8, doBatOffPress },
{ 5, MAINS_SUPPLY_OFF, 11, NULL },
{ 6, SUPPLIES_OFF, 3, doSuppliesOff },
{ 7, CHARGER_DISCONNECTED, 1, doChrgrDisconnect },
{ 8, CHARGER_DISCONNECTED, 9, doChrgrDisconnect },
{ 9, SUPPLIES_OFF, 1, doSuppliesOff },
{ 10, CHARGER_DISCONNECTED, 0, doChrgrDisconnect },
{ 11, MAINS_SUPPLY_ON, 5, NULL },
{ 11, SUPPLIES_OFF, 12, doSuppliesOff },
{ 11, COMPUTER_OFF_PRESSED, 13, doCompOffPress },
{ 12, CHARGER_DISCONNECTED, 0, doChrgrDisconnect },
{ 13, SUPPLIES_OFF, 12, doSuppliesOff },
{-1, -1, -1, NULL }
};
struct fsm PowerFSM = { "Power FSM", 0, PowerTable,NULL,NULL,0,-1};
// Translate wire state changes into FSM events.
static void PowerCabinetMainsOn(__attribute__((unused)) unsigned int dummy)
{
//printf("%s called\n",__FUNCTION__);
doFSM(&PowerFSM,MAINS_SUPPLY_ON,NULL);
}
static void PowerCabinetMainsOff(__attribute__((unused)) unsigned int dummy)
{
//printf("%s called\n",__FUNCTION__);
doFSM(&PowerFSM,MAINS_SUPPLY_OFF,NULL);
}
static void PowerCabinetComputerOn(unsigned int value)
{
//printf("%s called value = %d\n",__FUNCTION__,value);
if(value != 0)
doFSM(&PowerFSM,COMPUTER_ON_PRESSED,NULL);
}
static void PowerCabinetComputerOff(unsigned int value)
{
//printf("%s called value = %d\n",__FUNCTION__,value);
if(value != 0)
doFSM(&PowerFSM,COMPUTER_OFF_PRESSED,NULL);
}
static void PowerCabinetBatteryOn(unsigned int value)
{
//printf("%s called value = %d\n",__FUNCTION__,value);
if(value != 0)
doFSM(&PowerFSM,BATTERY_ON_PRESSED,NULL);
}
static void PowerCabinetBatteryOff(unsigned int value)
{
//printf("%s called value = %d\n",__FUNCTION__,value);
if(value != 0)
doFSM(&PowerFSM,BATTERY_OFF_PRESSED,NULL);
}
extern double Vbat;
static void checkVbat(__attribute__((unused)) unsigned int value)
{
if(SuppliesOn && (Vbat < 20.0))
{
doFSM(&PowerFSM,SUPPLIES_OFF,NULL);
}
}
// Wire up the power cabinet !
void PowerCabinetInit(__attribute__((unused)) GtkBuilder *builder,
__attribute__((unused)) GString *sharedPath,
__attribute__((unused)) GString *userPath)
{
//printf("%s called\n",__FUNCTION__);
connectWires(MAINS_SUPPLY_ON,PowerCabinetMainsOn);
connectWires(MAINS_SUPPLY_OFF,PowerCabinetMainsOff);
connectWires(COMPUTER_ON_PRESSED,PowerCabinetComputerOn);
connectWires(COMPUTER_OFF_PRESSED,PowerCabinetComputerOff);
connectWires(BATTERY_ON_PRESSED,PowerCabinetBatteryOn);
connectWires(BATTERY_OFF_PRESSED,PowerCabinetBatteryOff);
connectWires(TIMER100HZ,checkVbat);
}
void PowerCabinetTidy(__attribute__((unused)) GString *userPath)
{
//g_info("%s called\n",__FUNCTION__);
}