From ebb10d50a34981fb1ee200011120d1e8a0ca11cb Mon Sep 17 00:00:00 2001
From: Florian Kargl <florian.kargl@student.tugraz.at>
Date: Tue, 13 Aug 2019 17:44:10 +0200
Subject: [PATCH] refactor(leds): Use new portexpander API for leds

---
 lib/card10/leds.c         | 21 +++++++++------------
 lib/card10/portexpander.c | 31 -------------------------------
 lib/card10/portexpander.h |  4 ----
 3 files changed, 9 insertions(+), 47 deletions(-)

diff --git a/lib/card10/leds.c b/lib/card10/leds.c
index 3f284db4a..4be29e467 100644
--- a/lib/card10/leds.c
+++ b/lib/card10/leds.c
@@ -278,10 +278,7 @@ static uint8_t power_pin_conversion(uint8_t group)
 
 static void power_all(void)
 {
-	for (int i = 0; i < 3; i++) {
-		portexpander_prep(i, 0);
-	}
-	portexpander_update();
+	portexpander_out_clr(PIN_0 | PIN_1 | PIN_2);
 }
 
 void leds_update_power(void)
@@ -294,14 +291,14 @@ void leds_update_power(void)
 	if (new_groups == active_groups) {
 		return;
 	}
-	for (int i = 0; i < 3; i++) {
-		if (i < new_groups) {
-			portexpander_prep(power_pin_conversion(i), 0);
-		} else {
-			portexpander_prep(power_pin_conversion(i), 1);
-		}
+
+	uint8_t out_val = 0;
+	for (int i = new_groups; i < 3; ++i) {
+		out_val |= (1 << power_pin_conversion(i));
 	}
-	portexpander_update();
+
+	portexpander_out_put(PIN_0 | PIN_1 | PIN_2, out_val);
+
 	if (active_groups < new_groups) {
 		for (int i = 0; i < powerup_wait_cycles; i++) {
 			__NOP();
@@ -336,7 +333,7 @@ void leds_update(void)
 
 void leds_flashlight(bool power)
 {
-	portexpander_set(7, (power) ? 0 : 1);
+	portexpander_out_put(PIN_7, (power) ? 0 : 1);
 }
 
 void leds_set_gamma_table(uint8_t rgb_channel, uint8_t table[256])
diff --git a/lib/card10/portexpander.c b/lib/card10/portexpander.c
index 403fd33fe..f3aa4b3fe 100644
--- a/lib/card10/portexpander.c
+++ b/lib/card10/portexpander.c
@@ -190,19 +190,6 @@ bool portexpander_detected(void)
 	return detected;
 }
 
-void portexpander_set(uint8_t pin, uint8_t value)
-{
-	if (detected && pin < 8) {
-		if (value) {
-			output_state |= (1 << pin);
-		} else {
-			output_state &= ~(1 << pin);
-		}
-
-		portexpander_write(PE_C_OUTPUT_PORT, output_state);
-	}
-}
-
 /* ************************************************************************** */
 void portexpander_out_set(uint8_t mask)
 {
@@ -221,30 +208,12 @@ void portexpander_out_clr(uint8_t mask)
 	}
 }
 
-void portexpander_prep(uint8_t pin, uint8_t value)
-{
-	if (pin < 8) {
-		if (value) {
-			output_state |= (1 << pin);
-		} else {
-			output_state &= ~(1 << pin);
-		}
-	}
-}
-
 /* ************************************************************************** */
 uint8_t portexpander_out_get(uint8_t mask)
 {
 	return output_state & mask;
 }
 
-void portexpander_update(void)
-{
-	if (detected) {
-		portexpander_write(PE_C_OUTPUT_PORT, output_state);
-	}
-}
-
 /* ************************************************************************** */
 void portexpander_out_put(uint8_t mask, uint8_t val)
 {
diff --git a/lib/card10/portexpander.h b/lib/card10/portexpander.h
index 477b32da1..6f203dd78 100644
--- a/lib/card10/portexpander.h
+++ b/lib/card10/portexpander.h
@@ -6,10 +6,6 @@
 #include <stdint.h>
 #include <stdbool.h>
 
-void portexpander_set(uint8_t pin, uint8_t value);
-void portexpander_prep(uint8_t pin, uint8_t value);
-void portexpander_update(void);
-
 /**
  * Structure type for configuring the portexpander.
  */
-- 
GitLab