-
Notifications
You must be signed in to change notification settings - Fork 13
/
Manager.h
127 lines (102 loc) · 3.58 KB
/
Manager.h
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
/*
* Manager.h
*
* Created on: 26 Sep 2012
* Author: jack
*
* This class runs the show. It is responsible for:
*
* - Polling TRXs in sequence (a "roll call")
* - retrying MAX_RETRIES times if no response is received
* - ensure we only do one roll call per SAMPLE_PERIOD
*
* - Listening for TXs.
* - We try to learn when TXs are expected to arrive so we can pause
* TRX polling for CC_TX_WINDOW milliseconds to minimise
* the chances of an RF collision.
*
* - Listening for commands from the serial port.
*
* THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE
* LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER
* PARTIES PROVIDE THE PROGRAM “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE
* QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE
* DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
*/
#ifndef MANAGER_H_
#define MANAGER_H_
#include "consts.h"
#include <Rfm12b.h>
#include "RxPacketFromSensor.h"
#include "CcTx.h"
class Manager {
public:
Manager();
void init();
void run();
private:
Rfm12b<RxPacketFromSensor> rfm;
bool auto_pair; /* auto_pair mode on or off? */
id_t pair_with; /* radio ID to pair with */
// bool retry_missing_trxs;
uint8_t trx_retries;
enum {
ONLY_KNOWN, /* Only print packets we know about */
ALL_VALID, /* Print all valid packets */
ALL /* Print all packets, including broken ones */
} print_packets;
/*****************************************
* CC TX (e.g. whole-house transmitters) *
*****************************************/
CcTxArray cc_txs;
/*****************************************
* CC TRX (e.g. EDF IAMs) *
*****************************************/
CcTrxArray cc_trxs;
uint8_t retries; /* number of times we've tried to poll the current TRX */
/* We need to keep track of when we started the TRX roll call so we can
* ensure that we only do one roll call per SAMPLE_PERIOD */
millis_t time_to_start_next_trx_roll_call;
/***************************
* Private methods
***************************/
/* Poll CC TRX (e.g. EDF IAM) with ID == id_next_cc_trx
* Listen for response. */
void poll_next_cc_trx();
void wait_for_cc_tx();
/* @return true if we get a response from id before wait_duration is up */
bool wait_for_response(const id_t& id, const millis_t& wait_duration);
void handle_serial_commands();
/**
* Process every packet in rx_packet_buffer appropriately
*
* @return true if a packet corresponding to id is found
*/
bool process_rx_pack_buf_and_find_id(const id_t& id);
void handle_pair_request(const RxPacketFromSensor& packet);
/**
* If pair_with != ID_INVALID then pair with pair_with.
*/
void pair(const RxPacketFromSensor& packet);
void change_state(const bool state);
/**
* Poll a CurrentCost transceiver (TRX), e.g. an EDF Wireless Transmitter Plug,
* to ask for the latest wattage reading.
*/
void poll_cc_trx(const id_t& id);
/**
* Send acknowledgement to complete pairing.
*/
void ack_cc_trx(const id_t& id);
/**
* Turn TRX on or off.
*/
void change_trx_state(const id_t& id, const bool state);
/**
* Blocks until finished TX.
*/
void send_command_to_trx(const byte& cmd1, const byte& cmd2, const id_t& id);
};
#endif /* MANAGER_H_ */