Skip to content

Commit

Permalink
Fix battery level code in adafruit_ble.cpp (qmk#6648)
Browse files Browse the repository at this point in the history
* Fix battery level code in adafruit_ble.cpp

The code in tsk_core/protocol/lufa/adafluit_ble.cpp that polls the
battery level for the Adafruit feather BLE controller reads the
regulated voltage, not the raw voltage coming from the battery. To do
that, the Adafruit Feather docs say you should read from pin A9:
https://learn.adafruit.com/adafruit-feather-32u4-basic-proto/power-management#measuring-battery-4-9.
(See also
https://learn.adafruit.com/adafruit-feather-32u4-bluefruit-le/pinouts#logic-pins-2-9.)

I'm not sure why, but analogRead(9); doesn't read the correct pin.
Checking all available analog pins experimentally, it turns out that
analogRead(7); returns the correct value. So the code above should read:

    state.vbat = analogRead(7);

* Update tmk_core/protocol/lufa/adafruit_ble.cpp

Co-Authored-By: Drashna Jaelre <drashna@live.com>

* Remove old comment

* Fix linking error

* Remove `#ifdef` around `#include analog.h`.

* Really fix linking error
  • Loading branch information
bwhelm authored and Philip Karlsson committed Dec 3, 2019
1 parent 3f621c4 commit bec5685
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
6 changes: 6 additions & 0 deletions drivers/avr/analog.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,15 @@

#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif
void analogReference(uint8_t mode);
int16_t analogRead(uint8_t pin);
int16_t adc_read(uint8_t mux);
#ifdef __cplusplus
}
#endif

#define ADC_REF_POWER (1 << REFS0)
#define ADC_REF_INTERNAL ((1 << REFS1) | (1 << REFS0))
Expand Down
2 changes: 2 additions & 0 deletions tmk_core/protocol/lufa.mk
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ ifeq ($(strip $(BLUETOOTH_ENABLE)), yes)
endif

ifeq ($(strip $(BLUETOOTH)), AdafruitBLE)
LUFA_SRC += analog.c
LUFA_SRC += $(LUFA_DIR)/adafruit_ble.cpp
endif

Expand All @@ -51,6 +52,7 @@ SRC += $(LUFA_SRC)
# Search Path
VPATH += $(TMK_PATH)/$(LUFA_DIR)
VPATH += $(LUFA_PATH)
VPATH += $(DRIVER_PATH)/avr

# Option modules
#ifdef $(or MOUSEKEY_ENABLE, PS2_MOUSE_ENABLE)
Expand Down
14 changes: 8 additions & 6 deletions tmk_core/protocol/lufa/adafruit_ble.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "action_util.h"
#include "ringbuffer.hpp"
#include <string.h>
#include "analog.h"

// These are the pin assignments for the 32u4 boards.
// You may define them to something else in your config.h
Expand All @@ -29,6 +30,12 @@
#define SAMPLE_BATTERY
#define ConnectionUpdateInterval 1000 /* milliseconds */

#ifdef SAMPLE_BATTERY
#ifndef BATTERY_LEVEL_PIN
# define BATTERY_LEVEL_PIN 7
#endif
#endif

static struct {
bool is_connected;
bool initialized;
Expand Down Expand Up @@ -631,15 +638,10 @@ void adafruit_ble_task(void) {
}

#ifdef SAMPLE_BATTERY
// I don't know if this really does anything useful yet; the reported
// voltage level always seems to be around 3200mV. We may want to just rip
// this code out.
if (timer_elapsed(state.last_battery_update) > BatteryUpdateInterval && resp_buf.empty()) {
state.last_battery_update = timer_read();

if (at_command_P(PSTR("AT+HWVBAT"), resbuf, sizeof(resbuf))) {
state.vbat = atoi(resbuf);
}
state.vbat = analogRead(BATTERY_LEVEL_PIN);
}
#endif
}
Expand Down

0 comments on commit bec5685

Please sign in to comment.