summaryrefslogtreecommitdiffstats
path: root/app/gauge.c
blob: c71d8d9d9bff1ac1d3a8b717557e324dabd5b64b (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
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
#include "project.h"

#define P1_N GPIO12
#define P1_N_PORT GPIOB
#define P1_P GPIO13
#define P1_P_PORT GPIOB
#define P2_N GPIO14
#define P2_N_PORT GPIOB
#define P2_P GPIO15
#define P2_P_PORT GPIOB


unsigned gauge_target;
static unsigned gauge_current;

#define GUAGE_N_STATE 6
static unsigned gauge_state;


static inline unsigned
ccw (unsigned v)
{
  v += GUAGE_N_STATE - 1;

  if (v >= GUAGE_N_STATE)
    v -= GUAGE_N_STATE;

  return v;
}


static inline unsigned
cw (unsigned v)
{
  v += 1;

  if (v >= GUAGE_N_STATE)
    v -= GUAGE_N_STATE;

  return v;
}

static inline void
p1 (int i)
{
  if (i < 0) {
    SET (P1_N);
    CLEAR (P1_P);
  } else if (i > 0) {
    CLEAR (P1_N);
    SET (P1_P);
  } else {
    CLEAR (P1_N);
    CLEAR (P1_P);
  }
}

static inline void
p2 (int i)
{
  if (i < 0) {
    SET (P2_N);
    CLEAR (P2_P);
  } else if (i > 0) {
    CLEAR (P2_N);
    SET (P2_P);
  } else {
    CLEAR (P2_N);
    CLEAR (P2_P);
  }
}



void
gauge_ticker (void)
{

  if (gauge_current < gauge_target) {
    gauge_state = cw (gauge_state);
    gauge_current++;
  } else if (gauge_current > gauge_target) {
    gauge_state = ccw (gauge_state);
    gauge_current--;
  }

  switch (gauge_state) {
  case 0:
    p1 (1);
    p2 (1);
    break;

  case 1:
    p1 (1);
    p2 (0);
    break;

  case 2:
    p1 (0);
    p2 (-1);
    break;

  case 3:
    p1 (-1);
    p2 (-1);
    break;

  case 4:
    p1 (-1);
    p2 (0);
    break;

  case 5:
    p1 (0);
    p2 (1);
  }

}

void
gauge_test (void)
{
  char buf[8];
  uint32_t now = dwt_read_cycle_counter();  /*This wraps every 59.9 seconds or so */

  now &= 0x3fffffff;            /* now wraps every 15s */
  now = now / (0x3fffffff / 810);

  gauge_target = now;

  sprintf (buf, "%3d", gauge_target);
  font8x8_put_str (vram_1, buf, 90, 24);
}




void
gauge_init (void)
{
  MAP_OUTPUT_PP (P1_N);
  MAP_OUTPUT_PP (P1_P);
  MAP_OUTPUT_PP (P2_N);
  MAP_OUTPUT_PP (P2_P);
}