Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

7-seg Display Driver Validation #218

Merged
merged 1 commit into from
Oct 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 61 additions & 48 deletions libraries/ms-drivers/src/seg_display.c
Original file line number Diff line number Diff line change
@@ -1,79 +1,92 @@
#include "seg_display.h"

#include "log.h"
#include "delay.h"

void set_seg_display(SegDisplay *display, uint8_t disp_value, uint8_t pin_number);
#define MAX_DISPLAY_VALUE 999
#define DELAY_BETWEEN_DIGITS_MS 3

void set_seg_display(SegDisplay *display, uint16_t val);
void seg_display_reset(SegDisplay *display);
void set_digit(SegDisplay *display, GpioAddress *digit, uint16_t val);

StatusCode seg_display_init(SegDisplay *display) {
gpio_init_pin(&display->A, GPIO_OUTPUT_PUSH_PULL, GPIO_STATE_LOW);
gpio_init_pin(&display->B, GPIO_OUTPUT_PUSH_PULL, GPIO_STATE_LOW);
gpio_init_pin(&display->C, GPIO_OUTPUT_PUSH_PULL, GPIO_STATE_LOW);
gpio_init_pin(&display->D, GPIO_OUTPUT_PUSH_PULL, GPIO_STATE_LOW);
gpio_init_pin(&display->D, GPIO_OUTPUT_PUSH_PULL, GPIO_STATE_HIGH);
gpio_init_pin(&display->DP, GPIO_OUTPUT_PUSH_PULL, GPIO_STATE_LOW);
gpio_init_pin(&display->D1, GPIO_OUTPUT_PUSH_PULL, GPIO_STATE_LOW);
gpio_init_pin(&display->D2, GPIO_OUTPUT_PUSH_PULL, GPIO_STATE_LOW);
gpio_init_pin(&display->D3, GPIO_OUTPUT_PUSH_PULL, GPIO_STATE_LOW);

set_seg_display(display, 8, 0);
set_seg_display(display, 8, 1);
set_seg_display(display, 8, 2);
return STATUS_CODE_OK;
}

void seg_display_reset(SegDisplay *display) {
gpio_set_state(&display->A, GPIO_STATE_LOW);
gpio_set_state(&display->B, GPIO_STATE_LOW);
gpio_set_state(&display->C, GPIO_STATE_LOW);
gpio_set_state(&display->D, GPIO_STATE_LOW);
gpio_set_state(&display->DP, GPIO_STATE_HIGH);
gpio_set_state(&display->D1, GPIO_STATE_HIGH);
gpio_set_state(&display->D2, GPIO_STATE_HIGH);
gpio_set_state(&display->D3, GPIO_STATE_HIGH);
}

void set_seg_display(SegDisplay *display, uint16_t val) { // Sets ABCD according to value
GpioState state;

state = ((val & 8) != 0) ? GPIO_STATE_HIGH : GPIO_STATE_LOW;
gpio_set_state(&display->D, state);

state = ((val & 4) != 0) ? GPIO_STATE_HIGH : GPIO_STATE_LOW;
gpio_set_state(&display->C, state);

state = ((val & 2) != 0) ? GPIO_STATE_HIGH : GPIO_STATE_LOW;
gpio_set_state(&display->B, state);

state = ((val & 1) != 0) ? GPIO_STATE_HIGH : GPIO_STATE_LOW;
gpio_set_state(&display->A, state);
}

void set_digit(SegDisplay *display, GpioAddress *digit, uint16_t val) { // Sets D1 D2 D3
gpio_set_state(digit, GPIO_STATE_LOW);
set_seg_display(display, val);
delay_ms(DELAY_BETWEEN_DIGITS_MS);
gpio_set_state(digit, GPIO_STATE_HIGH);
}

StatusCode seg_display_set_int(SegDisplay *display, uint16_t val) {
// Check for max value
if (val > 999) {
if (val > MAX_DISPLAY_VALUE) {
return STATUS_CODE_OUT_OF_RANGE;
} else {
set_seg_display(display, val % 10, 0);
val = val / 10;
set_seg_display(display, val % 10, 1);
val = val / 10;
set_seg_display(display, val % 10, 2);
seg_display_reset(display);

set_digit(display, &display->D1, (val / 100));
set_digit(display, &display->D2, ((val / 10) % 10));
set_digit(display, &display->D3, (val % 10));

return STATUS_CODE_OK;
}
}

StatusCode seg_display_set_float(SegDisplay *display, float val) {
int int_part = (int)val;
int dec_part = (int)((val - (float)int_part) * 10);
if (int_part > 99) {
uint16_t val_int = val * 10;

if (val_int > MAX_DISPLAY_VALUE) {
return STATUS_CODE_OUT_OF_RANGE;
} else {
set_seg_display(display, int_part % 10, 1);
int_part = int_part / 10;
set_seg_display(display, int_part % 10, 2);
set_seg_display(display, dec_part, 0);
// Set decimal point
seg_display_reset(display);

set_digit(display, &display->D1, (val_int / 100));

gpio_set_state(&display->DP, GPIO_STATE_LOW); // Enable decimal point
set_digit(display, &display->D2, ((val_int / 10) % 10));

gpio_set_state(&display->DP, GPIO_STATE_HIGH);
return STATUS_CODE_OK;
}
}
set_digit(display, &display->D3, (val_int % 10));

void set_seg_display(SegDisplay *display, uint8_t disp_value, uint8_t pin_number) {
// Sets A-D with the binary digits of 8
GpioState state_a = disp_value & 0x1;
GpioState state_b = disp_value & 0x2;
GpioState state_c = disp_value & 0x4;
GpioState state_d = disp_value & 0x8;

// Uses values of A, B, C, D to set bcd_pins' states
gpio_set_state(&display->A, state_a);
gpio_set_state(&display->B, state_b);
gpio_set_state(&display->C, state_c);
gpio_set_state(&display->D, state_d);

// Toggles the disp_sel_pin specified twice to set the value in the display
if (pin_number == 0) {
gpio_toggle_state(&display->D1);
gpio_toggle_state(&display->D1);
}
if (pin_number == 1) {
gpio_toggle_state(&display->D2);
gpio_toggle_state(&display->D2);
}
if (pin_number == 2) {
gpio_toggle_state(&display->D3);
gpio_toggle_state(&display->D3);
return STATUS_CODE_OK;
}
}
Loading