CheeseBoard
A library for use with the CheeseBoard Cheddar platform
CbHC12.cpp
1 #include <MutilaDebug.h>
2 #include <Millis.h>
3 #include <CheeseboardConfig.h>
4 #include "CbHC12.h"
5 
6 CbHC12Class CbHC12(HC12_RX_PIN, HC12_TX_PIN, HC12_SET_PIN);
7 
8 CbHC12Class::CbHC12Class(uint8_t rxPin, uint8_t txPin, uint8_t setPin) :
9  SoftwareSerial(rxPin, txPin),
10  _setPin(setPin)
11 {
12 }
13 
14 void CbHC12Class::begin(long speed)
15 {
16  SoftwareSerial::begin(speed);
17  pinMode(_setPin, OUTPUT);
18  setCommandMode(false, true);
19 }
20 
21 void CbHC12Class::setCommandMode(bool switchOn, bool force)
22 {
23  // Note: set pin's logic is inverted = set to:
24  // LOW for set mode
25  // HIGH for transparent mode
26  if (switchOn && (!_cmdMode || force)) {
27  _cmdMode = true;
28  digitalWrite(_setPin, !_cmdMode);
29  delay(40);
30  }
31  else if (!switchOn && (_cmdMode || force)) {
32  _cmdMode = false;
33  digitalWrite(_setPin, !_cmdMode);
34  delay(80);
35  }
36  DB(F("CbHC12Class::setCommandMode _cmdMode="));
37  DBLN(_cmdMode);
38 }
39 
41 {
42  bool oldCmdMode = _cmdMode;
43  setCommandMode(true);
44  const char expect[] = "OK\r\n";
45  uint8_t idx = 0;
46  unsigned long start = Millis();
47  println("AT");
48  while (Millis() - start < CheckTimeoutMs && idx < 4) {
49  if (available()) {
50  if (expect[idx] == read()) {
51  idx++;
52  } else {
53  break;
54  }
55  }
56  }
57  setCommandMode(oldCmdMode);
58  return idx == 4;
59 }
60 
61 
CbHC12Class(uint8_t rxPin, uint8_t txPin, uint8_t setPin)
Definition: CbHC12.cpp:8
void setCommandMode(bool switchOn, bool force=false)
Definition: CbHC12.cpp:21
HC12 Wireless Serial class.
Definition: CbHC12.h:16
bool check()
Definition: CbHC12.cpp:40
void begin(long speed)
Definition: CbHC12.cpp:14