aboutsummaryrefslogtreecommitdiff
path: root/Counter/Counter.cs
blob: 416cb3b0f3c0b1e424f5031694205a75f7489c96 (plain)
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
using System;

/// <summary>
/// Descrizione di riepilogo per Counter.
/// </summary>
public class Counter:
	PureData.External
{
    int i_count,i_down,i_up;
    float step;

    public Counter(PureData.AtomList args)
	{
	    this.step = args.Count >= 3?(float)args[2]:1;
        
        float f2 = args.Count >= 2?(float)args[1]:0;
        float f1 = args.Count >= 1?(float)args[0]:0;

        if(args.Count < 2) f2 = f1;
        
        this.i_down = (int)((f1<f2)?f1:f2);
        this.i_up = (int)((f1>f2)?f1:f2);
        
	    this.i_count = this.i_down;

        AddInlet(_list,new PureData.Symbol("bound"));
        AddInlet(ref step);

        AddOutlet(_float);
        AddOutlet(_bang);
    }

	// this function MUST exist
	private static void Setup(Counter obj)
	{
	    AddMethod(0,new Method(obj.Bang));
        AddMethod(0,"reset",new Method(obj.Reset));
        AddMethod(0,"set",new MethodFloat(obj.Set));
        AddMethod(0,"bound",new MethodList(obj.Bound));
	}

    protected void Bang() 
    {
        float f = this.i_count;
        int step = (int)this.step;
        this.i_count += step;
        
        if(this.i_down-this.i_up != 0) {
            if(step > 0 && this.i_count > this.i_up) {
                this.i_count = this.i_down;
                Outlet(1);
            }
            else if(this.i_count < this.i_down) {
                this.i_count = this.i_up;
                Outlet(1);
            }
        }
        
        Outlet(0,f);
    }

    protected void Reset() 
    { 
        this.i_count = this.i_down;
    }

    protected void Set(float f) 
    { 
        this.i_count = (int)f;
    }

    protected void Bound(PureData.AtomList args) 
    { 
        float f1 = (float)args[0];
        float f2 = (float)args[1];
        
        this.i_down = (int)((f1<f2)?f1:f2);
        this.i_up = (int)((f1>f2)?f1:f2);
    }

}