Skip to content

Commit 6dda9d4

Browse files
committed
Add options for get/put TX power (wifi)
1 parent 19dd739 commit 6dda9d4

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

src/network_interfaces/ws_networking_pico.h

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,64 @@ class ws_networking_pico : public Wippersnapper {
168168
/********************************************************/
169169
int32_t getRSSI() { return WiFi.RSSI(); }
170170

171+
/********************************************************/
172+
/*!
173+
@brief Puts a 32-bit integer into a buffer in little-endian format.
174+
@param buf
175+
Buffer to write to.
176+
@param x
177+
32-bit integer to write.
178+
*/
179+
/********************************************************/
180+
void putLittleEndian32intoBuffer(uint8_t *buf, uint32_t x) {
181+
buf[0] = x & 0xFF;
182+
buf[1] = (x >> 8) & 0xFF;
183+
buf[2] = (x >> 16) & 0xFF;
184+
buf[3] = (x >> 24) & 0xFF;
185+
}
186+
187+
/********************************************************/
188+
/*!
189+
@brief Gets a 32-bit integer from a buffer in little-endian format.
190+
@param buf
191+
Buffer to fetch from.
192+
@return float
193+
*/
194+
/********************************************************/
195+
float getLittleEndian32fromBuffer(uint8_t *buf) {
196+
return (float)(buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24));
197+
}
198+
199+
/********************************************************/
200+
/*!
201+
@brief Gets the RPi Pico's WiFi transmit power.
202+
@return float
203+
*/
204+
/********************************************************/
205+
float common_hal_wifi_radio_get_tx_power() {
206+
uint8_t buf[13];
207+
memcpy(buf, "qtxpower\x00\x00\x00\x00\x00", 13);
208+
cyw43_ioctl(&cyw43_state, CYW43_IOCTL_GET_VAR, 13, buf, CYW43_ITF_STA);
209+
return getLittleEndian32fromBuffer(buf) * 0.25f;
210+
}
211+
212+
/********************************************************/
213+
/*!
214+
@brief Sets the RPi Pico's WiFi transmit power.
215+
@param dbmPower
216+
Transmit power in dBm.
217+
*/
218+
/********************************************************/
219+
void setTxPower(float dbmPower) {
220+
// https://github.com/adafruit/circuitpython/blob/main/ports/raspberrypi/common-hal/wifi/Radio.c#L101C1-L108C2
221+
int dbm_times_four = (int)(4 * dbmPower);
222+
uint8_t buf[9 + 4];
223+
memcpy(buf, "qtxpower\x00", 9);
224+
putLittleEndian32intoBuffer(buf + 9, dbm_times_four);
225+
cyw43_ioctl(&cyw43_state, CYW43_IOCTL_SET_VAR, 9 + 4, buf, CYW43_ITF_STA);
226+
cyw43_ioctl(&cyw43_state, CYW43_IOCTL_SET_VAR, 9 + 4, buf, CYW43_ITF_AP);
227+
}
228+
171229
/********************************************************/
172230
/*!
173231
@brief Initializes the MQTT client

0 commit comments

Comments
 (0)