From e0762c2e7702155a9ea2a55d35172adc84aecf9a Mon Sep 17 00:00:00 2001 From: robbederks Date: Mon, 4 Nov 2019 17:26:37 -0800 Subject: [PATCH] Add Python & USB API for controlling phone power (#313) * Added interface for phone power * Add power function in python * Fixed struct --- board/board_declarations.h | 2 ++ board/boards/black.h | 7 ++++++- board/boards/grey.h | 3 ++- board/boards/pedal.h | 7 ++++++- board/boards/uno.h | 9 +++++++-- board/boards/white.h | 7 ++++++- board/main.c | 4 ++++ python/__init__.py | 6 +++++- 8 files changed, 38 insertions(+), 7 deletions(-) diff --git a/board/board_declarations.h b/board/board_declarations.h index 2fd3976a..e4e29f99 100644 --- a/board/board_declarations.h +++ b/board/board_declarations.h @@ -11,6 +11,7 @@ typedef bool (*board_check_ignition)(void); typedef uint32_t (*board_read_current)(void); typedef void (*board_set_ir_power)(uint8_t percentage); typedef void (*board_set_fan_power)(uint8_t percentage); +typedef void (*board_set_phone_power)(bool enabled); struct board { const char *board_type; @@ -27,6 +28,7 @@ struct board { board_read_current read_current; board_set_ir_power set_ir_power; board_set_fan_power set_fan_power; + board_set_phone_power set_phone_power; }; // ******************* Definitions ******************** diff --git a/board/boards/black.h b/board/boards/black.h index f033e82b..c8879116 100644 --- a/board/boards/black.h +++ b/board/boards/black.h @@ -146,6 +146,10 @@ void black_set_fan_power(uint8_t percentage){ UNUSED(percentage); } +void black_set_phone_power(bool enabled){ + UNUSED(enabled); +} + void black_init(void) { common_init_gpio(); @@ -227,5 +231,6 @@ const board board_black = { .check_ignition = black_check_ignition, .read_current = black_read_current, .set_fan_power = black_set_fan_power, - .set_ir_power = black_set_ir_power + .set_ir_power = black_set_ir_power, + .set_phone_power = black_set_phone_power }; diff --git a/board/boards/grey.h b/board/boards/grey.h index 1a39bce0..03479a97 100644 --- a/board/boards/grey.h +++ b/board/boards/grey.h @@ -17,5 +17,6 @@ const board board_grey = { .check_ignition = white_check_ignition, .read_current = white_read_current, .set_fan_power = white_set_fan_power, - .set_ir_power = white_set_ir_power + .set_ir_power = white_set_ir_power, + .set_phone_power = white_set_phone_power }; \ No newline at end of file diff --git a/board/boards/pedal.h b/board/boards/pedal.h index 02612d3f..96ca11a4 100644 --- a/board/boards/pedal.h +++ b/board/boards/pedal.h @@ -73,6 +73,10 @@ void pedal_set_fan_power(uint8_t percentage){ UNUSED(percentage); } +void pedal_set_phone_power(bool enabled){ + UNUSED(enabled); +} + void pedal_init(void) { common_init_gpio(); @@ -108,5 +112,6 @@ const board board_pedal = { .check_ignition = pedal_check_ignition, .read_current = pedal_read_current, .set_fan_power = pedal_set_fan_power, - .set_ir_power = pedal_set_ir_power + .set_ir_power = pedal_set_ir_power, + .set_phone_power = pedal_set_phone_power }; \ No newline at end of file diff --git a/board/boards/uno.h b/board/boards/uno.h index f5765c22..ffce05a0 100644 --- a/board/boards/uno.h +++ b/board/boards/uno.h @@ -140,6 +140,10 @@ uint32_t uno_read_current(void){ return 0U; } +void uno_set_phone_power(bool enabled){ + set_gpio_output(GPIOB, 4, enabled); +} + void uno_init(void) { common_init_gpio(); @@ -168,7 +172,7 @@ void uno_init(void) { uno_set_gps_load_switch(true); // Turn on phone regulator - set_gpio_output(GPIOB, 4, 1); + uno_set_phone_power(true); // Initialize IR PWM and set to 0% set_gpio_alternate(GPIOB, 7, GPIO_AF2_TIM4); @@ -243,5 +247,6 @@ const board board_uno = { .check_ignition = uno_check_ignition, .read_current = uno_read_current, .set_fan_power = uno_set_fan_power, - .set_ir_power = uno_set_ir_power + .set_ir_power = uno_set_ir_power, + .set_phone_power = uno_set_phone_power }; diff --git a/board/boards/white.h b/board/boards/white.h index 899ba8d4..0fce9d68 100644 --- a/board/boards/white.h +++ b/board/boards/white.h @@ -236,6 +236,10 @@ bool white_check_ignition(void){ return !get_gpio_input(GPIOA, 1); } +void white_set_phone_power(bool enabled){ + UNUSED(enabled); +} + void white_init(void) { common_init_gpio(); @@ -332,5 +336,6 @@ const board board_white = { .check_ignition = white_check_ignition, .read_current = white_read_current, .set_fan_power = white_set_fan_power, - .set_ir_power = white_set_ir_power + .set_ir_power = white_set_ir_power, + .set_phone_power = white_set_phone_power }; diff --git a/board/main.c b/board/main.c index 3bcae876..1a4bd48d 100644 --- a/board/main.c +++ b/board/main.c @@ -313,6 +313,10 @@ int usb_cb_control_msg(USB_Setup_TypeDef *setup, uint8_t *resp, bool hardwired) resp[1] = ((fan_rpm & 0xFF00U) >> 8U); resp_len = 2; break; + // **** 0xb3: set phone power + case 0xb3: + current_board->set_phone_power(setup->b.wValue.w > 0U); + break; // **** 0xc0: get CAN debug info case 0xc0: puts("can tx: "); puth(can_tx_cnt); diff --git a/python/__init__.py b/python/__init__.py index f22631da..bad119eb 100644 --- a/python/__init__.py +++ b/python/__init__.py @@ -626,4 +626,8 @@ class Panda(object): def get_fan_rpm(self): dat = self._handle.controlRead(Panda.REQUEST_IN, 0xb2, 0, 0, 2) a = struct.unpack("H", dat) - return a[0] \ No newline at end of file + return a[0] + +# ****************** Phone ***************** + def set_phone_power(self, enabled): + self._handle.controlWrite(Panda.REQUEST_OUT, 0xb3, int(enabled), 0, b'') \ No newline at end of file