Skip to content

Commit

Permalink
Add Python & USB API for controlling phone power (commaai#313)
Browse files Browse the repository at this point in the history
* Added interface for phone power

* Add power function in python

* Fixed struct
  • Loading branch information
robbederks authored Nov 5, 2019
1 parent ba9fb69 commit e0762c2
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 7 deletions.
2 changes: 2 additions & 0 deletions board/board_declarations.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 ********************
Expand Down
7 changes: 6 additions & 1 deletion board/boards/black.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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
};
3 changes: 2 additions & 1 deletion board/boards/grey.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
7 changes: 6 additions & 1 deletion board/boards/pedal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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
};
9 changes: 7 additions & 2 deletions board/boards/uno.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
};
7 changes: 6 additions & 1 deletion board/boards/white.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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
};
4 changes: 4 additions & 0 deletions board/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 5 additions & 1 deletion python/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -626,4 +626,8 @@ def set_fan_power(self, percentage):
def get_fan_rpm(self):
dat = self._handle.controlRead(Panda.REQUEST_IN, 0xb2, 0, 0, 2)
a = struct.unpack("H", dat)
return a[0]
return a[0]

# ****************** Phone *****************
def set_phone_power(self, enabled):
self._handle.controlWrite(Panda.REQUEST_OUT, 0xb3, int(enabled), 0, b'')

0 comments on commit e0762c2

Please sign in to comment.