Skip to content

Commit 56f9f0d

Browse files
committed
add tx power get/set
1 parent 71a0015 commit 56f9f0d

File tree

1 file changed

+21
-3
lines changed
  • ports/raspberrypi/common-hal/wifi

1 file changed

+21
-3
lines changed

ports/raspberrypi/common-hal/wifi/Radio.c

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,17 @@
5959
#define NETIF_STA (&cyw43_state.netif[CYW43_ITF_STA])
6060
#define NETIF_AP (&cyw43_state.netif[CYW43_ITF_AP])
6161

62+
static inline uint32_t nw_get_le32(const uint8_t *buf) {
63+
return buf[0] | buf[1] << 8 | buf[2] << 16 | buf[3] << 24;
64+
}
65+
66+
static inline void nw_put_le32(uint8_t *buf, uint32_t x) {
67+
buf[0] = x;
68+
buf[1] = x >> 8;
69+
buf[2] = x >> 16;
70+
buf[3] = x >> 24;
71+
}
72+
6273
NORETURN static void ro_attribute(int attr) {
6374
mp_raise_msg_varg(&mp_type_AttributeError, MP_ERROR_TEXT("'%s' object has no attribute '%q'"), "Radio", attr);
6475
}
@@ -96,12 +107,19 @@ void common_hal_wifi_radio_set_mac_address(wifi_radio_obj_t *self, const uint8_t
96107
}
97108

98109
mp_float_t common_hal_wifi_radio_get_tx_power(wifi_radio_obj_t *self) {
99-
return MICROPY_FLOAT_CONST(0.);
110+
uint8_t buf[13];
111+
memcpy(buf, "qtxpower\x00\x00\x00\x00\x00", 13);
112+
cyw43_ioctl(&cyw43_state, CYW43_IOCTL_GET_VAR, 13, buf, CYW43_ITF_STA);
113+
return nw_get_le32(buf) * MICROPY_FLOAT_CONST(0.25);
100114
}
101115

102116
void common_hal_wifi_radio_set_tx_power(wifi_radio_obj_t *self, const mp_float_t tx_power) {
103-
ro_attribute(MP_QSTR_tx_power);
104-
117+
mp_int_t dbm_times_four = (int)(4 * tx_power);
118+
uint8_t buf[9 + 4];
119+
memcpy(buf, "qtxpower\x00", 9);
120+
nw_put_le32(buf + 9, dbm_times_four);
121+
cyw43_ioctl(&cyw43_state, CYW43_IOCTL_SET_VAR, 9 + 4, buf, CYW43_ITF_STA);
122+
cyw43_ioctl(&cyw43_state, CYW43_IOCTL_SET_VAR, 9 + 4, buf, CYW43_ITF_AP);
105123
}
106124

107125
mp_obj_t common_hal_wifi_radio_get_mac_address_ap(wifi_radio_obj_t *self) {

0 commit comments

Comments
 (0)