Skip to content

Commit

Permalink
* Code cleanup.
Browse files Browse the repository at this point in the history
* Made the configure() function of PinDriver interface optional to allow manual pin configuration.
  • Loading branch information
burjui committed Feb 3, 2012
1 parent c1b6abf commit 755e041
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 21 deletions.
25 changes: 14 additions & 11 deletions hd44780.c
Expand Up @@ -29,7 +29,6 @@ HD44780_Result hd44780_init(HD44780 *display, HD44780_Mode mode,
{
HD44780_RETURN_ASSERT(display != NULL, HD44780_ERROR);
HD44780_RETURN_ASSERT(hal != NULL, HD44780_ERROR);
HD44780_RETURN_ASSERT(hal->pins.configure != NULL, HD44780_ERROR);
HD44780_RETURN_ASSERT(hal->pins.write != NULL, HD44780_ERROR);
HD44780_RETURN_ASSERT(hal->delay_microseconds != NULL, HD44780_ERROR);
HD44780_RETURN_ASSERT(pinout != NULL, HD44780_ERROR);
Expand All @@ -39,17 +38,20 @@ HD44780_Result hd44780_init(HD44780 *display, HD44780_Mode mode,
display->hal = *hal;
display->pinout = *pinout;

display->hal.pins.configure(&display->pinout.rs, HD44780_PIN_OUTPUT);
display->hal.pins.configure(&display->pinout.en, HD44780_PIN_OUTPUT);
if (display->hal.pins.configure != NULL)
{
display->hal.pins.configure(&display->pinout.rs, HD44780_PIN_OUTPUT);
display->hal.pins.configure(&display->pinout.en, HD44780_PIN_OUTPUT);

if (display->pinout.rw.gpio != NULL)
display->hal.pins.configure(&display->pinout.rw, HD44780_PIN_OUTPUT);
if (display->pinout.rw.gpio != NULL)
display->hal.pins.configure(&display->pinout.rw, HD44780_PIN_OUTPUT);

if (display->pinout.backlight.gpio != NULL)
display->hal.pins.configure(&display->pinout.backlight, HD44780_PIN_OUTPUT);
}

if (display->pinout.backlight.gpio != NULL)
{
display->hal.pins.configure(&display->pinout.backlight, HD44780_PIN_OUTPUT);
display->hal.pins.write(&display->pinout.backlight, HD44780_PIN_LOW);
}

if (mode == HD44780_MODE_4BIT)
{
Expand Down Expand Up @@ -273,7 +275,7 @@ HD44780_Result hd44780_right_to_left(HD44780 *display)
return hd44780_command(display, HD44780_CMD_ENTRYMODESET | display->displaymode);
}

/* FIXME moves the cursor off screen */
/* FIXME moves the cursor out of screen */
HD44780_Result hd44780_create_char(HD44780 *display, uint8_t location, const uint8_t *charmap)
{
HD44780_RETURN_ASSERT(display != NULL, HD44780_ERROR);
Expand Down Expand Up @@ -309,12 +311,13 @@ HD44780_Result hd44780_config(HD44780 *display)
{
HD44780_RETURN_ASSERT(display != NULL, HD44780_ERROR);
HD44780_RETURN_ASSERT(display->hal.delay_microseconds != NULL, HD44780_ERROR);
HD44780_RETURN_ASSERT(display->hal.pins.configure != NULL, HD44780_ERROR);
HD44780_RETURN_ASSERT(display->hal.pins.write != NULL, HD44780_ERROR);

for (unsigned i = 0; i < display->dp_amount; ++i)
{
display->hal.pins.configure(&display->pinout.dp[display->dp_offset + i], HD44780_PIN_OUTPUT);
if (display->hal.pins.configure != NULL)
display->hal.pins.configure(&display->pinout.dp[display->dp_offset + i], HD44780_PIN_OUTPUT);

display->hal.pins.write(&display->pinout.dp[display->dp_offset + i], HD44780_PIN_LOW);
}

Expand Down
20 changes: 10 additions & 10 deletions hd44780.h
Expand Up @@ -60,17 +60,20 @@ typedef struct
typedef struct
{
HD44780_Pin rs; // LOW: command. HIGH: character.
HD44780_Pin en; // activated by a HIGH pulse.
HD44780_Pin rw; // LOW: write to LCD. HIGH: read from LCD.
HD44780_Pin backlight; // should be connected to base/gate of transistor/FET
HD44780_Pin dp[8]; // data pins
HD44780_Pin en; // activated by a HIGH pulse.
HD44780_Pin rw; // optional; LOW: write to LCD. HIGH: read from LCD.
HD44780_Pin backlight; // optional; should be connected to base/gate of transistor/FET
HD44780_Pin dp[8]; // data pins DP0..DP7; DP0..DP3 are optional if using 4-bit mode
} HD44780_Pinout;

typedef enum { HD44780_OK, HD44780_ERROR } HD44780_Result;
typedef enum { HD44780_PIN_INPUT, HD44780_PIN_OUTPUT } HD44780_PinMode;
typedef enum { HD44780_PIN_LOW, HD44780_PIN_HIGH } HD44780_PinState;

/* Hardware-dependent pin control interface */
/* Hardware-dependent pin control interface.
* configure() function is optional if you want to configure
* the display pins manually.
*/
typedef struct
{
HD44780_Result (*configure)(HD44780_Pin *pin, HD44780_PinMode mode);
Expand Down Expand Up @@ -142,15 +145,12 @@ HD44780_Result hd44780_write8bits(HD44780 *display, uint8_t value);
HD44780_Result hd44780_write4bits(HD44780 *display, uint8_t value);
HD44780_Result hd44780_pulse_enable_pin(HD44780 *display);

#define HD44780_MAKE_8BITS(b0,b1,b2,b3,b4,b5,b6,b7) \
#define HD44780_MAKE_5BITS(b4,b3,b2,b1,b0) \
(((b0) & 1) | \
((b1) & 1) << 1 | \
((b2) & 1) << 2 | \
((b3) & 1) << 3 | \
((b4) & 1) << 4 | \
((b5) & 1) << 5 | \
((b6) & 1) << 6 | \
((b7) & 1) << 7)
((b4) & 1) << 4)

#ifdef __cplusplus
}
Expand Down

0 comments on commit 755e041

Please sign in to comment.