From 93176052c129f8422af4700d32c9660459678456 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 d21049cf..4a2c9967 100644
--- a/lib/card10/leds.c
+++ b/lib/card10/leds.c
@@ -285,10 +285,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)
@@ -301,14 +298,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();
@@ -343,7 +340,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 b799e215..979e73da 100644
--- a/lib/card10/portexpander.c
+++ b/lib/card10/portexpander.c
@@ -188,19 +188,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)
 {
@@ -219,30 +206,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 727f8dfe..a07c8163 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