BQ28Z610 Driver
BQ28Z610 Battery fuel gauge device driver for Arduino
|
Util functions implementation. More...
#include "utils.h"
Functions | |
bool | _isRequestMinAllowed (int len) |
bool | _isAllowedRequestSize (int len) |
bool | isAllowedRequestPayloadSize (int len) |
int | sendCommand (byte command) |
int | sendCommand (byte reg, word command) |
int | sendData (byte reg, byte *data, int len) |
int | requestBlock (byte *buf) |
byte | requestByte () |
int | requestBytes (byte *buf, int len) |
word | requestWord () |
byte | checksum (byte *data, byte len) |
bool | validate (byte *data) |
word | composeWord (byte *buf, int lsbIndex, bool littleEndian) |
u32 | composeDoubleWord (byte *buf) |
u32 | composeValue (byte *buf, int from, int till) |
String | stringFromProgmem (PGM_P stringPtr) |
void | printLongHex (u32 val, bool newLine) |
void | printWordHex (word val, bool newLine) |
void | printByteHex (byte val, bool newLine) |
void | printBytesHex (byte *buf, int len) |
void | printBin (u32 val, int n, bool newLine) |
void | printLongSplitBin (u32 val) |
void | printLongBin (u32 val, bool newLine) |
void | printWordBin (word val, bool newLine) |
void | printByteBin (byte val, bool newLine) |
void | __printPgm (PGM_P pgmPtr) |
void | __printlnPgm (PGM_P pgmPtr) |
void | __printCaption (PGM_P caption) |
void | __printUnits (PGM_P units, bool newLine) |
void | printInteger (int value, PGM_P units, bool newLine) |
void | printInteger (PGM_P caption, int value, PGM_P units, bool newLine) |
void | printInteger (PGM_P caption, int value, bool newLine) |
void | printFloat (float value, int format, PGM_P units, bool newLine) |
void | printFloat (PGM_P caption, float value, int format, PGM_P units) |
void | printFloat (PGM_P caption, float value, int format, PGM_P(*unitsFn)()) |
void | printPremil (PGM_P caption, int value, PGM_P units) |
void | printPremil (PGM_P caption, int value, PGM_P(*unitsFn)()) |
void | printFlag (PGM_P caption, u32 flags, int n) |
void | printFlag (PGM_P caption, u32 flags, Flag(*flagFn)()) |
void | printFlag (PGM_P caption, u32 flags, Flag flag) |
void | printFlag (u32 flags, Flag flag) |
void | printWordHex (PGM_P caption, word val, bool newLine) |
Util functions implementation.
MIT License
Copyright (c) 2024 Oleksii Sylichenko
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
bool _isAllowedRequestSize | ( | int | len | ) |
Check whether length is greater than 0 and lower that 32. This check is useful before single request, because the device cannot response more that 32 bytes at once.
bool _isRequestMinAllowed | ( | int | len | ) |
Check whether length is greater than 0.
byte checksum | ( | byte * | data, |
byte | len | ||
) |
The checksum is the bitwise inversion of the sum of bytes.
For example:
~(0x35 + 0x00 + 0x23 + 0x01 + 0x67 + 0x45 + 0xAB + 0x89 + 0xEF + 0xCD) = ~(0xF5) = ~(0b11110101) = 0b00001010 = 0x0A
u32 composeDoubleWord | ( | byte * | buf | ) |
Returns the Unsigned integer 32-bit value of 4 bytes from the buffer in Little Endian format.
buf | - data array |
a | - index of the least significant byte; bits: [0..7] |
b | - index of the most significant byte |
u32 composeValue | ( | byte * | buf, |
int | from, | ||
int | till | ||
) |
Compose value from the bytes of the buffer in Little Endian format.
buf | - data array |
from | - index of the least significant byte; bits: [0..7] |
till | - index of the most significant byte |
word composeWord | ( | byte * | buf, |
int | lsbIndex, | ||
bool | littleEndian | ||
) |
Compose the word value from the two bytes of the buffer.
buf | - data array |
lowerByteIndex | - Index of the byte that should be placed in the lower position of the result. |
littleEndian | - Ending indicator:
|
bool isAllowedRequestPayloadSize | ( | int | len | ) |
Check whether length is greater than 0 and lower that 32.
Max Data length in the Block Protocol is 32.
void printBin | ( | u32 | val, |
int | n, | ||
bool | newLine | ||
) |
Print value in binary format with leading zeros.
void printBytesHex | ( | byte * | buf, |
int | len | ||
) |
Print content of the byte array in one line in hex format with leading zeros if necessary. [ AA BB CC DD ... ]
void printFlag | ( | PGM_P | caption, |
u32 | flags, | ||
Flag(*)() | flagFn | ||
) |
void printFlag | ( | PGM_P | caption, |
u32 | flags, | ||
int | n | ||
) |
Print the Flag number with the caption.
void printFlag | ( | u32 | flags, |
Flag | flag | ||
) |
void printInteger | ( | int | value, |
PGM_P | units, | ||
bool | newLine = false |
||
) |
Print in format: "value units"
void printInteger | ( | PGM_P | caption, |
int | value, | ||
bool | newLine = true |
||
) |
Print in format: "Caption: value"
void printInteger | ( | PGM_P | caption, |
int | value, | ||
PGM_P | units, | ||
bool | newLine = true |
||
) |
Print in format: "Caption: value units"
void printLongHex | ( | u32 | val, |
bool | newLine | ||
) |
Print value in the HEX format with leading 0x and zero if necessary.
void printLongSplitBin | ( | u32 | val | ) |
Print long value bits (32) in two lines: 31 - 16 15 - 0
void printPremil | ( | PGM_P | caption, |
int | value, | ||
PGM_P | units | ||
) |
Print the integer value as a float divided by 1000 with 3 decimal places in format: "Caption: 65.536"
int requestBlock | ( | byte * | buf | ) |
Request the device for 36 bytes per multiple requests using the Block Protocol.
int requestBytes | ( | byte * | buf, |
int | len | ||
) |
Request the device for len bytes per single request. Length of the requested data must not be greater than 32, because it cannot response more.
word requestWord | ( | ) |
Read word from the Device in Little Endian and return as normal word.
If response: [0xaa, 0xAA] then return: 0xAAaa
int sendCommand | ( | byte | reg, |
word | command | ||
) |
Sending word command in Little Endian to the register.
Bytes of command should be in normal ordering. Ordering will be changed to Little Endian by the function.
0x4321 to REG:
write: [ REG, 0x21, 0x43 ]
int sendData | ( | byte | reg, |
byte * | data, | ||
int | len | ||
) |
Send byte array data with specified length into specified register.
Order of the bytes in the data array should be prepared to sending.
The length should not be greater than 32 and less than 1.
String stringFromProgmem | ( | PGM_P | stringPtr | ) |
Read string from PROGMEM and return as String in RAM.
bool validate | ( | byte * | data | ) |
Validate the data via checksum: the sum of the data bytes and the checksum should equal a full byte. Length of the data array according to the Block Protocol should be equal 36. The last two bytes should represent the checksum and the total length.