aboutsummaryrefslogtreecommitdiff
path: root/src/tm1637.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/tm1637.c')
-rw-r--r--src/tm1637.c40
1 files changed, 24 insertions, 16 deletions
diff --git a/src/tm1637.c b/src/tm1637.c
index 9cc79d6..d4a4e44 100644
--- a/src/tm1637.c
+++ b/src/tm1637.c
@@ -33,17 +33,6 @@ void _tm1637_pin_write(uint32_t pin, bool state) {
GPIOA->ODR ^= (((GPIOA->ODR & (1 << pin)) >> pin) ^ state) << pin;
}
-void tm1637_begin() {
- _tm1637_GPIOMODE_gp_output();
-
- // general purpose output mode for clk
- GPIOA->MODER &= ~(0b11 << (PINOUT_DISP_CLK * 2));
- GPIOA->MODER |= (0b01 << (PINOUT_DISP_CLK * 2));
-
- _tm1637_pin_write(PINOUT_DISP_CLK, 1);
- _tm1637_pin_write(PINOUT_DISP_DIO, 1);
-}
-
void _tm1637_start() {
_tm1637_pin_write(PINOUT_DISP_DIO, 0);
}
@@ -54,13 +43,15 @@ void _tm1637_stop() {
}
bool _tm1637_ack() {
+ bool ack = false;
_tm1637_pin_write(PINOUT_DISP_CLK, 0);
_tm1637_pin_write(PINOUT_DISP_DIO, 0);
_tm1637_GPIOMODE_open_drain();
+ ack = (GPIOA->IDR & (1 << PINOUT_DISP_DIO)) == 0;
_tm1637_pin_write(PINOUT_DISP_CLK, 1);
_tm1637_pin_write(PINOUT_DISP_CLK, 0);
_tm1637_GPIOMODE_gp_output();
- return true;
+ return ack;
}
/**
@@ -70,7 +61,6 @@ bool _tm1637_ack() {
*/
TM1637Sequence _tm1637_send(TM1637Sequence command) {
TM1637Sequence response = { .data = 0, .length = 0 };
-
_tm1637_start();
for (uint32_t byte = 0; byte < command.length; byte++) {
@@ -79,11 +69,10 @@ TM1637Sequence _tm1637_send(TM1637Sequence command) {
_tm1637_pin_write(PINOUT_DISP_DIO, (command.data[byte] & (1 << bit)) >> bit);
_tm1637_pin_write(PINOUT_DISP_CLK, 1);
}
- //TODO: confirm ack
- /*bool ack =*/ _tm1637_ack();
+ bool ack = _tm1637_ack();
+ if (!ack) byte -= 1;
}
- // stop condition
_tm1637_stop();
return response;
@@ -111,3 +100,22 @@ void tm1637_segmentsend(uint8_t segment, uint8_t data) {
};
_tm1637_send(seq);
}
+
+void _tm1637_clear() {
+ for (unsigned int x = 0; x < 4; x++)
+ tm1637_segmentsend(x, 0);
+}
+
+void tm1637_begin() {
+ _tm1637_GPIOMODE_gp_output();
+
+ // general purpose output mode for clk
+ GPIOA->MODER &= ~(0b11 << (PINOUT_DISP_CLK * 2));
+ GPIOA->MODER |= (0b01 << (PINOUT_DISP_CLK * 2));
+
+ _tm1637_pin_write(PINOUT_DISP_CLK, 1);
+ _tm1637_pin_write(PINOUT_DISP_DIO, 1);
+
+ _tm1637_clear();
+}
+