~gabe/flashlight-firmware/anduril2

« back to all changes in this revision

Viewing changes to ToyKeeper/spaghetti-monster/anduril/steps.py

  • Committer: Selene Scriven
  • Date: 2019-05-24 00:00:21 UTC
  • mto: (483.1.1 fsm)
  • mto: This revision was merged to the branch mainline in revision 443.
  • Revision ID: bzr@toykeeper.net-20190524000021-2f8tp4zvfe9aas7f
added GXB172 firmware from loneoceans (tiny841 boost driver)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
"""steps.py: Calculate the stepped ramp levels used by Anduril.
 
4
Usage: steps.py floor ceiling num_steps
 
5
For example:
 
6
    > ./steps.py 1 150 3
 
7
    1: 1
 
8
    2: 75
 
9
    3: 150
 
10
"""
 
11
 
 
12
def main(args):
 
13
    floor, ceil, steps = [int(x) for x in args[:3]]
 
14
    for i in range(steps):
 
15
        guess = floor + (i * (float(ceil-floor)/(steps-1)))
 
16
        this = nearest_level(guess, floor, ceil, steps)
 
17
        #print('%i: %i (guess: %i)' % (i+1, this, guess))
 
18
        print('%i: %i' % (i+1, this))
 
19
 
 
20
 
 
21
def nearest_level(target, floor, ceil, steps):
 
22
    """Copied/adapted from anduril.c"""
 
23
    # bounds check
 
24
    # using int16_t here saves us a bunch of logic elsewhere,
 
25
    # by allowing us to correct for numbers < 0 or > 255 in one central place
 
26
    mode_min = floor;
 
27
    mode_max = ceil;
 
28
 
 
29
    if (target < mode_min): return mode_min;
 
30
    if (target > mode_max): return mode_max;
 
31
    # the rest isn't relevant for smooth ramping
 
32
    #if (! ramp_style): return target;
 
33
 
 
34
    ramp_range = ceil - floor;
 
35
    ramp_discrete_step_size = ramp_range / (steps-1);
 
36
    this_level = floor;
 
37
 
 
38
    for i in range(steps):
 
39
        this_level = floor + (i * int(ramp_range) / (steps-1));
 
40
        diff = int(target - this_level);
 
41
        if (diff < 0): diff = -diff;
 
42
        if (diff <= (ramp_discrete_step_size>>1)):
 
43
            return this_level;
 
44
 
 
45
    return this_level;
 
46
 
 
47
 
 
48
if __name__ == "__main__":
 
49
    import sys
 
50
    main(sys.argv[1:])
 
51