Radio control: use the Jeti protocol to communicate between a DIY sensor and a Jeti receiver (using 9 bits one wire serial)
JetiC.cpp@0:32193823db59, 2013-10-29 (annotated)
- Committer:
- robertspil
- Date:
- Tue Oct 29 19:39:38 2013 +0000
- Revision:
- 0:32193823db59
Initial release -tested with 1768 and 11U34
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
robertspil | 0:32193823db59 | 1 | /*======== communication between a sensor (processor 32 bits) and a Jeti Duplex receiver============== |
robertspil | 0:32193823db59 | 2 | _____Jeti Data communication protocol______________________ |
robertspil | 0:32193823db59 | 3 | The communication from the RX module to the JetiBox |
robertspil | 0:32193823db59 | 4 | 1. It is 1-wire serial communication (9600 Bauds 9bits+parity + 2 stops)) |
robertspil | 0:32193823db59 | 5 | 2. 1 message sent consists of 34 byte, 2 control character (start=0xfe finish=0xff) and 32 bytes text between these control characters. |
robertspil | 0:32193823db59 | 6 | 4. 1 Byte consists of 13 bits in the format: T-D-D-D-D-D-D-D-D-I-P-S-S (LSB first= T-0-1-2-3-4-5-6-7-I-P-S-S) |
robertspil | 0:32193823db59 | 7 | where: T = startbit, D = 8 data bits, I control characters or text ( 1=text, 0=control), P = parity based odd , S = stop bit (two). |
robertspil | 0:32193823db59 | 8 | 5. The JetiBox answers each message (34 byte) with one byte ACK message (with I=0: control byte) |
robertspil | 0:32193823db59 | 9 | 0xF0 =11110000 if no button (four bits with value 1 if the button is NOT pushed |
robertspil | 0:32193823db59 | 10 | bit 4 =Right , Bit 5 = Up (or Back) , Bit 6 = Down or Select und Bit 7 Left |
robertspil | 0:32193823db59 | 11 | 6.a short delay is needed between the received message and the ACK message |
robertspil | 0:32193823db59 | 12 | from the end of 0xff to begin of ACK :3,9 msec (mesured with Saleae) |
robertspil | 0:32193823db59 | 13 | -TU module waits 21 msec and reply with a new 34 bytes message => total length = 79 msec |
robertspil | 0:32193823db59 | 14 | |
robertspil | 0:32193823db59 | 15 | If there is no ACK, the TU module waits 25 msec and resend the message |
robertspil | 0:32193823db59 | 16 | |
robertspil | 0:32193823db59 | 17 | ____hardware_________________________ |
robertspil | 0:32193823db59 | 18 | - a Mbed processor |
robertspil | 0:32193823db59 | 19 | - one pin of the processor provides the signal (Tx and Rx) to the receiver or to a JetiBox |
robertspil | 0:32193823db59 | 20 | - I use a safety resistor 1k to 4.7k between the Pin and the Rx |
robertspil | 0:32193823db59 | 21 | |
robertspil | 0:32193823db59 | 22 | ___tested with |
robertspil | 0:32193823db59 | 23 | 1) a JetiBox directly connected to the Mbed processor and to a 4.8V battery |
robertspil | 0:32193823db59 | 24 | 2) a a JeTi receiver , a transmitter with a Jeti TU module |
robertspil | 0:32193823db59 | 25 | |
robertspil | 0:32193823db59 | 26 | The processor: MBED lpc1768 or LPC11u34 (The same program exists with a Ardiono Pro Mini 3.3V - with native 9 bits) |
robertspil | 0:32193823db59 | 27 | |
robertspil | 0:32193823db59 | 28 | ___communication between the sensor programs and the Jeti interface________________ |
robertspil | 0:32193823db59 | 29 | - start the Jeti interface with a instance of the class JetiC, - afterwards the interface is running automatically with interrupts |
robertspil | 0:32193823db59 | 30 | - process the answer from the JetiBox |
robertspil | 0:32193823db59 | 31 | isAnswer() returns True when a answer message is received |
robertspil | 0:32193823db59 | 32 | getAnswer() returns the answer code (look at main.cpp) |
robertspil | 0:32193823db59 | 33 | |
robertspil | 0:32193823db59 | 34 | |
robertspil | 0:32193823db59 | 35 | */ |
robertspil | 0:32193823db59 | 36 | /*============================================================================ |
robertspil | 0:32193823db59 | 37 | Copyright (C) 2013 Robert Spilleboudt |
robertspil | 0:32193823db59 | 38 | |
robertspil | 0:32193823db59 | 39 | This program is free software; you can redistribute it and/or modify |
robertspil | 0:32193823db59 | 40 | it under the terms of the GNU General Public License as published by |
robertspil | 0:32193823db59 | 41 | the Free Software Foundation; either version 2 of the License, or |
robertspil | 0:32193823db59 | 42 | (at your option) any later version. |
robertspil | 0:32193823db59 | 43 | |
robertspil | 0:32193823db59 | 44 | This program is distributed in the hope that it will be useful, |
robertspil | 0:32193823db59 | 45 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
robertspil | 0:32193823db59 | 46 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
robertspil | 0:32193823db59 | 47 | GNU General Public License for more details. |
robertspil | 0:32193823db59 | 48 | |
robertspil | 0:32193823db59 | 49 | You should have received a copy of the GNU General Public License |
robertspil | 0:32193823db59 | 50 | along with this program; if not, write to the Free Software |
robertspil | 0:32193823db59 | 51 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
robertspil | 0:32193823db59 | 52 | ============================================================================*/ |
robertspil | 0:32193823db59 | 53 | |
robertspil | 0:32193823db59 | 54 | #include "core.h" |
robertspil | 0:32193823db59 | 55 | |
robertspil | 0:32193823db59 | 56 | #define UART_PIN p29 //pin for tx or rx , connect to the RX module through a 4.7k resitance |
robertspil | 0:32193823db59 | 57 | #define UART_W1 96 /* this is the pulse width , the theoretical value is 104(microsec) for 9600 bauds but there are some bugs... |
robertspil | 0:32193823db59 | 58 | Online compiler mbed.org |
robertspil | 0:32193823db59 | 59 | with mbed 1768 use the theoretical value 104 |
robertspil | 0:32193823db59 | 60 | with mbed 11u24 use 96 |
robertspil | 0:32193823db59 | 61 | Offline compiler code sourcery (2012/03) |
robertspil | 0:32193823db59 | 62 | mbed 1768 use 96 |
robertspil | 0:32193823db59 | 63 | for the 11u24: not tested because the .bin size is too great |
robertspil | 0:32193823db59 | 64 | */ |
robertspil | 0:32193823db59 | 65 | extern instr_data rc; //common data for all the programs |
robertspil | 0:32193823db59 | 66 | /*-------------data for the interrupts are here---------------------------*/ |
robertspil | 0:32193823db59 | 67 | volatile uint8_t UART_pos; |
robertspil | 0:32193823db59 | 68 | /*Normal text message |
robertspil | 0:32193823db59 | 69 | * UART_pos = 0 -> start sending data with this interrupt enabled, start byte with 9.bit=0 |
robertspil | 0:32193823db59 | 70 | * UART_pos = 1-32 -> send display data with 9.bit=1 |
robertspil | 0:32193823db59 | 71 | * UART_pos = 33 -> send end byte with 9.bit=0 |
robertspil | 0:32193823db59 | 72 | * UART_pos = 34 -> set sendpos=0 and disable this interrupt |
robertspil | 0:32193823db59 | 73 | Special message when returning to the expander :\x7E\x01\x31 |
robertspil | 0:32193823db59 | 74 | * UART_pos =100 start sending data with this interrupt enabled, start byte with 9.bit=0 , value \x7E |
robertspil | 0:32193823db59 | 75 | * UART_pos =101 send \x01 with 9.bit=1 |
robertspil | 0:32193823db59 | 76 | * UART_pos =102 send \x31 with 9.bit=1 |
robertspil | 0:32193823db59 | 77 | ending with 34 to disble the interrupt |
robertspil | 0:32193823db59 | 78 | */ |
robertspil | 0:32193823db59 | 79 | volatile unsigned char JETI_buffer[33]; // the message from the sensot to the JetiBox |
robertspil | 0:32193823db59 | 80 | volatile unsigned char UART_data ; //character transmitted or received, being filled bit / bit |
robertspil | 0:32193823db59 | 81 | volatile bool UART_isanswer; |
robertspil | 0:32193823db59 | 82 | DigitalInOut UART_pin(UART_PIN); // pin connected to the signal from Rx module |
robertspil | 0:32193823db59 | 83 | InterruptIn UART_startBit(UART_PIN) ; //interrupt to detect the start bit (RX) |
robertspil | 0:32193823db59 | 84 | Timeout UART_nextBit; // pulse duration |
robertspil | 0:32193823db59 | 85 | Timeout UART_nextTx; //next start of transmission |
robertspil | 0:32193823db59 | 86 | Ticker UART_ticker; //ticker period= 1 msec |
robertspil | 0:32193823db59 | 87 | volatile int UART_Tx_start;//msec counter to the next start of a tx message |
robertspil | 0:32193823db59 | 88 | |
robertspil | 0:32193823db59 | 89 | //---------busy statistic---------------------------------------------- |
robertspil | 0:32193823db59 | 90 | //char processing |
robertspil | 0:32193823db59 | 91 | volatile int UART_counter;//within a char : next bit position value 0..8 (9 bits)+ 9=parity 10,11=stop FOR RX or TX |
robertspil | 0:32193823db59 | 92 | bool textchar ; //true if text, false if control |
robertspil | 0:32193823db59 | 93 | bool UART_errorchar ;// true if error (parity, stop bit) |
robertspil | 0:32193823db59 | 94 | uint8_t UART_parity; |
robertspil | 0:32193823db59 | 95 | void ISRstartBit() ; |
robertspil | 0:32193823db59 | 96 | void ISRdataBitRx(); |
robertspil | 0:32193823db59 | 97 | void ISRdataBitTx(); |
robertspil | 0:32193823db59 | 98 | //----interrupt routines --------------------------------------------------------------------------------------- |
robertspil | 0:32193823db59 | 99 | void UART_tickerAdd(){ //call every msec |
robertspil | 0:32193823db59 | 100 | UART_Tx_start--; |
robertspil | 0:32193823db59 | 101 | if( UART_Tx_start<=0){//start the Tx message |
robertspil | 0:32193823db59 | 102 | UART_pos = 0; |
robertspil | 0:32193823db59 | 103 | UART_counter= -1; |
robertspil | 0:32193823db59 | 104 | UART_nextTx.attach_us(&ISRdataBitTx , 1000); |
robertspil | 0:32193823db59 | 105 | UART_Tx_start=10000; //the next start is not yet scheduled |
robertspil | 0:32193823db59 | 106 | UART_startBit.fall(NULL); // no RX start bit interrupt |
robertspil | 0:32193823db59 | 107 | UART_pin.output(); |
robertspil | 0:32193823db59 | 108 | UART_pin=1; |
robertspil | 0:32193823db59 | 109 | UART_isanswer=false; |
robertspil | 0:32193823db59 | 110 | } |
robertspil | 0:32193823db59 | 111 | } |
robertspil | 0:32193823db59 | 112 | |
robertspil | 0:32193823db59 | 113 | void ISRstartBit() { //Rx detect the start bit, start timeout to read the first data bit |
robertspil | 0:32193823db59 | 114 | UART_nextBit.attach_us(&ISRdataBitRx , UART_W1); |
robertspil | 0:32193823db59 | 115 | UART_counter= -1;//preparing for the first data bit |
robertspil | 0:32193823db59 | 116 | UART_data=0 ;//received data |
robertspil | 0:32193823db59 | 117 | UART_errorchar = false; |
robertspil | 0:32193823db59 | 118 | UART_parity=0; |
robertspil | 0:32193823db59 | 119 | UART_startBit.fall(NULL); // detach the ISRstartBit |
robertspil | 0:32193823db59 | 120 | UART_Tx_start =10000; // no not start Tx during the Rx receive |
robertspil | 0:32193823db59 | 121 | } |
robertspil | 0:32193823db59 | 122 | void ISRdataBitRx() { //nextbit timeout in Rx mode |
robertspil | 0:32193823db59 | 123 | UART_counter++; |
robertspil | 0:32193823db59 | 124 | if(UART_counter< 12) { //this is not the last bit of a character - immediatly start the next timer interrupt |
robertspil | 0:32193823db59 | 125 | UART_nextBit.attach_us(&ISRdataBitRx , UART_W1); // next bit |
robertspil | 0:32193823db59 | 126 | } |
robertspil | 0:32193823db59 | 127 | int pinread = UART_pin.read(); |
robertspil | 0:32193823db59 | 128 | if(UART_counter<8) { // process the first 8 databits |
robertspil | 0:32193823db59 | 129 | UART_data = (UART_data >> 1); //Right shift RX_data so the new bit can be masked into the Rx_data byte. |
robertspil | 0:32193823db59 | 130 | if(pinread==1) { |
robertspil | 0:32193823db59 | 131 | UART_data |= 0x80; //Set MSB of RX data if received bit == 1. |
robertspil | 0:32193823db59 | 132 | UART_parity++; |
robertspil | 0:32193823db59 | 133 | } |
robertspil | 0:32193823db59 | 134 | return; |
robertspil | 0:32193823db59 | 135 | } |
robertspil | 0:32193823db59 | 136 | if(UART_counter==8) { // control bit |
robertspil | 0:32193823db59 | 137 | textchar = pinread; |
robertspil | 0:32193823db59 | 138 | UART_parity +=pinread; |
robertspil | 0:32193823db59 | 139 | return; |
robertspil | 0:32193823db59 | 140 | } |
robertspil | 0:32193823db59 | 141 | if(UART_counter==9) { //parity |
robertspil | 0:32193823db59 | 142 | UART_parity +=pinread; |
robertspil | 0:32193823db59 | 143 | if(UART_parity%2 ==0) { //UART_errorchar=true; |
robertspil | 0:32193823db59 | 144 | UART_errorchar=true; |
robertspil | 0:32193823db59 | 145 | } |
robertspil | 0:32193823db59 | 146 | return; |
robertspil | 0:32193823db59 | 147 | } |
robertspil | 0:32193823db59 | 148 | if(UART_counter==10 || UART_counter==11) { //stop bits |
robertspil | 0:32193823db59 | 149 | if(pinread==0) { |
robertspil | 0:32193823db59 | 150 | UART_errorchar=true; |
robertspil | 0:32193823db59 | 151 | } |
robertspil | 0:32193823db59 | 152 | return; |
robertspil | 0:32193823db59 | 153 | } |
robertspil | 0:32193823db59 | 154 | //the bits of one UART character are received |
robertspil | 0:32193823db59 | 155 | UART_Tx_start =25 ; //this start the Tx within 25 msec//restart the TX message after 25 msec |
robertspil | 0:32193823db59 | 156 | |
robertspil | 0:32193823db59 | 157 | if(UART_errorchar || textchar) { |
robertspil | 0:32193823db59 | 158 | //rc.jetiptr->stat.UART_err ++; |
robertspil | 0:32193823db59 | 159 | //rc.jetiptr->status=0; |
robertspil | 0:32193823db59 | 160 | return; //do not process a incorrect answer |
robertspil | 0:32193823db59 | 161 | } |
robertspil | 0:32193823db59 | 162 | UART_isanswer=true; |
robertspil | 0:32193823db59 | 163 | rc.jetiptr->buttons= UART_data; |
robertspil | 0:32193823db59 | 164 | } |
robertspil | 0:32193823db59 | 165 | |
robertspil | 0:32193823db59 | 166 | |
robertspil | 0:32193823db59 | 167 | void ISRdataBitTx() { //nextbit timeout in Tx mode |
robertspil | 0:32193823db59 | 168 | int txpin =0; |
robertspil | 0:32193823db59 | 169 | if(UART_counter== -1) { //start a new char UART_data with the correct textchar |
robertspil | 0:32193823db59 | 170 | switch(UART_pos) { // UART_data = next char to transmit |
robertspil | 0:32193823db59 | 171 | case 0:// start TX message |
robertspil | 0:32193823db59 | 172 | UART_startBit.fall(NULL); |
robertspil | 0:32193823db59 | 173 | UART_pin.output(); |
robertspil | 0:32193823db59 | 174 | UART_pin=0; //begin the start bit |
robertspil | 0:32193823db59 | 175 | UART_data = 0xFE; |
robertspil | 0:32193823db59 | 176 | textchar = false; |
robertspil | 0:32193823db59 | 177 | UART_pos++; |
robertspil | 0:32193823db59 | 178 | break; |
robertspil | 0:32193823db59 | 179 | case 33: // send end byte with 9.bit=0 |
robertspil | 0:32193823db59 | 180 | UART_data = 0xFF; |
robertspil | 0:32193823db59 | 181 | textchar = false; |
robertspil | 0:32193823db59 | 182 | UART_pos++; |
robertspil | 0:32193823db59 | 183 | break; |
robertspil | 0:32193823db59 | 184 | case 34: // all data sent |
robertspil | 0:32193823db59 | 185 | // listen to RX |
robertspil | 0:32193823db59 | 186 | UART_pin.input(); |
robertspil | 0:32193823db59 | 187 | UART_pin.mode(PullUp); |
robertspil | 0:32193823db59 | 188 | //UART_pin.mode(PullNone); |
robertspil | 0:32193823db59 | 189 | UART_startBit.fall(& ISRstartBit);//detect the next received start bit |
robertspil | 0:32193823db59 | 190 | UART_Tx_start =25 ; //this start the Tx within 25 msec//restart the TX message after 25 msec |
robertspil | 0:32193823db59 | 191 | return; |
robertspil | 0:32193823db59 | 192 | case 100: |
robertspil | 0:32193823db59 | 193 | UART_data = 0x7E; |
robertspil | 0:32193823db59 | 194 | textchar = false; |
robertspil | 0:32193823db59 | 195 | UART_pos++; |
robertspil | 0:32193823db59 | 196 | break; |
robertspil | 0:32193823db59 | 197 | case 101: |
robertspil | 0:32193823db59 | 198 | UART_data = 0x01; |
robertspil | 0:32193823db59 | 199 | textchar = true; |
robertspil | 0:32193823db59 | 200 | UART_pos++; |
robertspil | 0:32193823db59 | 201 | break; |
robertspil | 0:32193823db59 | 202 | case 102: |
robertspil | 0:32193823db59 | 203 | UART_data = 0x31; |
robertspil | 0:32193823db59 | 204 | textchar = true; |
robertspil | 0:32193823db59 | 205 | UART_pos=34; |
robertspil | 0:32193823db59 | 206 | break; |
robertspil | 0:32193823db59 | 207 | |
robertspil | 0:32193823db59 | 208 | default: // set 9.bit=1 text message |
robertspil | 0:32193823db59 | 209 | textchar = true; |
robertspil | 0:32193823db59 | 210 | UART_data = JETI_buffer[UART_pos-1]; // send byte from LCD buffer |
robertspil | 0:32193823db59 | 211 | UART_pos++; // increment to next byte |
robertspil | 0:32193823db59 | 212 | } |
robertspil | 0:32193823db59 | 213 | //the start bit |
robertspil | 0:32193823db59 | 214 | txpin=0; |
robertspil | 0:32193823db59 | 215 | UART_parity=0; |
robertspil | 0:32193823db59 | 216 | } |
robertspil | 0:32193823db59 | 217 | // write each bit, beginning with LSB |
robertspil | 0:32193823db59 | 218 | if(UART_counter >=0 && UART_counter<8) { //data bits 0..7 |
robertspil | 0:32193823db59 | 219 | int out = UART_data & 0x01; |
robertspil | 0:32193823db59 | 220 | txpin=out; |
robertspil | 0:32193823db59 | 221 | UART_parity += out; |
robertspil | 0:32193823db59 | 222 | UART_data = (UART_data >>1); |
robertspil | 0:32193823db59 | 223 | } |
robertspil | 0:32193823db59 | 224 | if(UART_counter==8) { //control bit |
robertspil | 0:32193823db59 | 225 | if(textchar) |
robertspil | 0:32193823db59 | 226 | txpin =1; |
robertspil | 0:32193823db59 | 227 | else |
robertspil | 0:32193823db59 | 228 | txpin=0; |
robertspil | 0:32193823db59 | 229 | UART_parity += txpin; |
robertspil | 0:32193823db59 | 230 | |
robertspil | 0:32193823db59 | 231 | } |
robertspil | 0:32193823db59 | 232 | if(UART_counter==9) { //parity bit |
robertspil | 0:32193823db59 | 233 | UART_parity++; |
robertspil | 0:32193823db59 | 234 | txpin= UART_parity%2; |
robertspil | 0:32193823db59 | 235 | } |
robertspil | 0:32193823db59 | 236 | if(UART_counter<10) { |
robertspil | 0:32193823db59 | 237 | UART_nextBit.attach_us(&ISRdataBitTx , UART_W1); //next tx bit |
robertspil | 0:32193823db59 | 238 | UART_counter++; |
robertspil | 0:32193823db59 | 239 | } else { //stop bits |
robertspil | 0:32193823db59 | 240 | UART_counter=-1; //to start the next char |
robertspil | 0:32193823db59 | 241 | txpin=1; // stop bits |
robertspil | 0:32193823db59 | 242 | UART_nextBit.attach_us(&ISRdataBitTx , 2*UART_W1); //2 stop bits |
robertspil | 0:32193823db59 | 243 | } |
robertspil | 0:32193823db59 | 244 | UART_pin.write(txpin); |
robertspil | 0:32193823db59 | 245 | } |
robertspil | 0:32193823db59 | 246 | //----communication |
robertspil | 0:32193823db59 | 247 | JetiC::JetiC() { //start the interface with the Jeti transmitter module |
robertspil | 0:32193823db59 | 248 | for(int i=0; i<32; i++) { |
robertspil | 0:32193823db59 | 249 | JETI_buffer[i]=0x20 ; |
robertspil | 0:32193823db59 | 250 | } |
robertspil | 0:32193823db59 | 251 | JETI_buffer[0] ='S'; |
robertspil | 0:32193823db59 | 252 | JETI_buffer[1] ='E'; |
robertspil | 0:32193823db59 | 253 | JETI_buffer[2] ='N'; |
robertspil | 0:32193823db59 | 254 | JETI_buffer[3] ='S'; |
robertspil | 0:32193823db59 | 255 | JETI_buffer[4] ='O'; |
robertspil | 0:32193823db59 | 256 | JETI_buffer[5] ='R'; |
robertspil | 0:32193823db59 | 257 | |
robertspil | 0:32193823db59 | 258 | JETI_buffer[32]='\0'; |
robertspil | 0:32193823db59 | 259 | |
robertspil | 0:32193823db59 | 260 | UART_ticker.attach_us(&UART_tickerAdd, 1000); |
robertspil | 0:32193823db59 | 261 | UART_Tx_start =0; //this start the Tx |
robertspil | 0:32193823db59 | 262 | } |
robertspil | 0:32193823db59 | 263 | bool JetiC::isAnswer() { |
robertspil | 0:32193823db59 | 264 | if(!UART_isanswer) |
robertspil | 0:32193823db59 | 265 | return false; |
robertspil | 0:32193823db59 | 266 | //message received |
robertspil | 0:32193823db59 | 267 | UART_isanswer = false; |
robertspil | 0:32193823db59 | 268 | UART_Tx_start =25 ; //this start the Tx within 25 msec |
robertspil | 0:32193823db59 | 269 | return true; //return true only one time |
robertspil | 0:32193823db59 | 270 | } |
robertspil | 0:32193823db59 | 271 | //----//various utility functions to fill the buffer without the big "printf", too big for the memeory----------------------------- |
robertspil | 0:32193823db59 | 272 | void JetiC::clear() { //fill with spaces |
robertspil | 0:32193823db59 | 273 | pos=0; |
robertspil | 0:32193823db59 | 274 | for(int i=0; i<32; i++) |
robertspil | 0:32193823db59 | 275 | JETI_buffer[i]=' '; |
robertspil | 0:32193823db59 | 276 | } |
robertspil | 0:32193823db59 | 277 | void JetiC::setPos(int x) { |
robertspil | 0:32193823db59 | 278 | if((x>=0) && (x<32)) |
robertspil | 0:32193823db59 | 279 | pos=x; |
robertspil | 0:32193823db59 | 280 | } |
robertspil | 0:32193823db59 | 281 | |
robertspil | 0:32193823db59 | 282 | void JetiC::print(char c) { |
robertspil | 0:32193823db59 | 283 | JETI_buffer[pos] = c; |
robertspil | 0:32193823db59 | 284 | if(pos< 31) |
robertspil | 0:32193823db59 | 285 | pos++; |
robertspil | 0:32193823db59 | 286 | } |
robertspil | 0:32193823db59 | 287 | void JetiC::print(long n , uint8_t len) { |
robertspil | 0:32193823db59 | 288 | printNumber(n, 0,len); |
robertspil | 0:32193823db59 | 289 | } |
robertspil | 0:32193823db59 | 290 | void JetiC::printf(float value, uint8_t p , uint8_t len) { //value ,decimal point , length |
robertspil | 0:32193823db59 | 291 | int valint= value * pow(10.0 , p) +0.5 ;//convert the float in fixed point |
robertspil | 0:32193823db59 | 292 | printNumber((int) valint, p, len); //print the numbers |
robertspil | 0:32193823db59 | 293 | } |
robertspil | 0:32193823db59 | 294 | |
robertspil | 0:32193823db59 | 295 | void JetiC::printNumber(int valint, uint8_t decimal,uint8_t len) { //decimal ==0 => no decimal point decimal ==2 => string as 123.45 |
robertspil | 0:32193823db59 | 296 | int i=0; |
robertspil | 0:32193823db59 | 297 | int sign = 1; |
robertspil | 0:32193823db59 | 298 | if(valint<0) { |
robertspil | 0:32193823db59 | 299 | sign=0; |
robertspil | 0:32193823db59 | 300 | valint=-valint; |
robertspil | 0:32193823db59 | 301 | } |
robertspil | 0:32193823db59 | 302 | while(valint > 0 || (decimal>0 &&i<decimal+2) ||(decimal==0 && i<1)) { |
robertspil | 0:32193823db59 | 303 | if(i>0 && i==decimal) { //insert adecimal point |
robertspil | 0:32193823db59 | 304 | JETI_buffer[pos +len-i -1] = '.'; |
robertspil | 0:32193823db59 | 305 | } else { |
robertspil | 0:32193823db59 | 306 | JETI_buffer[pos +len-i -1] = (char)(((int)'0')+(valint % 10)); |
robertspil | 0:32193823db59 | 307 | valint /= 10; |
robertspil | 0:32193823db59 | 308 | } |
robertspil | 0:32193823db59 | 309 | i++; |
robertspil | 0:32193823db59 | 310 | if(i>len-2) |
robertspil | 0:32193823db59 | 311 | break; |
robertspil | 0:32193823db59 | 312 | } |
robertspil | 0:32193823db59 | 313 | if(sign==0) |
robertspil | 0:32193823db59 | 314 | JETI_buffer[pos +len-i -1] = '-'; |
robertspil | 0:32193823db59 | 315 | else |
robertspil | 0:32193823db59 | 316 | JETI_buffer[pos +len-i -1] = '+'; |
robertspil | 0:32193823db59 | 317 | |
robertspil | 0:32193823db59 | 318 | pos+= len; |
robertspil | 0:32193823db59 | 319 | } |
robertspil | 0:32193823db59 | 320 | void JetiC::print_p(const char *s) { |
robertspil | 0:32193823db59 | 321 | char c; |
robertspil | 0:32193823db59 | 322 | while((c = (*s)) !='\0') { |
robertspil | 0:32193823db59 | 323 | s++; |
robertspil | 0:32193823db59 | 324 | print(c); |
robertspil | 0:32193823db59 | 325 | } |
robertspil | 0:32193823db59 | 326 | } |
robertspil | 0:32193823db59 | 327 | //--------debug the jeti protocol |
robertspil | 0:32193823db59 | 328 | void JetiC::debug_display() { |
robertspil | 0:32193823db59 | 329 | clear(); |
robertspil | 0:32193823db59 | 330 | print_p(" JetiDebug"); //line1 |
robertspil | 0:32193823db59 | 331 | setPos(16); |
robertspil | 0:32193823db59 | 332 | if(buttons== B_RIGHT) |
robertspil | 0:32193823db59 | 333 | print_p(" B_RIGHT"); |
robertspil | 0:32193823db59 | 334 | if(buttons== B_DOWN) |
robertspil | 0:32193823db59 | 335 | print_p(" B_DOWN"); |
robertspil | 0:32193823db59 | 336 | if(buttons== B_UP) |
robertspil | 0:32193823db59 | 337 | print_p(" B_UP"); |
robertspil | 0:32193823db59 | 338 | if(buttons== B_LEFT) |
robertspil | 0:32193823db59 | 339 | print_p(" B_LEFT"); |
robertspil | 0:32193823db59 | 340 | return; |
robertspil | 0:32193823db59 | 341 | |
robertspil | 0:32193823db59 | 342 | } |