Skip to content

Commit

Permalink
drv/battery: drop port parameter
Browse files Browse the repository at this point in the history
It seems reasonable to expect that each hub will only have one built-in battery. So drop the port parameter.

movehub fw size -16
  • Loading branch information
dlech committed Jun 1, 2019
1 parent 3b2b4de commit c45103c
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 27 deletions.
4 changes: 2 additions & 2 deletions extmod/modbattery.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@

STATIC mp_obj_t battery_voltage(void) {
uint16_t volt;
pb_assert(pbdrv_battery_get_voltage_now(PBIO_PORT_SELF, &volt));
pb_assert(pbdrv_battery_get_voltage_now(&volt));
return mp_obj_new_int(volt);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(battery_voltage_obj, battery_voltage);

STATIC mp_obj_t battery_current(void) {
uint16_t cur;
pb_assert(pbdrv_battery_get_current_now(PBIO_PORT_SELF, &cur));
pb_assert(pbdrv_battery_get_current_now(&cur));
return mp_obj_new_int(cur);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(battery_current_obj, battery_current);
Expand Down
15 changes: 4 additions & 11 deletions lib/pbio/drv/battery/battery_adc.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,19 @@

#if PBDRV_CONFIG_BATTERY

#include <stdbool.h>

#include <pbdrv/adc.h>

#include <pbio/error.h>
#include <pbio/port.h>
#include <sys/process.h>

PROCESS(pbdrv_battery_process, "battery");

pbio_error_t pbdrv_battery_get_voltage_now(pbio_port_t port, uint16_t *value) {
pbio_error_t pbdrv_battery_get_voltage_now(uint16_t *value) {
uint16_t raw;
pbio_error_t err;

if (port != PBIO_PORT_SELF) {
return PBIO_ERROR_INVALID_PORT;
}

err = pbdrv_adc_get_ch(PBDRV_CONFIG_BATTERY_ADC_VOLTAGE_CH, &raw);
if (err != PBIO_SUCCESS) {
return err;
Expand All @@ -35,14 +32,10 @@ pbio_error_t pbdrv_battery_get_voltage_now(pbio_port_t port, uint16_t *value) {
return PBIO_SUCCESS;
}

pbio_error_t pbdrv_battery_get_current_now(pbio_port_t port, uint16_t *value) {
pbio_error_t pbdrv_battery_get_current_now(uint16_t *value) {
uint16_t raw;
pbio_error_t err;

if (port != PBIO_PORT_SELF) {
return PBIO_ERROR_INVALID_PORT;
}

// this is measuring the voltage across a 0.05 ohm shunt resistor probably
// via an op amp with unknown gain.
err = pbdrv_adc_get_ch(PBDRV_CONFIG_BATTERY_ADC_CURRENT_CH, &raw);
Expand Down
6 changes: 3 additions & 3 deletions lib/pbio/drv/battery/battery_ev3_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@

#if PBDRV_CONFIG_BATTERY

#include <stdbool.h>
#include <stdio.h>

#include <pbio/error.h>
#include <pbio/port.h>
#include <sys/process.h>

PROCESS(pbdrv_battery_process, "battery");

static FILE *f_voltage;
static FILE *f_current;

pbio_error_t pbdrv_battery_get_voltage_now(pbio_port_t port, uint16_t *value) {
pbio_error_t pbdrv_battery_get_voltage_now(uint16_t *value) {
int32_t microvolt;
if (0 == fseek(f_voltage, 0, SEEK_SET) &&
0 <= fscanf(f_voltage, "%d", &microvolt) &&
Expand All @@ -30,7 +30,7 @@ pbio_error_t pbdrv_battery_get_voltage_now(pbio_port_t port, uint16_t *value) {
return PBIO_ERROR_IO;
}

pbio_error_t pbdrv_battery_get_current_now(pbio_port_t port, uint16_t *value) {
pbio_error_t pbdrv_battery_get_current_now(uint16_t *value) {
int32_t microamp;
if (0 == fseek(f_current, 0, SEEK_SET) &&
0 <= fscanf(f_current, "%d", &microamp) &&
Expand Down
11 changes: 4 additions & 7 deletions lib/pbio/include/pbdrv/battery.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include <pbdrv/config.h>

#include <pbio/error.h>
#include <pbio/port.h>

#if PBDRV_CONFIG_BATTERY

Expand All @@ -26,32 +25,30 @@ PROCESS_NAME(pbdrv_battery_process);

/**
* Gets the battery voltage.
* @param [in] port The I/O port
* @param [out] value The voltage in millivolts
* @return ::PBIO_SUCCESS on success, ::PBIO_ERROR_INVALID_PORT if
* the port is not valid or ::PBIO_ERROR_IO if there was
* an I/O error.
*/
pbio_error_t pbdrv_battery_get_voltage_now(pbio_port_t port, uint16_t *value);
pbio_error_t pbdrv_battery_get_voltage_now(uint16_t *value);

/**
* Gets the battery current.
* @param [in] port The I/O port
* @param [out] value The current in milliamps
* @return ::PBIO_SUCCESS on success, ::PBIO_ERROR_INVALID_PORT if
* the port is not valid or ::PBIO_ERROR_IO if there was
* an I/O error.
*/
pbio_error_t pbdrv_battery_get_current_now(pbio_port_t port, uint16_t *value);
pbio_error_t pbdrv_battery_get_current_now(uint16_t *value);

#else

static inline void _pbdrv_battery_poll(uint32_t now) { }
static inline void _pbdrv_battery_deinit(void) { }
static inline pbio_error_t pbdrv_battery_get_voltage_now(pbio_port_t port, uint16_t *value) {
static inline pbio_error_t pbdrv_battery_get_voltage_now(uint16_t *value) {
return PBIO_ERROR_NOT_SUPPORTED;
}
static inline pbio_error_t pbdrv_battery_get_current_now(pbio_port_t port, uint16_t *value) {
static inline pbio_error_t pbdrv_battery_get_current_now(uint16_t *value) {
return PBIO_ERROR_NOT_SUPPORTED;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/pbio/platform/hub4/sys.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ static void init(void) {
IWDG->KR = 0xaaaa; // refresh counter
IWDG->KR = 0xcccc; // start watchdog timer

pbdrv_battery_get_voltage_now(PBIO_PORT_SELF, &battery_voltage);
pbdrv_battery_get_voltage_now(&battery_voltage);
avg_battery_voltage = battery_voltage;

_pbio_light_set_user_mode(false);
Expand Down Expand Up @@ -200,7 +200,7 @@ static void update_battery(clock_time_t now) {
poll_interval = clock_to_msec(now - prev_poll_time);
prev_poll_time = now;

pbdrv_battery_get_voltage_now(PBIO_PORT_SELF, &battery_voltage);
pbdrv_battery_get_voltage_now(&battery_voltage);

avg_battery_voltage = (avg_battery_voltage * (BATTERY_PERIOD_MS - poll_interval)
+ battery_voltage * poll_interval) / BATTERY_PERIOD_MS;
Expand Down
4 changes: 2 additions & 2 deletions lib/pbio/platform/move_hub/sys.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ static void init(void) {
uint16_t battery_voltage;
uint8_t r, g, b;

pbdrv_battery_get_voltage_now(PBIO_PORT_SELF, &battery_voltage);
pbdrv_battery_get_voltage_now(&battery_voltage);
avg_battery_voltage = battery_voltage;

_pbio_light_set_user_mode(false);
Expand Down Expand Up @@ -192,7 +192,7 @@ static void update_battery(clock_time_t now) {
poll_interval = clock_to_msec(now - prev_poll_time);
prev_poll_time = now;

pbdrv_battery_get_voltage_now(PBIO_PORT_SELF, &battery_voltage);
pbdrv_battery_get_voltage_now(&battery_voltage);

avg_battery_voltage = (avg_battery_voltage * (BATTERY_PERIOD_MS - poll_interval)
+ battery_voltage * poll_interval) / BATTERY_PERIOD_MS;
Expand Down

0 comments on commit c45103c

Please sign in to comment.