summaryrefslogtreecommitdiffstats
path: root/martin/door/src/hcs200.cpp
blob: 661380a55cdc3da3d8c627dea2fe4c8e7ccb01e9 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include <Arduino.h>

#include "hcs200.h"

/** Pulse width lengths used by the transmitter.  A pulse width is the time
 * between a transition from 0 to 1 or from 1 to 0 of the input pin.
 */
enum RbType
{
  RB_SHORT = 0, // pulse_width
  RB_LONG,      // 2 * pulse_width
  RB_ERROR
};

/** Determine if 'pulse' is a short pulse, RB_SHORT or a long one, RB_LONG.
 *  Normally, a short pulse is the same as the 'tx_clock' and a long pulse is
 *  twice as long.  However, timing problems with servicing interrupts means
 *  that we need to add some fudge factors.
 */
int HCS200::classify(unsigned pulse)
{
  int d = pulse - tx_clock;
  if (d < -100)
    return RB_ERROR;
  else if (d < 100)
    return RB_SHORT;
  else
  {
    d -= tx_clock;
    if (d < -100)
      return RB_ERROR;
    else if (d < 100)
      return RB_LONG;
    else
      return RB_ERROR;
  }
}

#define IN_RANGE(x, min, max) (x >= min && x <= max)
void HCS200::on_isr(uint8_t value)
{
  unsigned long timestamp = micros();
  unsigned long pulse_width = timestamp - last_timestamp;
  int d;

  switch (rx_state) {
    case RS_PREAMBLE_START:
      // value == 0
      preamble_count = 1;
      rx_state = RS_PREAMBLE_LOW;
      tx_clock = pulse_width;
      break;
    case RS_PREAMBLE_LOW:
      // value == 1
      preamble_count++;
      d = pulse_width - tx_clock;
      rx_state = (IN_RANGE(d, -100, 100)) ? RS_PREAMBLE_HIGH : RS_NOSYNC;
      break;
    case RS_PREAMBLE_HIGH:
      // value == 0
      preamble_count++;
      d = pulse_width - tx_clock;
      if (IN_RANGE(d, -100, 100))
        rx_state = (preamble_count == 23) ? RS_PREAMBLE_HEADER : RS_PREAMBLE_LOW;
      else
        rx_state = RS_NOSYNC;
      break;
    case RS_PREAMBLE_HEADER:
      // header should be low for 10*tx_clock
      d = pulse_width - 10 * tx_clock;
      if (value == 1 && IN_RANGE(d, -100, 100))
      {
        rx_state = RS_DATA;
        rx_bit_count = 0;
        memset(rx_buf, 0, sizeof(rx_buf));
      }
      else
        rx_state = RS_NOSYNC;
      break;
    case RS_DATA:
      if (value == 1)
      {
        int first = classify(last_pulse_width);
        int second = classify(pulse_width);
        // received a 1 bit
        if (first == RB_SHORT && second == RB_LONG)
        {
          int idx = rx_bit_count / 32;
          rx_buf[idx] >>= 1;
          rx_buf[idx] |= 0x80000000;
          rx_bit_count++;
        }
        // received a 0 bit
        else if (first == RB_LONG && second == RB_SHORT)
        {
          int idx = rx_bit_count / 32;
          rx_buf[idx] >>= 1;
          rx_bit_count++;
        }
        else
          rx_state = RS_NOSYNC;

        // we ignore the last bit as it's always "1"
        // instead we use the raising edge as trigger to stop
        if (rx_bit_count == MAX_BITS - 1)
          rx_state = RS_COMPLETED;
      }
      break;
  }

  // check outside of the state machine
  // this is important as otherwise otherwise we always miss the start
  if (rx_state == RS_NOSYNC && value == 1)
    rx_state = RS_PREAMBLE_START;

  last_timestamp = timestamp;
  last_pulse_width = pulse_width;
}

void HCS200::reset()
{
  rx_state = RS_NOSYNC;
}

bool HCS200::decode(HCS200_Keycode &out)
{
  if (rx_state != RS_COMPLETED)
    return false;
  out.encrypted = rx_buf[0];
  out.serial    = rx_buf[1] & 0x0FFFFFFF;
  out.buttons   = (rx_buf[1] >> 28) & 0xF;
  out.lowbat    = rx_buf[2] & 0x80000000;
  return true;
}

void HCS200::print_state(Print &stream)
{
  stream.print("rx_state=");
  stream.print(rx_state, DEC);
  stream.print(", rx_bit_count=");
  Serial.print(rx_bit_count, DEC);
  stream.print(", tx_clock=");
  stream.print(tx_clock, DEC);
  stream.print(", preamble_count=");
  stream.println(preamble_count, DEC);
}

void HCS200_Keycode::print(Print &stream)
{
  stream.print("Keyfob# ");
  stream.print(serial, HEX);
  stream.print(", buttons:");
  if (buttons & BM_S0)
    stream.print(" 1");
  if (buttons & BM_S1)
    stream.print(" 2");
  if (buttons & BM_S2)
    stream.print(" 3");
  if (buttons & BM_S3)
    stream.print(" 4");
  stream.print(", lowbat=");
  stream.print(lowbat, DEC);
  stream.print(", code=");
  stream.print(encrypted, HEX);
  stream.print("\n");
}

#define PULSE_WIDTH 440

inline void HCS200_Keycode::send(bool value, std::function<void(int)> setOutput)
{
  if (!value)
  {
    setOutput(HIGH);
    delayMicroseconds(PULSE_WIDTH);
    delayMicroseconds(PULSE_WIDTH);
    setOutput(LOW);
    delayMicroseconds(PULSE_WIDTH);
  }
  else
  {
    setOutput(HIGH);
    delayMicroseconds(PULSE_WIDTH);
    setOutput(LOW);
    delayMicroseconds(PULSE_WIDTH);
    delayMicroseconds(PULSE_WIDTH);
  }
}

void HCS200_Keycode::send(std::function<void(int)> setOutput)
{
  uint32_t val;

  // preamble
  for(unsigned short i = 0; i < 11; i++)
  {
    setOutput(HIGH);
    delayMicroseconds(PULSE_WIDTH);
    setOutput(LOW);
    delayMicroseconds(PULSE_WIDTH);
  }
  setOutput(HIGH);
  delayMicroseconds(PULSE_WIDTH);

  // header
  setOutput(LOW);
  delayMicroseconds(PULSE_WIDTH * 10);

  // encrypted
  val = this->encrypted;
  for(unsigned short i = 0; i < 32; i++)
  {
    send(val & 0x01, setOutput);
    val >>= 1;
  }

  // serial
  val = this->serial;
  for(unsigned short i = 0; i < 28; i++)
  {
    send(val & 0x01, setOutput);
    val >>= 1;
  }

  // buttons
  val = this->buttons;
  for(unsigned short i = 0; i < 4; i++)
  {
    send(val & 0x01, setOutput);
    val >>= 1;
  }

  // lowbat
  send(this->lowbat, setOutput);

  // RPT
  send(1, setOutput);

  // guard time
  setOutput(LOW);
  delayMicroseconds(PULSE_WIDTH * 39);
}