MySensors Library & Examples  2.3.2-62-ge298769
PJONSwitch.h
1 
2 /*-O//\ __ __
3  |-gfo\ |__| | | | |\ | ®
4  |!y°o:\ | __| |__| | \| 13.0
5  |y"s§+`\ multi-master, multi-media bus network protocol
6  /so+:-..`\ Copyright 2010-2020 by Giovanni Blu Mitolo [email protected]
7  |+/:ngr-*.`\
8  |5/:%&-a3f.:;\
9  \+//u/+g%{osv,,\
10  \=+&/osw+olds.\\
11  \:/+-.-°-:+oss\
12  | | \oy\\
13  > <
14 ______-| |-__________________________________________________________________
15 
16 PJONSwitch has been contributed by Fred Larsen.
17 
18 This class does the same as the PJONSimpleSwitch but supports routing packets
19 between buses of different strategies using the Any strategy.
20 _____________________________________________________________________________
21 
22 This software is experimental and it is distributed "AS IS" without any
23 warranty, use it at your own risk.
24 
25 Copyright 2010-2020 by Giovanni Blu Mitolo [email protected]
26 
27 Licensed under the Apache License, Version 2.0 (the "License");
28 you may not use this file except in compliance with the License.
29 You may obtain a copy of the License at
30 
31  http://www.apache.org/licenses/LICENSE-2.0
32 
33 Unless required by applicable law or agreed to in writing, software
34 distributed under the License is distributed on an "AS IS" BASIS,
35 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
36 See the License for the specific language governing permissions and
37 limitations under the License. */
38 
39 #pragma once
40 
41 #include "PJONSimpleSwitch.h"
42 #include "strategies/Any/Any.h"
43 
44 class PJONAny : public PJON<Any>
45 {
46 public:
47  PJONAny(
48  const uint8_t id = PJON_NOT_ASSIGNED
49  ) : PJON<Any>(id) {};
50 
51  PJONAny(
52  StrategyLinkBase *link,
53  const uint8_t id = PJON_NOT_ASSIGNED
54  ) : PJON<Any>(id)
55  {
56  strategy.set_link(link);
57  };
58 
59  PJONAny(
60  StrategyLinkBase *link,
61  const uint8_t bus_id[],
62  const uint8_t id = PJON_NOT_ASSIGNED
63  ) : PJON<Any>(
64  bus_id,
65  id
66  )
67  {
68  strategy.set_link(link);
69  }
70 
71  void set_link(StrategyLinkBase *link)
72  {
73  strategy.set_link(link);
74  }
75 };
76 
77 class PJONSwitch : public PJONSimpleSwitch<Any>
78 {
79 public:
81 
82  PJONSwitch(
83  uint8_t bus_count,
84  PJONAny * const bus_list[],
85  uint8_t default_gateway = PJON_NOT_ASSIGNED
86  ) : PJONSimpleSwitch<Any>(bus_count, (PJON<Any>* const *)bus_list, default_gateway) { };
87 };
88 
89 // Specialized class to simplify declaration when using 2 buses
90 template<class A, class B>
91 class PJONSwitch2 : public PJONSwitch
92 {
93  StrategyLink<A> linkA;
94  StrategyLink<B> linkB;
95  PJONAny busA, busB;
96 public:
97  PJONSwitch2(uint8_t default_gateway = PJON_NOT_ASSIGNED)
98  {
99  PJON<Any>* buses[2] = { &busA, &busB };
100  PJONSimpleSwitch<Any>::connect_buses(2, buses, default_gateway);
101  busA.set_link(&linkA);
102  busB.set_link(&linkB);
103  };
104 
105  PJONAny &get_bus(const uint8_t ix)
106  {
107  return ix == 0 ? busA : busB;
108  }
109 
110  A &get_strategy_0()
111  {
112  return linkA.strategy;
113  }
114  B &get_strategy_1()
115  {
116  return linkB.strategy;
117  }
118 };
119 
120 // Specialized class to simplify declaration when using 3 buses
121 template<class A, class B, class C>
122 class PJONSwitch3 : public PJONSwitch
123 {
124  StrategyLink<A> linkA;
125  StrategyLink<B> linkB;
126  StrategyLink<C> linkC;
127  PJONAny busA, busB, busC;
128 public:
129  PJONSwitch3(uint8_t default_gateway = PJON_NOT_ASSIGNED)
130  {
131  PJON<Any> *buses[3] = { &busA, &busB, &busC };
132  PJONSimpleSwitch<Any>::connect_buses(3, buses, default_gateway);
133  busA.set_link(&linkA);
134  busB.set_link(&linkB);
135  busC.set_link(&linkC);
136  };
137 
138  PJONAny &get_bus(const uint8_t ix)
139  {
140  return ix == 0 ? busA : (ix == 1 ? busB : busC);
141  }
142 
143  A &get_strategy_0()
144  {
145  return linkA.strategy;
146  }
147  B &get_strategy_1()
148  {
149  return linkB.strategy;
150  }
151  C &get_strategy_2()
152  {
153  return linkC.strategy;
154  }
155 };
PJONSwitch
Definition: PJONSwitch.h:77
StrategyLinkBase
Definition: StrategyLinkBase.h:23
PJONAny
Definition: PJONSwitch.h:44
PJON
Definition: PJON.h:77
PJONSwitch3
Definition: PJONSwitch.h:122
PJONSimpleSwitch
Definition: PJONSimpleSwitch.h:63
PJONSwitch2
Definition: PJONSwitch.h:91