allow changing status led brightness

This commit is contained in:
Benjamin Wiegand
2025-08-29 16:59:28 -07:00
parent ebb329ac95
commit 8363ecbd13
5 changed files with 37 additions and 8 deletions
+1
View File
@@ -45,6 +45,7 @@ target_link_libraries(BattMITM_2.0
pico_stdlib
hardware_i2c
pico_i2c_slave
hardware_pwm
)
# Add the standard include files to the build
+1
View File
@@ -44,3 +44,4 @@
#define LAPTOP_I2C_BAUD BATT_I2C_BAUD // use the same baud (for now)
#define STATUS_LED_MAX_BRIGHTNESS 10000 // 0-65535
+2 -2
View File
@@ -83,7 +83,7 @@ void mitm_loop() {
smbus_transfer_t* transfer = static_queue_peek(mitm_transfer_queue);
if (transfer == NULL) return;
status_led(1);
status_mitm(true);
while (transfer != NULL) {
@@ -170,7 +170,7 @@ void mitm_loop() {
transfer = static_queue_peek(mitm_transfer_queue);
}
status_led(0);
status_mitm(false);
}
+29 -5
View File
@@ -24,13 +24,37 @@
#include "status.h"
#include "config.h"
#include "hardware/gpio.h"
#include "hardware/pwm.h"
#define STATUS_ITEMS 1
#define STATUS_ITEM_MITM_INDEX 0
bool status_item_states[STATUS_ITEMS];
void init_status() {
gpio_init(STATUS_LED_PIN);
gpio_set_dir(STATUS_LED_PIN, GPIO_OUT);
pwm_config config = pwm_get_default_config();
gpio_set_function(STATUS_LED_PIN, GPIO_FUNC_PWM);
uint slice_num = pwm_gpio_to_slice_num(STATUS_LED_PIN);
pwm_init(slice_num, &config, true);
}
// blinking leds always make things look cooler than they are
void status_led(int state) {
gpio_put(STATUS_LED_PIN, state);
void status_led_update() {
uint16_t brightness = 0;
uint16_t brightness_steps = STATUS_LED_MAX_BRIGHTNESS / STATUS_ITEMS;
for (int i = 0; i < STATUS_ITEMS; i++) {
brightness += brightness_steps * status_item_states[i];
}
pwm_set_gpio_level(STATUS_LED_PIN, brightness);
}
void status_mitm(bool activity) {
status_item_states[STATUS_ITEM_MITM_INDEX] = activity;
status_led_update();
}
+4 -1
View File
@@ -21,6 +21,9 @@
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
*/
#include <stdbool.h>
// blinking leds always make things look cooler than they are
void init_status();
void status_led(int state);
void status_mitm(bool activity);