|
| 1 | +/* |
| 2 | +Library for Bafang BBS01/BBS02 Displays (C965...) |
| 3 | +
|
| 4 | +Copyright © 2016 Jens Kießling ([email protected]) |
| 5 | +inspired by Kingmeter Library (Michael Fabry) |
| 6 | +
|
| 7 | +This program is free software: you can redistribute it and/or modify |
| 8 | +it under the terms of the GNU General Public License as published by |
| 9 | +the Free Software Foundation, either version 3 of the License, or |
| 10 | +(at your option) any later version. |
| 11 | +
|
| 12 | +This program is distributed in the hope that it will be useful, |
| 13 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | +GNU General Public License for more details. |
| 16 | +
|
| 17 | +You should have received a copy of the GNU General Public License |
| 18 | +along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | +*/ |
| 20 | + |
| 21 | + |
| 22 | +// Includes |
| 23 | +#include "config.h" |
| 24 | +#include "display_bafang.h" |
| 25 | +#include "globals.h" |
| 26 | + |
| 27 | +#if (DISPLAY_TYPE & DISPLAY_TYPE_BAFANG) |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | +// Definitions |
| 32 | +#define RXSTATE_STARTCODE 0 |
| 33 | +#define RXSTATE_MSGBODY 1 |
| 34 | +#define RXSTATE_DONE 2 |
| 35 | + |
| 36 | + |
| 37 | +// Local function prototypes |
| 38 | +static void BAFANG_Service(BAFANG_t* BF_ctx); |
| 39 | + |
| 40 | +uint8_t TxBuff[BF_MAX_TXBUFF]; |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | +/* Public functions (Prototypes declared by display_bafang.h) */ |
| 45 | + |
| 46 | +/**************************************************************************************************** |
| 47 | + * Bafang_Init() - Initializes the display object |
| 48 | + * |
| 49 | + ****************************************************************************************************/ |
| 50 | +#if HARDWARE_REV < 20 |
| 51 | +void Bafang_Init (BAFANG_t* BF_ctx, SoftwareSerial* DisplaySerial) |
| 52 | +#else |
| 53 | +void Bafang_Init (BAFANG_t* BF_ctx, HardwareSerial* DisplaySerial) |
| 54 | +#endif |
| 55 | +{ |
| 56 | + uint8_t i; |
| 57 | + BF_ctx->SerialPort = DisplaySerial; // Store serial port to use |
| 58 | + |
| 59 | + BF_ctx->RxState = RXSTATE_STARTCODE; |
| 60 | + BF_ctx->LastRx = millis(); |
| 61 | + |
| 62 | + for(i=0; i<BF_MAX_RXBUFF; i++) |
| 63 | + { |
| 64 | + BF_ctx->RxBuff[i] = 0x00; |
| 65 | + } |
| 66 | + |
| 67 | + DisplaySerial->begin(1200); |
| 68 | +} |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | +/**************************************************************************************************** |
| 73 | + * Bafang_Service() - Communicates data from and to the display |
| 74 | + * |
| 75 | + ***************************************************************************************************/ |
| 76 | +void Bafang_Service(BAFANG_t* BF_ctx) |
| 77 | +{ |
| 78 | + uint8_t i; |
| 79 | + // Search for Start Code |
| 80 | + if(BF_ctx->RxState == RXSTATE_STARTCODE) //waiting for start code |
| 81 | + { |
| 82 | + if (millis()-BF_ctx->LastRx>BF_DISPLAYTIMEOUT) //new transmission frame will come |
| 83 | + if(BF_ctx->SerialPort->available()) |
| 84 | + { |
| 85 | + BF_ctx->LastRx = millis(); |
| 86 | + BF_ctx->RxBuff[0]=BF_ctx->SerialPort->read(); |
| 87 | + if(BF_ctx->RxBuff[0]==BF_CMD_STARTREQUEST||BF_ctx->RxBuff[0]==BF_CMD_STARTINFO) //valid startcode detected |
| 88 | + { |
| 89 | + BF_ctx->RxCnt = 1; |
| 90 | + BF_ctx->RxState = RXSTATE_MSGBODY; |
| 91 | + } |
| 92 | + else |
| 93 | + { |
| 94 | + return; // No need to continue |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + // Receive Message body |
| 100 | + if(BF_ctx->RxState == RXSTATE_MSGBODY) |
| 101 | + { |
| 102 | + while(BF_ctx->SerialPort->available()) |
| 103 | + { |
| 104 | + BF_ctx->RxBuff[BF_ctx->RxCnt] = BF_ctx->SerialPort->read(); |
| 105 | + BF_ctx->RxCnt++; |
| 106 | + if(BF_ctx->RxCnt > 5) // something is wrong, reset |
| 107 | + { |
| 108 | + BF_ctx->RxState = RXSTATE_STARTCODE; |
| 109 | + BF_ctx->LastRx = millis(); |
| 110 | + break; |
| 111 | + } |
| 112 | + BF_ctx->LastRx = millis(); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + if ((millis()-BF_ctx->LastRx)>BF_DISPLAYTIMEOUT&&(BF_ctx->RxState == RXSTATE_MSGBODY)) //new message has been received -> analyze |
| 117 | + { |
| 118 | + BF_ctx->RxState = RXSTATE_DONE; |
| 119 | + } |
| 120 | + |
| 121 | + // Message received completely, analyze |
| 122 | + if(BF_ctx->RxState == RXSTATE_DONE) |
| 123 | + { |
| 124 | + BF_ctx->RxState = RXSTATE_STARTCODE; |
| 125 | + if (BF_ctx->RxBuff[0]==BF_CMD_STARTREQUEST) //display wants an answer, send it! |
| 126 | + { |
| 127 | + switch (BF_ctx->RxBuff[1]) |
| 128 | + { |
| 129 | + case BF_CMD_GETSPEED: |
| 130 | + TxBuff[0]=0; |
| 131 | + TxBuff[1]=min(spd/0.128,255); |
| 132 | + TxBuff[2]=TxBuff[1]+32; |
| 133 | + BF_sendmessage(BF_ctx,3); |
| 134 | + break; |
| 135 | + |
| 136 | + case BF_CMD_GETERROR: |
| 137 | + TxBuff[0]=1; |
| 138 | + BF_sendmessage(BF_ctx,1); |
| 139 | + break; |
| 140 | + |
| 141 | + case BF_CMD_GETBAT: |
| 142 | + TxBuff[0]=battery_percent_fromcapacity; |
| 143 | + TxBuff[1]=battery_percent_fromcapacity; |
| 144 | + BF_sendmessage(BF_ctx,2); |
| 145 | + |
| 146 | + case BF_CMD_GET1: |
| 147 | + TxBuff[0]=0; |
| 148 | + TxBuff[1]=0; |
| 149 | + BF_sendmessage(BF_ctx,2); |
| 150 | + break; |
| 151 | + |
| 152 | + case BF_CMD_GET2: |
| 153 | + TxBuff[0]=48; |
| 154 | + TxBuff[1]=48; |
| 155 | + BF_sendmessage(BF_ctx,2); |
| 156 | + break; |
| 157 | + } |
| 158 | + |
| 159 | + } |
| 160 | + else if(BF_ctx->RxBuff[0]==BF_CMD_STARTINFO) |
| 161 | + { |
| 162 | + switch (BF_ctx->RxBuff[1]) |
| 163 | + { |
| 164 | + case BF_CMD_LEVEL: |
| 165 | + if (BF_ctx->RxBuff[3]==BF_ctx->RxBuff[0]+BF_ctx->RxBuff[1]+BF_ctx->RxBuff[2]) //checksum is correct, set poti_stat |
| 166 | + { |
| 167 | + BF_ctx->Rx.PushAssist=0; |
| 168 | + switch(BF_ctx->RxBuff[2]) |
| 169 | + { |
| 170 | + case BF_LEVEL0: |
| 171 | + BF_ctx->Rx.AssistLevel=0; |
| 172 | + break; |
| 173 | + case BF_LEVEL1: |
| 174 | + BF_ctx->Rx.AssistLevel=1; |
| 175 | + break; |
| 176 | + case BF_LEVEL2: |
| 177 | + BF_ctx->Rx.AssistLevel=2; |
| 178 | + break; |
| 179 | + case BF_LEVEL3: |
| 180 | + BF_ctx->Rx.AssistLevel=3; |
| 181 | + break; |
| 182 | + case BF_LEVEL4: |
| 183 | + BF_ctx->Rx.AssistLevel=4; |
| 184 | + break; |
| 185 | + case BF_LEVEL5: |
| 186 | + BF_ctx->Rx.AssistLevel=5; |
| 187 | + break; |
| 188 | + case BF_LEVEL6: |
| 189 | + BF_ctx->Rx.AssistLevel=6; |
| 190 | + break; |
| 191 | + case BF_LEVEL7: |
| 192 | + BF_ctx->Rx.AssistLevel=7; |
| 193 | + break; |
| 194 | + case BF_LEVEL8: |
| 195 | + BF_ctx->Rx.AssistLevel=8; |
| 196 | + break; |
| 197 | + case BF_LEVEL9: |
| 198 | + BF_ctx->Rx.AssistLevel=9; |
| 199 | + break; |
| 200 | + case BF_PUSHASSIST: |
| 201 | + BF_ctx->Rx.PushAssist=1; |
| 202 | + break; |
| 203 | + } |
| 204 | + } |
| 205 | + break; |
| 206 | + |
| 207 | + case BF_CMD_MISC: |
| 208 | + BF_ctx->Rx.Headlight=(BF_ctx->RxBuff[2]==BF_LIGHTON); |
| 209 | + break; |
| 210 | + } |
| 211 | + } |
| 212 | + } |
| 213 | +} |
| 214 | + |
| 215 | +void BF_sendmessage(BAFANG_t* BF_ctx,uint8_t count) |
| 216 | +{ |
| 217 | + for(int i=0; i<count; i++) |
| 218 | + BF_ctx->SerialPort->write(TxBuff[i]); |
| 219 | +} |
| 220 | + |
| 221 | + |
| 222 | + |
| 223 | +#endif // (DISPLAY_TYPE & DISPLAY_TYPE_BAFANG) |
0 commit comments