summaryrefslogtreecommitdiffstats
path: root/radiator-plc/stm32/app/control.c
blob: f3a35c91f17923a8921d09d281fab6a5ab5ffa1e (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
#include "project.h"


static uint32_t target = 25000;
static uint32_t value = ~0U;

#define SHORT 200
#define LONG 1200
#define REPEAT 400

#define HYST 75UL


static void
show_temp (char t, uint32_t v, int y)
{
  char buf[16];
  uint32_t i, f;

  i = v / 1000;
  f = v / 100 - (i * 10);

  if (v == ~0U)
    {
      sprintf (buf, "%c:error ", t);
    }
  else
    {
      sprintf (buf, "%c:%2d.%1d%cC", t, (int) i, (int) f, 0xdf);
    }

  lcd_write (buf, 0, y);
}

void
control_update (void)
{

  show_temp ('P', value, 0);
  show_temp ('T', target, 1);

  if (value == ~0U)
    relay_off ();
  else if (value < (target - HYST))
    relay_on ();
  else if (value > (target + HYST))
    relay_off ();


}

void
control_tick (void)
{
  static int down, up, update;

  if (!gpio_get (GPIOB, GPIO4))
    down++;
  else
    down = 0;

  if (!gpio_get (GPIOB, GPIO6))
    up++;
  else
    up = 0;

  if ((down == SHORT) || (down == LONG))
    {
      if (target > 100)
        target -= 100;
      if (down == LONG)
        down = LONG - REPEAT;
      control_update ();
    }

  if ((up == SHORT) || (up == LONG))
    {
      if (target < 99900)
        target += 100;
      if (up == LONG)
        up = LONG - REPEAT;
      control_update ();
    }

  update++;

  if (update == 1000)
    {
      control_update ();
      update = 0;
    }
}

void
control_report (uint32_t v)
{

  value = v;
  control_update ();
}

void
control_init (void)
{

  gpio_set_mode (GPIOB, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN,
                 GPIO4 | GPIO6);
  gpio_set_mode (GPIOB, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL,
                 GPIO5);
  gpio_set (GPIOB, GPIO4 | GPIO6);
  gpio_clear (GPIOB, GPIO5);


}