Update oXs_out_frsky.cpp
[openXsensor.git] / openXsensor / oXs_out_multiplex.h
blob2cbbd339d5d2c76aaca602a945321f11cbb1bd67
1 #ifndef OXS_OUT_MULTIPLEX_h
2 #define OXS_OUT_MULTIPLEX_h
4 #include "oXs_config_basic.h"
5 #include "oXs_config_advanced.h"
6 #include "oXs_config_macros.h"
7 #include "oXs_ms5611.h" // we need the variodata struct
8 #include "oXs_4525.h" // we need the airspeeddata struct
9 #include "oXs_curr.h" // we need the currentdata struct
10 #include "oXs_voltage.h" // we need the arduinodata struct
11 #include "oXs_sdp3x.h" // we need the airspeed data
12 //#include <Arduino.h>
13 #include "oXs_general.h"
14 #include "oXs_gps.h"
16 #if defined(PROTOCOL) && (PROTOCOL == MULTIPLEX)
19 struct t_mbOneData {
20 uint8_t volatile active;
21 uint8_t volatile response[3];
22 } ;
24 struct t_mbAllData {
25 struct t_mbOneData mbData[16] ;
26 } ;
30 #define MB_CMD_RESET 0x5A
31 #define MB_CMD_RESERVED_MIN 0x80
32 #define MB_CMD_RESERVED_MAX 0x8F
34 #define MB_NOVALUE 0x8000
36 #define MB_MAX_ADRESS 15
40 //list of all telemetry units supported by Multiplex protocol
41 #define MU_ALT 0x08 // 1m (-500 2000)
42 #define MU_VSPD 0x03 // 0,1 m/s (-500 500)
43 #define MU_CURR 0x02 // 0,1 A (-1000 1000)
44 #define MU_VOLT 0x01 // 0,1 V (-600 600)
45 #define MU_TEMP 0x06 // 0,1 C (-250 7000)
46 #define MU_RPM 0x05 // 100t/m?? ou 10t/min
47 #define MU_MAH 0x0B // 1mAh (-16000 16000)
48 #define MU_ASPD 0x04 // 0,1km/h (0-6000)
49 #define MU_LEVEL 0x09 // 1% (0-100)
50 #define MU_DIR 0x07 // 0,1 degrés (0 3600)
51 #define MU_LIQUID 0x0C // 1ml (0-16000)
52 #define MU_DIST 0x0D // 0,1 km (0-16000)
53 // End of list of all telemetry units supported by Multiplex protocol
56 #define MULTIPLEX_UNITS MU_LEVEL , MU_ALT , MU_VSPD , MU_LEVEL , MU_ALT , MU_VOLT , MU_VOLT , MU_VOLT , MU_VOLT , MU_VOLT ,\
57 MU_VOLT , MU_CURR , MU_MAH , MU_DIR , MU_ASPD , MU_ALT , MU_RPM , MU_ALT , MU_DIR , MU_LEVEL ,\
58 MU_ALT , MU_ASPD , MU_VSPD , MU_LEVEL , MU_LEVEL , MU_VSPD , MU_LEVEL , MU_LEVEL , MU_LEVEL , MU_VSPD , \
59 MU_ALT , MU_ALT , MU_VOLT , MU_VOLT , MU_VOLT , MU_VOLT , MU_VOLT , MU_VOLT , MU_VOLT , MU_VOLT , \
60 MU_ALT
63 // This is the list of codes for each available measurements
64 #define ALTIMETER 1
65 #define VERTICAL_SPEED 2
66 #define SENSITIVITY 3
67 #define ALT_OVER_10_SEC 4 // DEFAULTFIELD can NOT be used ; this is the difference of altitude over the last 10 sec (kind of averaging vertical speed)
69 #define VOLT_1 5
70 #define VOLT_2 6
71 #define VOLT_3 7
72 #define VOLT_4 8
73 #define VOLT_5 9
74 #define VOLT_6 10
75 #define CURRENTMA 11
76 #define MILLIAH 12
77 #define GPS_COURSE 13
78 #define GPS_SPEED 14
79 #define GPS_ALTITUDE 15
80 #define RPM 16
81 #define GPS_DISTANCE 17
82 #define GPS_BEARING 18
83 #define SENSITIVITY_2 19
84 #define ALT_OVER_10_SEC_2 20
85 #define AIR_SPEED 21
86 #define PRANDTL_COMPENSATION 22
87 #define PPM_VSPEED 23
88 #define PPM 24
89 #define PRANDTL_DTE 25
90 #define TEST_1 26
91 #define TEST_2 27
92 #define TEST_3 28
93 #define VERTICAL_SPEED_A 29
94 #define REL_ALTIMETER 30
95 #define REL_ALTIMETER_2 31
96 #define CELL_1 32
97 #define CELL_2 33
98 #define CELL_3 34
99 #define CELL_4 35
100 #define CELL_5 36
101 #define CELL_6 37
102 #define CELL_MIN 38
103 #define CELL_TOT 39
104 #define ALTIMETER_MAX 40
105 // to do : add alt min, alt max , rpm max? , current max (not sure that it is neaded because it can be calculated on TX side
106 // End of list of type of available measurements
110 #define UNKNOWN false
111 #define KNOWN true
113 /***************************************************************************************/
114 /* Transmission status */
115 /***************************************************************************************/
116 #define TO_LOAD 0
117 #define LOADED 1
118 #define SENDING 2
119 #define SEND 3
122 class OXS_OUT {
123 public:
124 #ifdef DEBUG
125 OXS_OUT(uint8_t pinTx,HardwareSerial &print);
126 #else
127 OXS_OUT(uint8_t pinTx);
128 #endif
129 VARIODATA* varioData ;
130 VARIODATA* varioData_2 ;
131 AIRSPEEDDATA* airSpeedData ;
132 CURRENTDATA* currentData ;
133 VOLTAGEDATA* voltageData ;
134 void setup();
135 void sendData();
137 private:
138 // used by both protocols
139 uint8_t _pinTx;
140 #ifdef DEBUG
141 HardwareSerial* printer;
142 #endif
144 void formatAllMultiplexData() ;
145 uint8_t formatOneValue ( uint8_t currentFieldToSend ) ;
146 void setMultiplexNewData( uint16_t id, int32_t value , uint8_t alarm) ;
149 //extern int ppm ;
150 //extern bool ppmAvailable ;
151 extern struct ONE_MEASUREMENT ppm ;
152 extern struct ONE_MEASUREMENT mainVspeed ; // used to transmit the main Vspeed(calculated based on all set up in config)
153 extern struct ONE_MEASUREMENT compensatedClimbRate ; // used to transmit the compensated Vspeed
154 extern struct ONE_MEASUREMENT switchVSpeed ; // used to transmit the selected Vspeed
155 extern struct ONE_MEASUREMENT averageVSpeed ; // used to transmit the average Vspeed
156 extern struct ONE_MEASUREMENT vSpeedImu ; // used to transmit the Vspeedcalculated based on IMU
159 extern struct ONE_MEASUREMENT test1 ;
160 extern struct ONE_MEASUREMENT test2 ;
161 extern struct ONE_MEASUREMENT test3 ;
162 extern struct ONE_MEASUREMENT gliderRatio ;
163 extern struct ONE_MEASUREMENT sport_rpm ;
165 extern uint8_t selectedVario ;
167 extern struct ONE_MEASUREMENT yaw ;
168 extern struct ONE_MEASUREMENT pitch ;
169 extern struct ONE_MEASUREMENT roll ;
172 #ifdef MEASURE_RPM
173 extern volatile uint16_t RpmValue ;
174 extern bool RpmAvailable ;
175 #endif // MEASURE_RPM
177 extern volatile uint8_t debug01 ;
178 extern volatile uint8_t debug02 ;
179 extern volatile uint8_t debug03 ;
180 extern volatile uint8_t debug04 ;
183 void setMultiplexNewData( struct t_sportData * volatile pdata, uint16_t id, uint32_t value ) ;
184 void initMultiplexUart( struct t_mbAllData * volatile pdata ) ;
187 extern volatile bool RpmSet ;
188 extern volatile uint16_t RpmValue ;
190 extern int32_t GPS_lon; // longitude in degree with 7 decimals, (neg for S)
191 extern int32_t GPS_lat; // latitude in degree with 7 decimals, (neg for ?)
192 extern bool GPS_latAvailable;
193 extern int32_t GPS_altitude; // altitude in mm
194 extern uint16_t GPS_speed_3d; // speed in cm/s
195 extern uint16_t GPS_speed_2d; // speed in cm/s
196 extern uint32_t GPS_ground_course ; // degrees with 5 decimals
197 extern uint8_t GPS_numSat ;
198 extern uint16_t GPS_hdop; // Compute GPS quality signal
199 extern bool GPS_fix ; // true if gps data are available.
202 extern uint8_t GPS_fix_type ;
203 extern int16_t GPS_distance ;
204 extern int16_t GPS_bearing ;
208 // UART's state.
209 #define IDLE 0 // Idle state, both transmit and receive possible.
210 #define TRANSMIT 1 // Transmitting byte.
211 #define TRANSMIT_STOP_BIT 2 // Transmitting stop bit.
212 #define RECEIVE 3 // Receiving byte.
213 #define TxPENDING 4
214 #define WAITING 5
217 //This section chooses the correct timer values for Multiplex protocol = 38400 baud.
218 // Assumes a 16MHz clock
219 //#define TICKS2COUNT (278*6) // Ticks between two bits.
220 //#define TICKS2WAITONE (278*6) // Wait one bit period.
221 //#define TICKS2WAITONE_HALF (416*6) // Wait one and a half bit period.
222 #if F_CPU == 20000000L // 20MHz clock
223 // Sinan: Not tested
224 #define TICKS2COUNTMULTIPLEX (521) // Ticks between two bits.
225 #define TICKS2WAITONEMULTIPLEX (521) // Wait one bit period.
226 #define TICKS2WAITONE_HALFMULTIPLEX (781) // Wait one and a half bit period.
227 #elif F_CPU == 16000000L // 16MHz clock
228 #define TICKS2COUNTMULTIPLEX (417) // Ticks between two bits.
229 #define TICKS2WAITONEMULTIPLEX (417) // Wait one bit period.
230 #define TICKS2WAITONE_HALFMULTIPLEX (625) // Wait one and a half bit period.
231 #elif F_CPU == 8000000L // 8MHz clock
232 #define TICKS2COUNTMULTIPLEX (208) // Ticks between two bits.
233 #define TICKS2WAITONEMULTIPLEX (208) // Wait one bit period.
234 #define TICKS2WAITONE_HALFMULTIPLEX (313) // Wait one and a half bit period.
235 #else
236 #error Unsupported clock speed
237 #endif
241 //#define INTERRUPT_EXEC_CYCL 90 // Cycles to execute interrupt routines from interrupt.
242 //#define INTERRUPT_EARLY_BIAS 32 // Cycles to allow of other interrupts.
243 // INTERRUPT_EARLY_BIAS is to bias the sample point a bit early in case
244 // the Timer 0 interrupt (5.5uS) delays the start bit detection
245 #if F_CPU == 20000000L // 20MHz clock
246 #define INTERRUPT_EXEC_CYCL 90 // Cycles to execute interrupt routines from interrupt.
247 #define INTERRUPT_EARLY_BIAS 32 // Cycles to allow of other interrupts.
248 #elif F_CPU == 16000000L // 16MHz clock
249 #define INTERRUPT_EXEC_CYCL 90 // Cycles to execute interrupt routines from interrupt.
250 #define INTERRUPT_EARLY_BIAS 32 // Cycles to allow of other interrupts.
251 #elif F_CPU == 8000000L // 8MHz clock
252 #define INTERRUPT_EXEC_CYCL 90 // Cycles to execute interrupt routines from interrupt.
253 #define INTERRUPT_EARLY_BIAS 32 // Cycles to allow of other interrupts.
254 #else
255 #error Unsupported clock speed
256 #endif
258 #define INTERRUPT_ENTRY_TRANSMIT 59 // Cycles in ISR before sending first bit from first byte; Without this correction, first bit is sent 7.4 usec to late at 8 Mhz (so it takes 59 cycles = 7.4 usec * 8)
259 #define INTERRUPT_BETWEEN_TRANSMIT 64 // Cycles in ISR before sending first bit from 2d, 3rd... bytes; Without this correction, first bit is sent 4 usec to late at 16 Mhz (so it takes 64 cycles = 4 usec * 16)
261 // this section define some delays used ; values can be used by any protocol
262 #if F_CPU == 20000000L // 20MHz clock
263 #define DELAY_4000 ((uint16_t)4000.0 * 20.0 /16.0 )
264 #define DELAY_3500 ((uint16_t)3500.0 * 20.0 /16.0 )
265 #define DELAY_2000 ((uint16_t)2000.0 * 20.0 /16.0 )
266 #define DELAY_1600 ((uint16_t)1600.0 * 20.0 /16.0 )
267 #define DELAY_400 ((uint16_t)400.0 * 20.0 /16.0 )
268 #define DELAY_100 ((uint16_t)100.0 * 20.0 /16.0 )
270 #elif F_CPU == 16000000L // 16MHz clock
271 #define DELAY_4000 ((uint16_t) (4000L * 16) )
272 #define DELAY_3500 ((uint16_t) (3500L * 16) )
273 #define DELAY_2000 ((uint16_t) (2000L * 16) )
274 #define DELAY_1600 ((uint16_t) (1600L * 16) )
275 #define DELAY_400 ((uint16_t) (400 * 16) )
276 #define DELAY_100 ((uint16_t) (100 * 16) )
277 #elif F_CPU == 8000000L // 8MHz clock
278 #define DELAY_4000 ((uint16_t)4000L * 8 )
279 #define DELAY_3500 ((uint16_t)3500L * 8 )
280 #define DELAY_2000 ((uint16_t)2000 * 8 )
281 #define DELAY_1600 ((uint16_t)1600 * 8 )
282 #define DELAY_400 ((uint16_t)400 * 8 )
283 #define DELAY_100 ((uint16_t)100 * 8 )
284 #else
285 #error Unsupported clock speed
286 #endif
288 #define TCCR TCCR1A //!< Timer/Counter Control Register
289 #define TCCR_P TCCR1B //!< Timer/Counter Control (Prescaler) Register
290 #define OCR OCR1A //!< Output Compare Register
291 #define EXT_IFR EIFR //!< External Interrupt Flag Register
292 #define EXT_ICR EICRA //!< External Interrupt Control Register
294 #define TRXDDR DDRD
295 #define TRXPORT PORTD
296 #define TRXPIN PIND
298 #define SET_TX_PIN( ) ( TRXPORT |= ( 1 << PIN_SERIALTX ) )
299 #define CLEAR_TX_PIN( ) ( TRXPORT &= ~( 1 << PIN_SERIALTX ) )
300 #define GET_RX_PIN( ) ( TRXPIN & ( 1 << PIN_SERIALTX ) )
302 #define SET_TX_PIN_MB( ) ( TRXDDR &= ~( 1 << PIN_SERIALTX ) ) // in fact we put the port in high impedance because the rx has a pull up resistance
303 #define CLEAR_TX_PIN_MB( ) ( TRXDDR |= ( 1 << PIN_SERIALTX ) ) // in fact we put the port in output mode. PORT is already set to 0 during initialisation
305 #define MB_MAX_ADRESS 15
307 // values used by the state ".active"
308 #define NOT_ACTIVE 0
309 #define NOT_AVAILABLE 1
310 #define LOCKED 2
311 #define AVAILABLE 3
314 #endif // End of MULTIPLEX
315 #endif // OXS_OUT_MULTIPLEX_h