Skip to content

Commit

Permalink
Added SPI support and API usage example
Browse files Browse the repository at this point in the history
  • Loading branch information
hedgehogV committed Feb 10, 2019
1 parent df1e5f1 commit 1723349
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 30 deletions.
31 changes: 30 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ Based on the bitbanging efforts by [anxzhu](https://github.com/anxzhu) (2016-201
APIs rewritten in 2018 to follow the LiquidCrystal format by [valerio\new]
(https://github.com/5N44P).

Refactored. Removed dependency on any MCU hardware by Viacheslav Balandin
Refactored. Removed dependency on any MCU hardware. Added SPI support
by Viacheslav Balandin
https://github.com/hedgehogV/HT1621-lcd


Expand All @@ -16,6 +17,9 @@ https://github.com/hedgehogV/HT1621-lcd
* `HT1621(pPinSet *pCs, pPinSet *pSck, pPinSet *pMosi, pPinSet *pBacklight)`
Ctor. Starts the lcd with the pin assignement declared. The backlight pin is optional

* `HT1621(pInterface *pSpi, pPinSet *pCs, pPinSet *pBacklight)`
Starts the lcd with SPI interface. CS and backlight pins are optional. Tested with CPOL=LOW, EDGE=1

* `void clear()`
Clears the display

Expand Down Expand Up @@ -45,6 +49,31 @@ Turns off the display (doesn't turn off the backlight)
* `void displayOn()`
Turns the display back on after it has been disabled by `noDisplay()`

## API usage

To start display you have to call one of Ctors with pointers to toggle_Gpio or SpiTx functions.
You may need a function wrapper. Wrapper example:

```cpp
// spi wrapper
void SpiTx(uint8_t *ptr, uint8_t size)
{
HAL_SPI_Transmit(&hspi2, ptr, size, 2000);
}

// cs wrapper
void toggle_CS(bool b)
{
HAL_GPIO_WritePin(CS_GPIO_Port, CS_Pin, b? GPIO_PIN_SET:GPIO_PIN_RESET);
}

// ctor call example
HT1621 lcd = HT1621(&SpiTx, &toggle_CS);

// prints "hello" on display
lcd.print("HELLO");
```
## Internal functioning
Expand Down
71 changes: 44 additions & 27 deletions src/HT1621.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include "math.h"
#include "stdio.h"
#include "string.h"
#include <algorithm>

/**
* @brief CALCULATION DEFINES BLOCK
Expand Down Expand Up @@ -119,7 +120,6 @@ union tDataSeq
};


// TODO: give wrapper example for GPIO toggle in README and in hpp
HT1621::HT1621(pPinSet *pCs, pPinSet *pSck, pPinSet *pMosi, pPinSet *pBacklight)
{
pCsPin = pCs;
Expand All @@ -135,6 +135,19 @@ HT1621::HT1621(pPinSet *pCs, pPinSet *pSck, pPinSet *pMosi, pPinSet *pBacklight)
wrCmd(LCDON);
}

HT1621::HT1621(pInterface *pSpi, pPinSet *pCs, pPinSet *pBacklight)
{
pSpiInterface = pSpi;
pCsPin = pCs;
pBacklightPin = pBacklight;

wrCmd(BIAS);
wrCmd(RC256);
wrCmd(SYSDIS);
wrCmd(WDTDIS1);
wrCmd(SYSEN);
wrCmd(LCDON);
}

void HT1621::backlightOn()
{
Expand All @@ -158,25 +171,42 @@ void HT1621::displayOff()
wrCmd(LCDOFF);
}

void HT1621::wrBits(uint8_t bitField, uint8_t cnt)
void HT1621::wrBytes(uint8_t *ptr, uint8_t size)
{
if (!pSckPin || !pMosiPin)
return;
// probably need to use microsecond delays in this method
// after every pin toggle function to give display
// driver time for data reading. But current solution works for me

for (int i = 0; i < cnt; i++)
// lcd driver expects data in reverse order
std::reverse(ptr, ptr + size);

if (pCsPin)
pCsPin(LOW);

if (pSpiInterface)
pSpiInterface(ptr, size);
else if (pSckPin && pMosiPin)
{
pSckPin(LOW);
pMosiPin((bitField & 0x80)? HIGH : LOW);
pSckPin(HIGH);
bitField <<= 1;
for (int k = 0; k < size; k++)
{
uint8_t byte = ptr[k];

for (int i = 0; i < BITS_PER_BYTE; i++)
{
pSckPin(LOW);
pMosiPin((byte & 0x80)? HIGH : LOW);
pSckPin(HIGH);
byte <<= 1;
}
}
}

if (pCsPin)
pCsPin(HIGH);
}

void HT1621::wrBuffer()
{
if (!pCsPin)
return;

tDataSeq dataSeq = {};
dataSeq.type = MODE_DATA;
dataSeq.addr = 0;
Expand All @@ -185,29 +215,16 @@ void HT1621::wrBuffer()
dataSeq.data1 = (_buffer[3] << 8) + _buffer[2];
dataSeq.data2 = (_buffer[1] << 8) + _buffer[0];

pCsPin(LOW);

for (int i = 0; i < (int)sizeof(tDataSeq); i++)
{
wrBits(dataSeq.arr[7 - i], 8);
}

pCsPin(HIGH);
wrBytes(dataSeq.arr, sizeof(tDataSeq));
}

void HT1621::wrCmd(uint8_t cmd)
{
if (!pCsPin)
return;

tCmdSeq CommandSeq = {};
CommandSeq.type = MODE_CMD;
CommandSeq.data = cmd;

pCsPin(LOW);
wrBits(CommandSeq.arr[1], 8);
wrBits(CommandSeq.arr[0], 8);
pCsPin(HIGH);
wrBytes(CommandSeq.arr, sizeof(tCmdSeq));
}

void HT1621::batteryLevel(tBatteryLevel level)
Expand Down
17 changes: 15 additions & 2 deletions src/HT1621.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class HT1621
public:

using pPinSet = void(bool);
using pInterface = void(uint8_t *, uint8_t);

/**
* @brief Construct a new HT1621 object
Expand All @@ -53,6 +54,17 @@ class HT1621
*/
HT1621(pPinSet *pCs, pPinSet *pSck, pPinSet *pMosi, pPinSet *pBacklight = nullptr);

/**
* @brief Construct a new HT1621::HT1621 object
*
* Starts the lcd with SPI interface. CS and backlight pins are optional
*
* @param pSpi - pointer to SPI write function
* @param pCs - pointer to CS pin toggle function. Optional if SPI has hardware CS configured
* @param pBacklight - pointer to backlight pin toggle function. Optional
*/
HT1621(pInterface *pSpi, pPinSet *pCs = nullptr, pPinSet *pBacklight = nullptr);

/**
* @brief Turns on the display (doesn't affect the backlight)
*/
Expand Down Expand Up @@ -131,9 +143,10 @@ class HT1621
pPinSet *pSckPin = nullptr; // for display it is WR pin
pPinSet *pMosiPin = nullptr; // for display it is Data pin
pPinSet *pBacklightPin = nullptr; // display backlight pin
pInterface *pSpiInterface = nullptr; // ptr to SPI_tx implementation

// the most low-level function. Just put one bit into display
void wrBits(uint8_t bitField, uint8_t cnt);
// the most low-level function. Sends array of bytes into display
void wrBytes(uint8_t* ptr, uint8_t size);
// write _buffer to the display
void wrBuffer();
// write command sequance to display
Expand Down

0 comments on commit 1723349

Please sign in to comment.