-
Notifications
You must be signed in to change notification settings - Fork 16
/
Module.cpp
85 lines (68 loc) · 2.11 KB
/
Module.cpp
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
#include "Arduino.h"
#include "defines.h"
#include "Module.h"
Module::Module()
{
// cycle is used to see if the module has already been run during the current
// interrupt execution.
cycle = 10;
output = 0;
no_output_conversion = false;
}
uint16_t Module::run(uint8_t cycle)
{
// If the module has already been run during this cycle, then return the
// output without recalculating it.
if(this->cycle == cycle) return(this->output);
this->cycle = cycle;
// Compute the module's output and store it as a instance variable
this->output = this->compute();
return(this->output);
}
uint16_t Module::readInput(Module *module, int conversion)
{
// If someone forgot to attach a module to an input, assume they mean for that
// input to be 0.
if(! module) return(0);
// When no_output_conversion is true, the actual output value of a module is
// used, instead of being mapped to 0-4095. no_output_conversion is set in
// ModuleConstant.php
if(module->no_output_conversion)
{
return(module->run(this->cycle));
}
else
{
return(module->run(this->cycle) >> conversion);
}
}
uint16_t Module::readInput(Module *module, uint32_t map_low, uint32_t map_high)
{
// If someone forgot to attach a module to an input, assume they mean for that
// input to be 0.
if(! module) return(0);
// When no_output_conversion is true, the actual output value of a module is
// used, instead of being mapped. no_output_conversion is set in ModuleConstant.php
if(module->no_output_conversion)
{
return(module->run(this->cycle));
}
else
{
//
// The output equation below is the same as..
//
// return(map(module->run(this->cycle), 0, 4096, map_low, map_high));
//
// But since dividing by 4096 can be done a lot faster via bitshifting,
// this is a more efficient algorithm. When tested against a call to
// map(..), it's about 2x as fast.
//
return((((module->run(this->cycle) * (map_high - map_low)) >> 12) + map_low));
}
}
uint16_t Module::readInput(Module *module)
{
if(! module) return(0);
return(module->run(this->cycle));
}