Update oXs_out_frsky.cpp
[openXsensor.git] / openXsensor / oXs_out_jeti.h
blob249226568c6a678bdd2f9f9e52a4f4ccf841b250
1 #ifndef OXS_OUT_JETI_h
2 #define OXS_OUT_JETI_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 == JETI)
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
38 // list of Jeti type being used
39 #define JETI_14 0b00000001 // jeti TYpe = 1
40 #define JETI_22 0b00000100 // jeti TYpe = 4
41 #define JETI_30 0b00001000 // jeti TYpe = 8
43 // List of used combination of jeti type of data and number of decimals (in bits 5 and 6)
45 #define JETI14_0D 0b00000001 // 0 decimal, jeti TYpe = 1
46 #define JETI14_1D 0b00100001 // 1 decimal, jeti TYpe = 1
47 #define JETI14_2D 0b01000001 // 2 decimal, jeti TYpe = 1
48 #define JETI22_0D 0b00000100 // 0 decimal, jeti TYpe = 1
49 #define JETI22_1D 0b00100100 // 1 decimal, jeti TYpe = 1
50 #define JETI_GPS 0b00001001 // special GPS format in 4 bytes, type = 9 in decimal
52 // This is the list of oXs codes for each available measurements
53 //#define ALTIMETER 1
54 #define VERTICAL_SPEED 2
55 //#define SENSITIVITY 3
56 //#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)
57 #define VOLT_1 5
58 #define VOLT_2 6
59 #define VOLT_3 7
60 #define VOLT_4 8
61 #define VOLT_5 9
62 #define VOLT_6 10
63 #define CURRENTMA 11
64 #define MILLIAH 12
65 #define GPS_COURSE 13
66 #define GPS_SPEED 14
67 #define GPS_ALTITUDE 15
68 #define RPM 16
69 #define GPS_DISTANCE 17
70 #define GPS_BEARING 18
71 //#define SENSITIVITY_2 19
72 //#define ALT_OVER_10_SEC_2 20
73 #define AIR_SPEED 21
74 //#define PRANDTL_COMPENSATION 22
75 //#define PPM_VSPEED 23
76 //#define PPM 24
77 //#define PRANDTL_DTE 25
78 //#define TEST_1 26
79 //#define TEST_2 27
80 //#define TEST_3 28
81 //#define VERTICAL_SPEED_A 29
82 #define REL_ALTIMETER 30
83 //#define REL_ALTIMETER_2 31
84 #define CELL_1 32
85 #define CELL_2 33
86 #define CELL_3 34
87 #define CELL_4 35
88 #define CELL_5 36
89 #define CELL_6 37
90 #define CELL_MIN 38
91 #define CELL_TOT 39
92 #define ALTIMETER_MAX 40
93 #define GPS_LONG 41
94 #define GPS_LAT 42
95 #define FLOW_ACTUAL 43
96 #define FLOW_REMAIN 44
97 #define FLOW_PERCENT 45
98 #define TEMPERATURE 46
99 // 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
100 // End of list of type of available measurements
104 #define UNKNOWN false
105 #define KNOWN true
107 /***************************************************************************************/
108 /* Transmission status */
109 /***************************************************************************************/
110 #define TO_LOAD 0
111 #define LOADED 1
112 #define SENDING 2
113 #define SEND 3
116 class OXS_OUT {
117 public:
118 #ifdef DEBUG
119 OXS_OUT(uint8_t pinTx,HardwareSerial &print);
120 #else
121 OXS_OUT(uint8_t pinTx);
122 #endif
123 VARIODATA* varioData ;
124 VARIODATA* varioData_2 ;
125 AIRSPEEDDATA* airSpeedData ;
126 CURRENTDATA* currentData ;
127 VOLTAGEDATA* voltageData ;
128 void setup();
129 void sendData();
131 private:
132 // used by both protocols
133 uint8_t _pinTx;
134 #ifdef DEBUG
135 HardwareSerial* printer;
136 #endif
138 void formatAllJetiData() ;
139 uint8_t formatOneValue ( uint8_t currentFieldToSend ) ;
140 void setJetiNewData( uint16_t id, int32_t value , uint8_t alarm) ;
141 void initJetiListOfFields() ;
142 boolean retrieveFieldIfAvailable(uint8_t fieldId , int32_t * fieldValue , uint8_t * dataType) ;
143 boolean tryToAddFieldToJetiBuffer (void) ;
144 void addFieldToJetiBuffer(int32_t fieldValue , uint8_t dataType ) ;
145 // uint8_t jeti_crc8 (uint8_t *exbuf, unsigned char framelen) ;
146 void fillJetiBufferWithText() ;
147 void mergeLabelUnit( const uint8_t identifier , const char * label, const char * unit ) ;
148 uint32_t formatGpsLongLat (int32_t longLat, boolean isLong ) ; // return the long or latitude in Jeti format
151 //extern int ppm ;
152 //extern bool ppmAvailable ;
153 extern struct ONE_MEASUREMENT ppm ;
154 extern struct ONE_MEASUREMENT mainVspeed ; // used to transmit the main Vspeed(calculated based on all set up in config)
155 extern struct ONE_MEASUREMENT compensatedClimbRate ; // used to transmit the compensated Vspeed
156 extern struct ONE_MEASUREMENT switchVSpeed ; // used to transmit the selected Vspeed
157 extern struct ONE_MEASUREMENT averageVSpeed ; // used to transmit the average Vspeed
158 extern struct ONE_MEASUREMENT vSpeedImu ; // used to transmit the Vspeedcalculated based on IMU
161 extern struct ONE_MEASUREMENT test1 ;
162 extern struct ONE_MEASUREMENT test2 ;
163 extern struct ONE_MEASUREMENT test3 ;
164 extern struct ONE_MEASUREMENT gliderRatio ;
165 extern struct ONE_MEASUREMENT sport_rpm ;
167 extern uint8_t selectedVario ;
169 extern struct ONE_MEASUREMENT yaw ;
170 extern struct ONE_MEASUREMENT pitch ;
171 extern struct ONE_MEASUREMENT roll ;
174 #ifdef MEASURE_RPM
175 extern volatile uint16_t RpmValue ;
176 extern bool RpmAvailable ;
177 #endif // MEASURE_RPM
179 extern OXS_ADS1115 oXs_ads1115 ;
181 extern volatile uint8_t debug01 ;
182 extern volatile uint8_t debug02 ;
183 extern volatile uint8_t debug03 ;
184 extern volatile uint8_t debug04 ;
187 void setJetiNewData( struct t_sportData * volatile pdata, uint16_t id, uint32_t value ) ;
188 void initJetiUart( struct t_mbAllData * volatile pdata ) ;
191 extern volatile bool RpmSet ;
192 extern volatile uint16_t RpmValue ;
194 extern int32_t GPS_lon; // longitude in degree with 7 decimals, (neg for S)
195 extern int32_t GPS_lat; // latitude in degree with 7 decimals, (neg for ?)
196 extern bool GPS_latAvailable;
197 extern int32_t GPS_altitude; // altitude in mm
198 extern uint16_t GPS_speed_3d; // speed in cm/s
199 extern uint16_t GPS_speed_2d; // speed in cm/s
200 extern uint32_t GPS_ground_course ; // degrees with 5 decimals
201 extern uint8_t GPS_numSat ;
202 extern uint16_t GPS_hdop; // Compute GPS quality signal
203 extern uint8_t GPS_fix_type ;
204 extern int16_t GPS_distance ;
205 extern int16_t GPS_bearing ;
206 extern uint8_t GPS_numSat;
207 extern uint16_t GPS_hdop ; // Compute GPS quality signal
208 extern bool GPS_fix ; // true if gps data are available.
212 // UART's state.
213 #define IDLE 0 // Idle state, both transmit and receive possible.
214 #define TRANSMIT 1 // Transmitting byte.
215 #define TRANSMIT_STOP_BIT 2 // Transmitting stop bit.
216 #define RECEIVE 3 // Receiving byte.
217 #define TxPENDING 4
218 #define WAITING 5
223 //This section chooses the correct timer values for Jeti protocol = 9700 baud.(in fact between 9600 and 9800)
224 // Assumes a 16MHz clock
225 #if F_CPU == 20000000L // 20MHz clock
226 // Sinan: Not tested
227 #define TICKS2COUNTJETI (348*6) // Ticks between two bits.
228 #define TICKS2WAITONEJETI (348*6) // Wait one bit period.
229 #define TICKS2WAITONE_HALFJETI (520*6) // Wait one and a half bit period.
230 #elif F_CPU == 16000000L // 16MHz clock
231 #define TICKS2COUNTJETI (275*6) // Ticks between two bits. For 9600 => (278*6)
232 #define TICKS2WAITONEJETI (275*6) // Wait one bit period. For 9600 => (278*6)
233 #define TICKS2WAITONE_HALFJETI (411*6) // Wait one and a half bit period. For 9600 => (416*6)
234 #elif F_CPU == 8000000L // 8MHz clock
235 #define TICKS2COUNTJETI (139*6) // Ticks between two bits.
236 #define TICKS2WAITONEJETI (139*6) // Wait one bit period.
237 #define TICKS2WAITONE_HALFJETI (208*6) // Wait one and a half bit period.
238 #else
239 #error Unsupported clock speed
240 #endif
243 //#define INTERRUPT_EXEC_CYCL 90 // Cycles to execute interrupt routines from interrupt.
244 //#define INTERRUPT_EARLY_BIAS 32 // Cycles to allow of other interrupts.
245 // INTERRUPT_EARLY_BIAS is to bias the sample point a bit early in case
246 // the Timer 0 interrupt (5.5uS) delays the start bit detection
247 #if F_CPU == 20000000L // 20MHz clock
248 #define INTERRUPT_EXEC_CYCL 90 // Cycles to execute interrupt routines from interrupt.
249 #define INTERRUPT_EARLY_BIAS 32 // Cycles to allow of other interrupts.
250 #elif F_CPU == 16000000L // 16MHz clock
251 #define INTERRUPT_EXEC_CYCL 90 // Cycles to execute interrupt routines from interrupt.
252 #define INTERRUPT_EARLY_BIAS 32 // Cycles to allow of other interrupts.
253 #elif F_CPU == 8000000L // 8MHz clock
254 #define INTERRUPT_EXEC_CYCL 90 // Cycles to execute interrupt routines from interrupt.
255 #define INTERRUPT_EARLY_BIAS 32 // Cycles to allow of other interrupts.
256 #else
257 #error Unsupported clock speed
258 #endif
260 #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)
261 #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)
263 // this section define some delays used ; values can be used by any protocol
264 #if F_CPU == 20000000L // 20MHz clock
265 #define DELAY_4000 ((uint16_t)4000.0 * 20.0 /16.0 )
266 #define DELAY_3500 ((uint16_t)3500.0 * 20.0 /16.0 )
267 #define DELAY_2000 ((uint16_t)2000.0 * 20.0 /16.0 )
268 #define DELAY_1600 ((uint16_t)1600.0 * 20.0 /16.0 )
269 #define DELAY_400 ((uint16_t)400.0 * 20.0 /16.0 )
270 #define DELAY_100 ((uint16_t)100.0 * 20.0 /16.0 )
272 #elif F_CPU == 16000000L // 16MHz clock
273 #define DELAY_4000 ((uint16_t) (4000L * 16) )
274 #define DELAY_3500 ((uint16_t) (3500L * 16) )
275 #define DELAY_2000 ((uint16_t) (2000L * 16) )
276 #define DELAY_1600 ((uint16_t) (1600L * 16) )
277 #define DELAY_400 ((uint16_t) (400 * 16) )
278 #define DELAY_100 ((uint16_t) (100 * 16) )
279 #elif F_CPU == 8000000L // 8MHz clock
280 #define DELAY_4000 ((uint16_t)4000L * 8 )
281 #define DELAY_3500 ((uint16_t)3500L * 8 )
282 #define DELAY_2000 ((uint16_t)2000 * 8 )
283 #define DELAY_1600 ((uint16_t)1600 * 8 )
284 #define DELAY_400 ((uint16_t)400 * 8 )
285 #define DELAY_100 ((uint16_t)100 * 8 )
286 #else
287 #error Unsupported clock speed
288 #endif
290 #define TCCR TCCR1A //!< Timer/Counter Control Register
291 #define TCCR_P TCCR1B //!< Timer/Counter Control (Prescaler) Register
292 #define OCR OCR1A //!< Output Compare Register
293 #define EXT_IFR EIFR //!< External Interrupt Flag Register
294 #define EXT_ICR EICRA //!< External Interrupt Control Register
296 #define TRXDDR DDRD
297 #define TRXPORT PORTD
298 #define TRXPIN PIND
300 #define SET_TX_PIN( ) ( TRXPORT |= ( 1 << PIN_SERIALTX ) )
301 #define CLEAR_TX_PIN( ) ( TRXPORT &= ~( 1 << PIN_SERIALTX ) )
302 #define GET_RX_PIN( ) ( TRXPIN & ( 1 << PIN_SERIALTX ) )
304 #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
305 #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
307 #define SET_TX_PIN_TO_OUTPUT( ) (TRXDDR |= ( 1 << PIN_SERIALTX ) ) // put TX pin as output
308 #define SET_TX_PIN_TO_INPUT( ) (TRXDDR &= ~( 1 << PIN_SERIALTX ) ; ( TRXPORT |= ( 1 << PIN_SERIALTX ) ) // put TX pin as input with pullup activated
310 // values used by the state ".active"
311 #define NOT_ACTIVE 0
312 #define NOT_AVAILABLE 1
313 #define LOCKED 2
314 #define AVAILABLE 3
316 // Jeti protocol is 9 bits, 2 stops bits, odd parity, 9600-9800 bauds
319 #endif // End of JETI
320 #endif // OXS_OUT_JETI_h