1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

drivers/digit7seg: change _set_pin_value function

drivers/digit7seg: add return value doc in header and remove unused params in digit7seg_t

drivers/digit7seg: add return value doc in header

drivers/digit7seg: add return value doc in header
This commit is contained in:
Pierre Le Meur 2024-11-15 10:48:09 +01:00
parent 4d541ba50a
commit 37ceefb9d1
4 changed files with 24 additions and 60 deletions

View File

@ -33,6 +33,7 @@
/** The 7 segments + decimal point + the number of digits */
#define NB_PIN (8+dev->params.digits)
#define BYTE_BITS (0x08)
static void _set_pin_value(digit7seg_t *dev)
{
@ -44,13 +45,13 @@ static void _set_pin_value(digit7seg_t *dev)
uint8_t mask = 0xFF;
uint8_t current_value = (uint8_t)(dev->value >> (dev->current_digit * 8) & mask);
for (int i = 1; i <= 8; i++) {
if (current_value & (1 << (8 - i))) {
gpio_set(pins[i - 1]);
for (int i = 0; i < BYTE_BITS; i++) {
if (current_value & (1 << i)) {
gpio_set(pins[i]);
DEBUG("PIN SET\n");
}
else {
gpio_clear(pins[i - 1]);
gpio_clear(pins[i]);
DEBUG("PIN CLEAR\n");
}
}
@ -81,7 +82,6 @@ int digit7seg_init(digit7seg_t *dev, const digit7seg_params_t *params)
dev->params = *params;
dev->current_digit = 0;
dev->value = 0;
dev->status = TIMER_STOPPED;
if (dev->params.digits <= 0 || dev->params.digits > DIGIT7SEG_MAX_DIGITS) {
DEBUG("[Error] Invalid number of digit.\n");
@ -119,16 +119,9 @@ int digit7seg_init(digit7seg_t *dev, const digit7seg_params_t *params)
return DIGIT7SEG_OK;
}
int digit7seg_shift(digit7seg_t *dev)
{
_shift_display(dev, 0);
return 0;
}
int digit7seg_set_all_value(digit7seg_t *dev, uint32_t value)
void digit7seg_set_all_value(digit7seg_t *dev, uint32_t value)
{
dev->value = value;
return 0;
}
int digit7seg_set_value(digit7seg_t *dev, int index, uint8_t value)
@ -137,10 +130,10 @@ int digit7seg_set_value(digit7seg_t *dev, int index, uint8_t value)
return -1;
}
uint32_t temp_value = value << (index * 8);
uint32_t up_value = dev->value >> ((index + 1) * 8);
up_value <<= ((index + 1) * 8);
uint32_t down_value = ((0b00000001 << (index * 8)) - 1) & dev->value;
uint32_t temp_value = value << (index * BYTE_BITS);
uint32_t up_value = dev->value >> ((index + 1) * BYTE_BITS);
up_value <<= ((index + 1) * BYTE_BITS);
uint32_t down_value = ((0b00000001 << (index * BYTE_BITS)) - 1) & dev->value;
dev->value = up_value | temp_value | down_value;
@ -149,31 +142,17 @@ int digit7seg_set_value(digit7seg_t *dev, int index, uint8_t value)
int digit7seg_poweron(digit7seg_t *dev)
{
if (dev->status == TIMER_RUNNING) {
DEBUG("[Error] Timer is already running.\n");
return -1;
}
if (timer_init(dev->params.timer, DIGIT7SEG_TIMER_HZ, _shift_display, dev) != 0) {
DEBUG("[Error] Impossible to init timer.\n");
DEBUG("[Error] Not possible to init timer.\n");
return -1;
}
timer_set_periodic(dev->params.timer, 0, DIGIT7SEG_DELAY, TIM_FLAG_RESET_ON_MATCH);
dev->status = TIMER_RUNNING;
return 0;
}
int digit7seg_poweroff(digit7seg_t *dev)
void digit7seg_poweroff(digit7seg_t *dev)
{
if (dev->status == TIMER_STOPPED) {
DEBUG("[Error] Timer is already stopped.\n");
return -1;
}
timer_stop(dev->params.timer);
dev->status = TIMER_STOPPED;
return 0;
}

View File

@ -77,7 +77,7 @@ and turn on each digit at a rate that is imperceptible to the human eye.
## Error Handling
All driver functions return 0 on success or one of a negative error code defined by
Most of driver functions return 0 on success or one of a negative error code defined by
#digit7seg_error_codes.
## Usage

View File

@ -20,9 +20,9 @@
#include <inttypes.h>
#include "macros/units.h"
#include "periph/gpio.h"
#include "periph/timer.h"
#include "macros/units.h"
#ifdef __cplusplus
extern "C" {
@ -39,8 +39,6 @@ extern "C" {
* */
# define DIGIT7SEG_DELAY (DIGIT7SEG_TIMER_HZ / 2600)
#endif
#define TIMER_RUNNING (1) /**< status when timer is running */
#define TIMER_STOPPED (0) /**< status when timer is stopped */
/**
* @brief Return codes for @ref digit7seg_init
@ -101,27 +99,18 @@ typedef struct {
digit7seg_params_t params; /**< Device parameters */
uint32_t value; /**< Binary value to display */
uint8_t current_digit; /**< Displayed digit */
uint8_t status; /**< Status of the timer_periodic for this dev */
} digit7seg_t;
/**
* @brief Initialize the given DIGIT7SEG
*
* @param[out] dev Initialized device descriptor of DIGIT7SEG device
* @param[out] dev DIGIT7SEG device descriptor to initialize
* @param[in] params Device parameters to use
*
* @return 0 on success or negative error code, see #digit7seg_error_codes
*/
int digit7seg_init(digit7seg_t *dev, const digit7seg_params_t *params);
/**
* @brief Shift the current digit off and the next one on
*
* @param[in] dev Initialized device descriptor of DIGIT7SEG device
*
*/
int digit7seg_shift(digit7seg_t *dev);
/**
* @brief Set the value for every digit
*
@ -129,7 +118,7 @@ int digit7seg_shift(digit7seg_t *dev);
* @param[in] value the value as an uint32_t
*
*/
int digit7seg_set_all_value(digit7seg_t *dev, uint32_t value);
void digit7seg_set_all_value(digit7seg_t *dev, uint32_t value);
/**
* @brief Set the value for one digit
@ -139,6 +128,8 @@ int digit7seg_set_all_value(digit7seg_t *dev, uint32_t value);
* @param[in] value the value as an uint8_t, a is equal to the first bit
* b the second.... dp the 8th bit.
*
* @return 0 on success
* @return -1 when an incorrect digit is passed
*/
int digit7seg_set_value(digit7seg_t *dev, int index, uint8_t value);
@ -147,7 +138,8 @@ int digit7seg_set_value(digit7seg_t *dev, int index, uint8_t value);
*
* @param[in] dev Initialized device descriptor of DIGIT7SEG device
*
* @return -1 if the digit was already power on
* @return 0 on success
* @return -1 if the timer was impossible to start
*/
int digit7seg_poweron(digit7seg_t *dev);
@ -155,10 +147,8 @@ int digit7seg_poweron(digit7seg_t *dev);
* @brief Stop an periodic timer event to shift between each 7seg
*
* @param[in] dev Initialized device descriptor of DIGIT7SEG device
*
* @return -1 if the digit was already power off
*/
int digit7seg_poweroff(digit7seg_t *dev);
void digit7seg_poweroff(digit7seg_t *dev);
#ifdef __cplusplus
}

View File

@ -59,8 +59,8 @@ int main(void)
ztimer_sleep(ZTIMER_USEC, TEST_DELAY_US);
/* R I O T */
/* 11101110 00001100 11111100 00001110 */
uint32_t binary_riot = 0b11101110000011001111110000001110;
/* 01110111 00110000 00111111 01110000 */
uint32_t binary_riot = 0b01110111001100000011111101110000;
digit7seg_set_all_value(&dev, binary_riot);
if (digit7seg_poweron(&dev) == 0) {
@ -72,12 +72,7 @@ int main(void)
ztimer_sleep(ZTIMER_USEC, TEST_DELAY_US * 3);
if (digit7seg_poweroff(&dev) == 0) {
puts("...Stopped");
}
else {
puts("Error");
}
digit7seg_poweroff(&dev);
ztimer_sleep(ZTIMER_USEC, TEST_DELAY_US * 3);