fix tm1637: always send 3 command types
This commit is contained in:
parent
bee5a1a827
commit
97e8de0bc9
100
lib/led_tm1637.c
100
lib/led_tm1637.c
|
@ -12,13 +12,14 @@
|
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
/** library to communicate with a Titan Micro TM1637 IC attached to a 4-digit 7-segment (code)
|
||||
/** library to communicate with a Titan Micro TM1637 IC attached to a 7-segment displays (code)
|
||||
* @file led_tm1637.c
|
||||
* @author King Kévin <kingkevin@cuvoodoo.info>
|
||||
* @date 2017
|
||||
* @note peripherals used: GPIO @ref led_tm1637_gpio, timer @ref led_tm1637_timer
|
||||
* @note the protocol is very similar to I2C but incompatible for the following reasons: the capacitance is too large for open-drain type output with weak pull-up resistors (push-pull needs to be used, preventing to get ACKs since no indication of the ACK timing is provided); the devices doesn't use addresses; the STM32 I2C will switch to receiver mode when the first sent byte (the I2C address) has last bit set to 1 (such as for address commands with B7=1 where B7 is transmitted last), preventing to send further bytes (the data byte after the address)
|
||||
* @note only 4-digit 7-segment displays are considered as this is the most common case
|
||||
* @warning all calls are blocking
|
||||
* @note the protocol is very similar to I2C but incompatible for the following reasons: the pin capacitance is too large for open-drain type output with weak pull-up resistors (push-pull needs to be used, preventing to get ACKs since no indication of the ACK timing is provided); the devices doesn't use addresses; the STM32 I2C will switch to receiver mode when the first sent byte (the I2C address) has last bit set to 1 (such as for address commands with B7=1 where B7 is transmitted last), preventing to send further bytes (the data byte after the address)
|
||||
*
|
||||
* bit vs segment: 0bpgfedcba
|
||||
* +a+
|
||||
|
@ -30,7 +31,7 @@
|
|||
|
||||
/* standard libraries */
|
||||
#include <stdint.h> // standard integer types
|
||||
#include <stdlib.h> // general utilities
|
||||
#include <stdlib.h> // memory utilities
|
||||
#include <string.h> // string utilities
|
||||
|
||||
/* STM32 (including CM3) libraries */
|
||||
|
@ -58,9 +59,7 @@
|
|||
/** @} */
|
||||
|
||||
/** display brightness */
|
||||
static enum led_tm1637_brightness_t display_brightness = LED_TM1637_14DIV16;
|
||||
/** if display is on */
|
||||
static bool display_on = false;
|
||||
enum led_tm1637_brightness_t display_brightness = LED_TM1637_14DIV16;
|
||||
|
||||
/** ASCII characters encoded for the 7 segments digit block
|
||||
* @note starts with space
|
||||
|
@ -198,14 +197,15 @@ static inline void led_tm1637_tick(void)
|
|||
* @return if write succeeded
|
||||
* @note includes start and stop conditions
|
||||
*/
|
||||
static bool led_tm1637_write(const uint8_t* data, size_t length)
|
||||
static bool led_tm1637_write(const uint8_t* data, uint8_t length)
|
||||
{
|
||||
bool to_return = true; // return if write succeeded
|
||||
if (data==NULL || length==0) { // verify there it data to be read
|
||||
if (NULL==data || 0==length) { // verify there it data to be read
|
||||
return false;
|
||||
}
|
||||
|
||||
// enable timer for signal generation
|
||||
gpio_set_mode(GPIO(LED_TM1637_DIO_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(LED_TM1637_DIO_PIN)); // ensure we can output to sent start condition
|
||||
timer_set_counter(TIM(LED_TM1637_TIMER), 0); // reset timer counter
|
||||
timer_enable_counter(TIM(LED_TM1637_TIMER)); // enable timer to generate timing
|
||||
led_tm1637_tick(); // wait to enforce minimum time since last write
|
||||
|
@ -216,8 +216,9 @@ static bool led_tm1637_write(const uint8_t* data, size_t length)
|
|||
gpio_clear(GPIO(LED_TM1637_CLK_PORT), GPIO(LED_TM1637_CLK_PIN)); // put CLK low
|
||||
|
||||
// send data bytes (MSb first)
|
||||
for (size_t i=0; i<length; i++) { // send all bytes
|
||||
for (uint8_t i=0; i<length; i++) { // send all bytes
|
||||
uint8_t byte = data[i];
|
||||
gpio_set_mode(GPIO(LED_TM1637_DIO_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(LED_TM1637_DIO_PIN)); // switch DIO to output to send bits
|
||||
for (uint8_t b=0; b<8; b++) { // send all bits
|
||||
if (byte&0x1) { // send a 1
|
||||
gpio_set(GPIO(LED_TM1637_DIO_PORT), GPIO(LED_TM1637_DIO_PIN)); // put DIO high
|
||||
|
@ -239,11 +240,10 @@ static bool led_tm1637_write(const uint8_t* data, size_t length)
|
|||
}
|
||||
led_tm1637_tick(); // wait for next tick
|
||||
gpio_clear(GPIO(LED_TM1637_CLK_PORT), GPIO(LED_TM1637_CLK_PIN)); // put CLK low
|
||||
gpio_set_mode(GPIO(LED_TM1637_DIO_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(LED_TM1637_DIO_PIN)); // switch DIO back to output to send next byte
|
||||
}
|
||||
|
||||
// send stop condition
|
||||
gpio_set_mode(GPIO(LED_TM1637_DIO_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(LED_TM1637_DIO_PIN)); // ensure DIO is output (in case no ACK as been received
|
||||
gpio_set_mode(GPIO(LED_TM1637_DIO_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(LED_TM1637_DIO_PIN)); // switch DIO to output to send stop condition
|
||||
led_tm1637_tick(); // wait for next tick
|
||||
gpio_set(GPIO(LED_TM1637_CLK_PORT), GPIO(LED_TM1637_CLK_PIN)); // put CLK high
|
||||
led_tm1637_tick(); // wait for next tick
|
||||
|
@ -253,58 +253,55 @@ static bool led_tm1637_write(const uint8_t* data, size_t length)
|
|||
return to_return;
|
||||
}
|
||||
|
||||
bool led_tm1637_on(void)
|
||||
/** write commands on bus to send data
|
||||
* @param[in] address_command data data to send (including address as first byte)
|
||||
* @param[in] length length of data
|
||||
* @return if send succeeded
|
||||
*/
|
||||
static bool led_tm1637_send_data(uint8_t* address_command, uint8_t length)
|
||||
{
|
||||
uint8_t data[] = { 0x88+display_brightness }; // command to turn display on (use set brightness)
|
||||
bool to_return = false; // result to return
|
||||
if (led_tm1637_write(data,LENGTH(data))) { // send command
|
||||
display_on = true; // remember display is on
|
||||
to_return = true; // command succeeded
|
||||
// sanity check
|
||||
if (0==length && NULL==address_command) {
|
||||
return false;
|
||||
}
|
||||
return to_return; // return result
|
||||
|
||||
uint8_t data_command[] = { 0x40 }; // data command: write data, automatic address adding, normal
|
||||
uint8_t display_command[] = { 0x88+(display_brightness&0x0f) }; // display command: turn display on and set brightness
|
||||
|
||||
if (led_tm1637_write(data_command, LENGTH(data_command)) && led_tm1637_write(address_command, length) && led_tm1637_write(display_command, LENGTH(display_command))) { // send commands
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool led_tm1637_off(void)
|
||||
{
|
||||
uint8_t data[] = { 0x80+display_brightness }; // command to turn display off (use set brightness)
|
||||
if (led_tm1637_write(data,LENGTH(data))) { // send command
|
||||
display_on = false; // remember display is off
|
||||
return true; // command succeeded
|
||||
}
|
||||
return false; // return result
|
||||
}
|
||||
|
||||
bool led_tm1637_brightness(enum led_tm1637_brightness_t brightness)
|
||||
{
|
||||
display_brightness = brightness; // save brightness
|
||||
if (display_on) { // adjust brightness if display is on
|
||||
return led_tm1637_on(); // adjust brightness
|
||||
} else {
|
||||
return true; // command succeeded
|
||||
// note: while the TM1637 could recognize the display command thanks to the first two bits, we still need to send the data and address command first
|
||||
uint8_t data_command[] = { 0x40 }; // data command: write data, automatic address adding, normal
|
||||
uint8_t address_command[] = { 0xc0, 0x00, 0x00, 0x00, 0x00 }; // address command: clear all (if bit 7 of byte 4 is set switching off doesn't work correctly, I don't know why)
|
||||
uint8_t display_command[] = { 0x80 }; // display command: turn display off (we don't care about the brightness since it's set when turned on)
|
||||
|
||||
if (led_tm1637_write(data_command,LENGTH(data_command)) && led_tm1637_write(address_command, LENGTH(address_command)) && led_tm1637_write(display_command, LENGTH(display_command))) { // send commands
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void led_tm1637_brightness(enum led_tm1637_brightness_t brightness)
|
||||
{
|
||||
display_brightness = (brightness&0x0f); // save brightness
|
||||
}
|
||||
|
||||
bool led_tm1637_number(uint16_t number)
|
||||
{
|
||||
uint8_t write_data[] = { 0x40 }; // command: write data, automatic address adding, normal
|
||||
uint8_t data[] = { 0xc0, ascii_7segments[((number/1000)%10)+'0'-' '], ascii_7segments[((number/100)%10)+'0'-' '], ascii_7segments[((number/10)%10)+'0'-' '], ascii_7segments[((number/1)%10)+'0'-' '] }; // set address C0H and add data
|
||||
|
||||
if (led_tm1637_write(write_data,LENGTH(write_data)) && led_tm1637_write(data,LENGTH(data))) { // send commands
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
uint8_t data[] = { 0xc0, ascii_7segments[((number/1000)%10)+'0'-' '], ascii_7segments[((number/100)%10)+'0'-' '], ascii_7segments[((number/10)%10)+'0'-' '], ascii_7segments[((number/1)%10)+'0'-' '] }; // encode number on 4 digits
|
||||
return led_tm1637_send_data(data, LENGTH(data)); // transmit data
|
||||
}
|
||||
|
||||
bool led_tm1637_time(uint8_t hours, uint8_t minutes)
|
||||
{
|
||||
uint8_t write_data[] = { 0x40 }; // command: write data, automatic address adding, normal
|
||||
uint8_t data[] = { 0xc0, ascii_7segments[((hours/10)%10)+'0'-' '], ascii_7segments[((hours/1)%10)+'0'-' ']|0x80, ascii_7segments[((minutes/10)%10)+'0'-' '], ascii_7segments[((minutes/1)%10)+'0'-' '] }; // set address C0H and add data
|
||||
|
||||
if (led_tm1637_write(write_data,LENGTH(write_data)) && led_tm1637_write(data,LENGTH(data))) { // send commands
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
uint8_t data[] = { 0xc0, ascii_7segments[((hours/10)%10)+'0'-' '], ascii_7segments[((hours/1)%10)+'0'-' ']|0x80, ascii_7segments[((minutes/10)%10)+'0'-' '], ascii_7segments[((minutes/1)%10)+'0'-' '] }; // encode time on 4 digits
|
||||
return led_tm1637_send_data(data, LENGTH(data)); // transmit data
|
||||
}
|
||||
|
||||
bool led_tm1637_text(char* text)
|
||||
|
@ -317,11 +314,6 @@ bool led_tm1637_text(char* text)
|
|||
return false;
|
||||
}
|
||||
}
|
||||
uint8_t write_data[] = { 0x40 }; // command: write data, automatic address adding, normal
|
||||
uint8_t data[] = { 0xc0, ascii_7segments[(text[0]&0x7f)-' ']|(text[0]&0x80), ascii_7segments[(text[1]&0x7f)-' ']|(text[1]&0x80), ascii_7segments[(text[2]&0x7f)-' ']|(text[2]&0x80), ascii_7segments[(text[3]&0x7f)-' ']|(text[3]&0x80) }; // set address C0H and add data
|
||||
|
||||
if (led_tm1637_write(write_data,LENGTH(write_data)) && led_tm1637_write(data,LENGTH(data))) { // send commands
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
uint8_t data[] = { 0xc0, ascii_7segments[(text[0]&0x7f)-' ']|(text[0]&0x80), ascii_7segments[(text[1]&0x7f)-' ']|(text[1]&0x80), ascii_7segments[(text[2]&0x7f)-' ']|(text[2]&0x80), ascii_7segments[(text[3]&0x7f)-' ']|(text[3]&0x80) }; // encode text on 7 digits
|
||||
return led_tm1637_send_data(data, LENGTH(data)); // transmit data
|
||||
}
|
||||
|
|
|
@ -12,11 +12,12 @@
|
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
/** library to communicate with a Titan Micro TM1637 IC attached to a 4-digit 7-segment (API)
|
||||
/** library to communicate with a Titan Micro TM1637 IC attached to a 7-segment displays (API)
|
||||
* @file led_tm1637.h
|
||||
* @author King Kévin <kingkevin@cuvoodoo.info>
|
||||
* @date 2017
|
||||
* @note peripherals used: GPIO @ref led_tm1637_gpio, timer @ref led_tm1637_timer
|
||||
* @note only 4-digit 7-segment displays are considered as this is the most common case
|
||||
* @warning all calls are blocking
|
||||
*/
|
||||
#pragma once
|
||||
|
@ -40,16 +41,11 @@ void led_tm1637_setup(void);
|
|||
/** switch display on
|
||||
* @return if transmission succeeded
|
||||
*/
|
||||
bool led_tm1637_on(void);
|
||||
/** switch display off
|
||||
* @return if transmission succeeded
|
||||
*/
|
||||
bool led_tm1637_off(void);
|
||||
/** set display brightness
|
||||
* @param[in] brightness brightness level to set
|
||||
* @return if transmission succeeded
|
||||
*/
|
||||
bool led_tm1637_brightness(enum led_tm1637_brightness_t brightness);
|
||||
void led_tm1637_brightness(enum led_tm1637_brightness_t brightness);
|
||||
/** display number
|
||||
* @param[in] number number to display (0-9999)
|
||||
* @return if transmission succeeded
|
||||
|
@ -68,4 +64,3 @@ bool led_tm1637_time(uint8_t hours, uint8_t minutes);
|
|||
* @return if transmission succeeded
|
||||
*/
|
||||
bool led_tm1637_text(char* text);
|
||||
|
||||
|
|
Loading…
Reference in New Issue