-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbb_charge_pump.comp
85 lines (70 loc) · 2.51 KB
/
bb_charge_pump.comp
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
/********************************************************************************
bb_charge_pump.comp
userspace component
*********************************************************************************/
component bb_charge_pump "This component services the BitBang Charge Pump";
pin in bit enable "if high, start pump";
pin out bit pump.# [2] "Pump Outputs.";
option singleton yes; // makes no sense to have more than one of these components running
option userspace yes;
author "Travis Farmer";
license "GPL";
;;
#include <stdio.h> /* Standard input/output definitions */
#include <stdlib.h>
#include <stdint.h> /* Standard types */
#include <string.h> /* String function definitions */
#include <unistd.h> /* UNIX standard function definitions */
#include <errno.h> /* Error number definitions */
#include <sys/ioctl.h>
#include <inttypes.h>
#include <math.h>
#include <time.h>
unsigned long time_with_ms (void)
{
unsigned long ms; // Milliseconds
time_t s; // Seconds
struct timespec spec;
clock_gettime(CLOCK_REALTIME, &spec);
s = spec.tv_sec;
ms = (spec.tv_nsec / 1.0e6); // Convert nanoseconds to milliseconds
if (ms > 999) {
s++;
ms = 0;
}
return(ms);
}
void user_mainloop(void)
{
bool PumpState = false;
unsigned long lastTimer = 0UL;
while(true)
{
// ************************************************************************************
// IT DOESN'T MATTER IF THERE ARE NO OTHER INSTANCES ie SINGLETON, MUST USE THIS MACRO
// In this case, if you don't, no data gets written
// ************************************************************************************
FOR_ALL_INSTS()
{
if (enable == 1) {
unsigned long curTime = time_with_ms();
if (curTime - lastTimer >= 250) {
lastTimer = curTime;
if (PumpState == true) {
PumpState = false;
pump(0) = 1;
pump(1) = 0;
} else {
PumpState = true;
pump(0) = 0;
pump(1) = 1;
}
}
} else {
pump(0) = 0;
pump(1) = 0;
}
}
}
exit(0);
}