11#define TwoWire TwoWireInternal
2- #define Wire WireInternal
3- #define Wire1 WireInternal1
2+ #define Wire WireInternal
3+ #define Wire1 WireInternal1
44
55#include " pins_arduino.h"
66#include " ../../libraries/Wire/src/Wire.h"
@@ -10,110 +10,110 @@ static bool wireInitialized = false;
1010
1111// From https://www.diodes.com/datasheet/download/PI4IOE5V6408.pdf
1212static void writeRegister (uint8_t address, uint8_t reg, uint8_t value) {
13- WireInternal.beginTransmission (address);
14- WireInternal.write (reg);
15- WireInternal.write (value);
16- WireInternal.endTransmission ();
13+ WireInternal.beginTransmission (address);
14+ WireInternal.write (reg);
15+ WireInternal.write (value);
16+ WireInternal.endTransmission ();
1717}
1818
1919static uint8_t readRegister (uint8_t address, uint8_t reg) {
20- WireInternal.beginTransmission (address);
21- WireInternal.write (reg);
22- WireInternal.endTransmission (false );
23- WireInternal.requestFrom (address, 1 );
24- return WireInternal.read ();
20+ WireInternal.beginTransmission (address);
21+ WireInternal.write (reg);
22+ WireInternal.endTransmission (false );
23+ WireInternal.requestFrom (address, 1 );
24+ return WireInternal.read ();
2525}
2626
2727static void writeBitRegister (uint8_t address, uint8_t reg, uint8_t bit, uint8_t value) {
28- uint8_t val = readRegister (address, reg);
29- if (value) {
30- writeRegister (address, reg, val | (1 << bit));
31- } else {
32- writeRegister (address, reg, val & ~(1 << bit));
33- }
28+ uint8_t val = readRegister (address, reg);
29+ if (value) {
30+ writeRegister (address, reg, val | (1 << bit));
31+ } else {
32+ writeRegister (address, reg, val & ~(1 << bit));
33+ }
3434}
3535
3636static bool readBitRegister (uint8_t address, uint8_t reg, uint8_t bit) {
37- uint8_t val = readRegister (address, reg);
38- return ((val & (1 << bit)) > 0 );
37+ uint8_t val = readRegister (address, reg);
38+ return ((val & (1 << bit)) > 0 );
3939}
4040
4141void pinMode (ExpanderPin pin, uint8_t mode) {
42- if (!wireInitialized) {
43- WireInternal.begin (SDA, SCL);
44- wireInitialized = true ;
45- // reset all registers to default state
46- writeRegister (pin.address , 0x1 , 0x1 );
47- // set all pins as high as default state
48- writeRegister (pin.address , 0x9 , 0xFF );
49- // interrupt mask to all pins
50- writeRegister (pin.address , 0x11 , 0xFF );
51- // all input
52- writeRegister (pin.address , 0x3 , 0 );
53- }
54- writeBitRegister (pin.address , 0x3 , pin.pin , mode == OUTPUT);
55- if (mode == OUTPUT) {
56- // remove high impedence
57- writeBitRegister (pin.address , 0x7 , pin.pin , false );
58- } else if (mode == INPUT_PULLUP) {
59- // set pull-up resistor
60- writeBitRegister (pin.address , 0xB , pin.pin , true );
61- writeBitRegister (pin.address , 0xD , pin.pin , true );
62- } else if (mode == INPUT_PULLDOWN) {
63- // disable pull-up resistor
64- writeBitRegister (pin.address , 0xB , pin.pin , true );
65- writeBitRegister (pin.address , 0xD , pin.pin , false );
66- } else if (mode == INPUT) {
67- // disable pull selector resistor
68- writeBitRegister (pin.address , 0xB , pin.pin , false );
69- }
42+ if (!wireInitialized) {
43+ WireInternal.begin (SDA, SCL);
44+ wireInitialized = true ;
45+ // reset all registers to default state
46+ writeRegister (pin.address , 0x1 , 0x1 );
47+ // set all pins as high as default state
48+ writeRegister (pin.address , 0x9 , 0xFF );
49+ // interrupt mask to all pins
50+ writeRegister (pin.address , 0x11 , 0xFF );
51+ // all input
52+ writeRegister (pin.address , 0x3 , 0 );
53+ }
54+ writeBitRegister (pin.address , 0x3 , pin.pin , mode == OUTPUT);
55+ if (mode == OUTPUT) {
56+ // remove high impedence
57+ writeBitRegister (pin.address , 0x7 , pin.pin , false );
58+ } else if (mode == INPUT_PULLUP) {
59+ // set pull-up resistor
60+ writeBitRegister (pin.address , 0xB , pin.pin , true );
61+ writeBitRegister (pin.address , 0xD , pin.pin , true );
62+ } else if (mode == INPUT_PULLDOWN) {
63+ // disable pull-up resistor
64+ writeBitRegister (pin.address , 0xB , pin.pin , true );
65+ writeBitRegister (pin.address , 0xD , pin.pin , false );
66+ } else if (mode == INPUT) {
67+ // disable pull selector resistor
68+ writeBitRegister (pin.address , 0xB , pin.pin , false );
69+ }
7070}
7171
7272void digitalWrite (ExpanderPin pin, uint8_t val) {
73- if (!wireInitialized) {
74- WireInternal.begin (SDA, SCL);
75- wireInitialized = true ;
76- }
77- writeBitRegister (pin.address , 0x5 , pin.pin , val == HIGH);
73+ if (!wireInitialized) {
74+ WireInternal.begin (SDA, SCL);
75+ wireInitialized = true ;
76+ }
77+ writeBitRegister (pin.address , 0x5 , pin.pin , val == HIGH);
7878}
7979
8080int digitalRead (ExpanderPin pin) {
81- if (!wireInitialized) {
82- WireInternal.begin (SDA, SCL);
83- wireInitialized = true ;
84- }
85- return readBitRegister (pin.address , 0xF , pin.pin );
81+ if (!wireInitialized) {
82+ WireInternal.begin (SDA, SCL);
83+ wireInitialized = true ;
84+ }
85+ return readBitRegister (pin.address , 0xF , pin.pin );
8686}
8787
8888void NessoBattery::enableCharge () {
89- // AW32001E - address 0x49
90- // set CEB bit low (charge enable)
91- if (!wireInitialized) {
92- WireInternal.begin (SDA, SCL);
93- wireInitialized = true ;
94- }
95- writeBitRegister (0x49 , 0x1 , 3 , false );
89+ // AW32001E - address 0x49
90+ // set CEB bit low (charge enable)
91+ if (!wireInitialized) {
92+ WireInternal.begin (SDA, SCL);
93+ wireInitialized = true ;
94+ }
95+ writeBitRegister (0x49 , 0x1 , 3 , false );
9696}
9797
9898float NessoBattery::getVoltage () {
99- // BQ27220 - address 0x55
100- if (!wireInitialized) {
101- WireInternal.begin (SDA, SCL);
102- wireInitialized = true ;
103- }
104- uint16_t voltage = (readRegister (0x55 , 0x9 ) << 8 ) | readRegister (0x55 , 0x8 );
105- return (float )voltage / 1000 .0f ;
99+ // BQ27220 - address 0x55
100+ if (!wireInitialized) {
101+ WireInternal.begin (SDA, SCL);
102+ wireInitialized = true ;
103+ }
104+ uint16_t voltage = (readRegister (0x55 , 0x9 ) << 8 ) | readRegister (0x55 , 0x8 );
105+ return (float )voltage / 1000 .0f ;
106106}
107107
108108uint16_t NessoBattery::getChargeLevel () {
109- // BQ27220 - address 0x55
110- if (!wireInitialized) {
111- WireInternal.begin (SDA, SCL);
112- wireInitialized = true ;
113- }
114- uint16_t current_capacity = readRegister (0x55 , 0x11 ) << 8 | readRegister (0x55 , 0x10 );
115- uint16_t total_capacity = readRegister (0x55 , 0x13 ) << 8 | readRegister (0x55 , 0x12 );
116- return (current_capacity * 100 ) / total_capacity;
109+ // BQ27220 - address 0x55
110+ if (!wireInitialized) {
111+ WireInternal.begin (SDA, SCL);
112+ wireInitialized = true ;
113+ }
114+ uint16_t current_capacity = readRegister (0x55 , 0x11 ) << 8 | readRegister (0x55 , 0x10 );
115+ uint16_t total_capacity = readRegister (0x55 , 0x13 ) << 8 | readRegister (0x55 , 0x12 );
116+ return (current_capacity * 100 ) / total_capacity;
117117}
118118
119119ExpanderPin LORA_LNA_ENABLE (5 );
@@ -126,4 +126,4 @@ ExpanderPin LCD_RESET((1 << 8) | 1);
126126ExpanderPin GROVE_POWER_EN ((1 << 8 ) | 2);
127127ExpanderPin VIN_DETECT ((1 << 8 ) | 5);
128128ExpanderPin LCD_BACKLIGHT ((1 << 8 ) | 6);
129- ExpanderPin LED_BUILTIN ((1 << 8 ) | 7);
129+ ExpanderPin LED_BUILTIN ((1 << 8 ) | 7);
0 commit comments