//*****************************************************************************
//
//! Clears the OLED display.
//!
//! This function will clear the display.  All pixels in the display will be
//! turned off.
//!
//! \return None.
//
//*****************************************************************************
void
Display96x16x1Clear(void)
{
    unsigned long ulIdx;

    //
    // Move the display cursor to the first column of the first row.
    //
    Display96x16x1WriteFirst(0x80);
    Display96x16x1WriteArray(g_pucRow1, SIZE_CURSOR_ROW_COMMAND);

    //
    // Fill this row with zeros.
    //
    for(ulIdx = 0; ulIdx < 95; ulIdx++)
    {
        Display96x16x1WriteByte(0x00);
    }
    Display96x16x1WriteFinal(0x00);

    //
    // Move the display cursor to the first column of the second row.
    //
    Display96x16x1WriteFirst(0x80);
    Display96x16x1WriteArray(g_pucRow2, SIZE_CURSOR_ROW_COMMAND);

    //
    // Fill this row with zeros.
    //
    for(ulIdx = 0; ulIdx < 95; ulIdx++)
    {
        Display96x16x1WriteByte(0x00);
    }
    Display96x16x1WriteFinal(0x00);
}
//*****************************************************************************
//
//! Displays an image on the OLED display.
//!
//! \param pucImage is a pointer to the image data.
//! \param ulX is the horizontal position to display this image, specified in
//! columns from the left edge of the display.
//! \param ulY is the vertical position to display this image, specified in
//! eight scan line blocks from the top of the display (that is, only 0 and 1
//! are valid).
//! \param ulWidth is the width of the image, specified in columns.
//! \param ulHeight is the height of the image, specified in eight row blocks
//! (that is, only 1 and 2 are valid).
//!
//! This function will display a bitmap graphic on the display.  The image to
//! be displayed must be a multiple of eight scan lines high (that is, one row)
//! and will be drawn at a vertical position that is a multiple of eight scan
//! lines (that is, scan line zero or scan line eight, corresponding to row
//! zero or row one).
//!
//! The image data is organized with the first row of image data appearing left
//! to right, followed immediately by the second row of image data.  Each byte
//! contains the data for the eight scan lines of the column, with the top scan
//! line being in the least significant bit of the byte and the bottom scan
//! line being in the most significant bit of the byte.
//!
//! For example, an image four columns wide and sixteen scan lines tall would
//! be arranged as follows (showing how the eight bytes of the image would
//! appear on the display):
//!
//! \verbatim
//!     +-------+  +-------+  +-------+  +-------+
//!     |   | 0 |  |   | 0 |  |   | 0 |  |   | 0 |
//!     | B | 1 |  | B | 1 |  | B | 1 |  | B | 1 |
//!     | y | 2 |  | y | 2 |  | y | 2 |  | y | 2 |
//!     | t | 3 |  | t | 3 |  | t | 3 |  | t | 3 |
//!     | e | 4 |  | e | 4 |  | e | 4 |  | e | 4 |
//!     |   | 5 |  |   | 5 |  |   | 5 |  |   | 5 |
//!     | 0 | 6 |  | 1 | 6 |  | 2 | 6 |  | 3 | 6 |
//!     |   | 7 |  |   | 7 |  |   | 7 |  |   | 7 |
//!     +-------+  +-------+  +-------+  +-------+
//!
//!     +-------+  +-------+  +-------+  +-------+
//!     |   | 0 |  |   | 0 |  |   | 0 |  |   | 0 |
//!     | B | 1 |  | B | 1 |  | B | 1 |  | B | 1 |
//!     | y | 2 |  | y | 2 |  | y | 2 |  | y | 2 |
//!     | t | 3 |  | t | 3 |  | t | 3 |  | t | 3 |
//!     | e | 4 |  | e | 4 |  | e | 4 |  | e | 4 |
//!     |   | 5 |  |   | 5 |  |   | 5 |  |   | 5 |
//!     | 4 | 6 |  | 5 | 6 |  | 6 | 6 |  | 7 | 6 |
//!     |   | 7 |  |   | 7 |  |   | 7 |  |   | 7 |
//!     +-------+  +-------+  +-------+  +-------+
//! \endverbatim
//!
//! \return None.
//
//*****************************************************************************
void
Display96x16x1ImageDraw(const unsigned char *pucImage, unsigned long ulX,
                      unsigned long ulY, unsigned long ulWidth,
                      unsigned long ulHeight)
{
    //
    // Check the arguments.
    //
    ASSERT(ulX < 96);
    ASSERT(ulY < 2);
    ASSERT((ulX + ulWidth) <= 96);
    ASSERT((ulY + ulHeight) <= 2);

    //
    // The first few columns of the LCD buffer are not displayed, so increment
    // the X coorddinate by this amount to account for the non-displayed frame
    // buffer memory.
    //
    ulX += g_ucColumnAdjust;

    //
    // Loop while there are more rows to display.
    //
    while(ulHeight--)
    {
        //
        // Write the starting address within this row.
        //
        Display96x16x1WriteFirst(0x80);
        Display96x16x1WriteByte((ulY == 0) ? 0xb0 : 0xb1);
        Display96x16x1WriteByte(0x80);
        Display96x16x1WriteByte(ulX & 0x0f);
        Display96x16x1WriteByte(0x80);
        Display96x16x1WriteByte(0x10 | ((ulX >> 4) & 0x0f));
        Display96x16x1WriteByte(0x40);

        //
        // Write this row of image data.
        //
        Display96x16x1WriteArray(pucImage, ulWidth - 1);
        Display96x16x1WriteFinal(pucImage[ulWidth - 1]);

        //
        // Advance to the next row of the image.
        //
        pucImage += ulWidth;
        ulY++;
    }
}
//*****************************************************************************
//
//! Turns on the OLED display.
//!
//! This function will turn on the OLED display, causing it to display the
//! contents of its internal frame buffer.
//!
//! \return None.
//
//*****************************************************************************
void
Display96x16x1DisplayOn(void)
{
    uint32_t ulIdx;

    //
    // Re-initialize the display controller.  Loop through the initialization
    // sequence doing a single I2C transfer for each command.
    //
    for(ulIdx = 0; ulIdx < SIZE_INIT_CMDS;
        ulIdx += g_pucDisplayInit[ulIdx] + 1)
    {
        //
        // Send this command.
        //
        Display96x16x1WriteFirst(g_pucDisplayInit[ulIdx + 1]);
        Display96x16x1WriteArray(g_pucDisplayInit + ulIdx + 2,
                               g_pucDisplayInit[ulIdx] - 2);
        Display96x16x1WriteFinal(g_pucDisplayInit[ulIdx + g_pucDisplayInit[ulIdx]]);
    }
}
//*****************************************************************************
//
//! Turns on the OLED display.
//!
//! This function will turn on the OLED display, causing it to display the
//! contents of its internal frame buffer.
//!
//! \return None.
//
//*****************************************************************************
void
Display96x16x1DisplayOn(void)
{
    unsigned long ulIdx;

    //
    // Re-initialize the display controller.  Loop through the initialization
    // sequence doing a single I2C transfer for each command.
    //
    for(ulIdx = 0; ulIdx < sizeof(g_pucRITInit);
        ulIdx += g_pucRITInit[ulIdx] + 1)
    {
        //
        // Send this command.
        //
        Display96x16x1WriteFirst(g_pucRITInit[ulIdx + 1]);
        Display96x16x1WriteArray(g_pucRITInit + ulIdx + 2,
                                 g_pucRITInit[ulIdx] - 2);
        Display96x16x1WriteFinal(g_pucRITInit[ulIdx + g_pucRITInit[ulIdx]]);
    }
}
//*****************************************************************************
//
//! Clears a single line on the OLED display.
//!
//! \param ulY is the display line to be cleared, 0 or 1.
//!
//! This function will clear one text line of the display.
//!
//! \return None.
//
//*****************************************************************************
void
Display96x16x1ClearLine(unsigned long ulY)
{
    unsigned long ulIdx;

    //
    // Move the display cursor to the first column of the specified row.
    //
    Display96x16x1WriteFirst(0x80);
    Display96x16x1WriteArray(ulY ? g_pucRITRow2 : g_pucRITRow1,
                             sizeof(g_pucRITRow1));

    //
    // Fill this row with zeros.
    //
    for(ulIdx = 0; ulIdx < 95; ulIdx++)
    {
        Display96x16x1WriteByte(0x00);
    }
    Display96x16x1WriteFinal(0x00);
}
//*****************************************************************************
//
//! Initialize the OLED display.
//!
//! \param bFast is a boolean that is \e true if the I2C interface should be
//! run at 400 kbps and \e false if it should be run at 100 kbps.
//!
//! This function initializes the I2C interface to the OLED display and
//! configures the SSD0303 or SSD1300 controller on the panel.
//!
//! \return None.
//
//*****************************************************************************
void
Display96x16x1Init(tBoolean bFast)
{
    unsigned long ulIdx;

    //
    // Enable the I2C and GPIO port B blocks as they are needed by this driver.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);

#if (!(defined OSRAM_ONLY) && !(defined RIT_ONLY))
    //
    // Read SysCtl DID1 register to determine whether this is an older board
    // with the OSRAM display or a newer one with the RIT model.
    //
    g_ucDisplayIsRIT = (HWREG(SYSCTL_DID1) & (1 << 12)) ? 1 : 0;

    //
    // Set the correct number of non-displayed columns given the display type
    // we are using.
    //
    g_ucColumnAdjust = g_ucDisplayIsRIT ? 4 : 36;
#endif

    //
    // If using the RIT display, we need to enable power by pulling PD7 high.
    //
#ifndef OSRAM_ONLY
#ifndef RIT_ONLY
    if(g_ucDisplayIsRIT)
    {
#endif
        SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
        GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, GPIO_PIN_7);
        GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_7, GPIO_PIN_7);
#ifndef RIT_ONLY
    }
#endif
#endif

    //
    // Configure the I2C SCL and SDA pins for I2C operation.
    //
    GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_2 | GPIO_PIN_3);

    //
    // Initialize the I2C master.
    //
    I2CMasterInitExpClk(I2C0_MASTER_BASE, SysCtlClockGet(), bFast);

    //
    // Initialize the display controller.  Loop through the initialization
    // sequence doing a single I2C transfer for each command.
    //
    for(ulIdx = 0; ulIdx < SIZE_INIT_CMDS; ulIdx += g_pucDisplayInit[ulIdx] + 1)
    {
        //
        // Send this command.
        //
        Display96x16x1WriteFirst(g_pucDisplayInit[ulIdx + 1]);
        Display96x16x1WriteArray(g_pucDisplayInit + ulIdx + 2,
                               g_pucDisplayInit[ulIdx] - 2);
        Display96x16x1WriteFinal(g_pucDisplayInit[ulIdx +
                                                  g_pucDisplayInit[ulIdx]]);
    }

    //
    // Clear the frame buffer.
    //
    Display96x16x1Clear();
}
//*****************************************************************************
//
//! Displays a string on the OLED display.
//!
//! \param pcStr is a pointer to the string to display.
//! \param ulX is the horizontal position to display the string, specified in
//! columns from the left edge of the display.
//! \param ulY is the vertical position to display the string, specified in
//! eight scan line blocks from the top of the display (that is, only 0 and 1
//! are valid).
//!
//! This function will draw a string on the display.  Only the ASCII characters
//! between 32 (space) and 126 (tilde) are supported; other characters will
//! result in random data being draw on the display (based on whatever appears
//! before/after the font in memory).  The font is mono-spaced, so characters
//! such as ``i'' and ``l'' have more white space around them than characters
//! such as ``m'' or ``w''.
//!
//! If the drawing of the string reaches the right edge of the display, no more
//! characters will be drawn.  Therefore, special care is not required to avoid
//! supplying a string that is ``too long'' to display.
//!
//! \return None.
//
//*****************************************************************************
void
Display96x16x1StringDraw(const char *pcStr, unsigned long ulX, unsigned long ulY)
{
    //
    // Check the arguments.
    //
    ASSERT(ulX < 96);
    ASSERT(ulY < 2);

    //
    // Move the display cursor to the requested position on the display.
    //
    Display96x16x1WriteFirst(0x80);
    Display96x16x1WriteByte((ulY == 0) ? 0xb0 : 0xb1);
    Display96x16x1WriteByte(0x80);
    Display96x16x1WriteByte((ulX + g_ucColumnAdjust) & 0x0f);
    Display96x16x1WriteByte(0x80);
    Display96x16x1WriteByte(0x10 | (((ulX + g_ucColumnAdjust) >> 4) & 0x0f));
    Display96x16x1WriteByte(0x40);

    //
    // Loop while there are more characters in the string.
    //
    while(*pcStr != 0)
    {
        //
        // See if there is enough space on the display for this entire
        // character.
        //
        if(ulX <= 90)
        {
            //
            // Write the contents of this character to the display.
            //
            Display96x16x1WriteArray(g_pucFont[*pcStr - ' '], 5);

            //
            // See if this is the last character to display (either because the
            // right edge has been reached or because there are no more
            // characters).
            //
            if((ulX == 90) || (pcStr[1] == 0))
            {
                //
                // Write the final column of the display.
                //
                Display96x16x1WriteFinal(0x00);

                //
                // The string has been displayed.
                //
                return;
            }

            //
            // Write the inter-character padding column.
            //
            Display96x16x1WriteByte(0x00);
        }
        else
        {
            //
            // Write the portion of the character that will fit onto the
            // display.
            //
            Display96x16x1WriteArray(g_pucFont[*pcStr - ' '], 95 - ulX);
            Display96x16x1WriteFinal(g_pucFont[*pcStr - ' '][95 - ulX]);

            //
            // The string has been displayed.
            //
            return;
        }

        //
        // Advance to the next character.
        //
        pcStr++;

        //
        // Increment the X coordinate by the six columns that were just
        // written.
        //
        ulX += 6;
    }
}
//*****************************************************************************
//
//! Initialize the OLED display.
//!
//! \param bFast is a boolean that is \e true if the I2C interface should be
//! run at 400 kbps and \e false if it should be run at 100 kbps.
//!
//! This function initializes the I2C interface to the OLED display and
//! configures the SSD0303 or SSD1300 controller on the panel.
//!
//! \return None.
//
//*****************************************************************************
void
Display96x16x1Init(uint8_t bFast)
{
    uint32_t ulTmp;

    //
    // Enable the I2C and GPIO port B blocks as they are needed by this driver.
    //
    SYSCTL->RCGC1 |= (1 << 12);                    /* enable clock to I2C0  */
    SYSCTL->RCGC2 |= (1 <<  1);                    /* enable clock to GPIOB */

#if (!(defined OSRAM_ONLY) && !(defined RIT_ONLY))
    //
    // Read SysCtl DID1 register to determine whether this is an older board
    // with the OSRAM display or a newer one with the RIT model.
    //
    g_ucDisplayIsRIT = (SYSCTL->DID1 & (1 << 12)) ? 1 : 0;

    //
    // Set the correct number of non-displayed columns given the display type
    // we are using.
    //
    g_ucColumnAdjust = g_ucDisplayIsRIT ? 4 : 36;
#endif

    //
    // If using the RIT display, we need to enable power by pulling PD7 high.
    //
#ifndef OSRAM_ONLY
#ifndef RIT_ONLY
    if(g_ucDisplayIsRIT)
    {
#endif
        SYSCTL->RCGC2 |= (1 <<  3);                /* enable clock to GPIOD */
        SysCtlDelay(1);            /* wait a tiny bit after enabling clocks */

        GPIOD->DIR |= (1 << 7);       /* set GPIOD-pin7 direction to output */
        GPIOD->DATA_Bits[1 << 7] = (1 << 7);       /* drive GPIOD-pin7 high */
#ifndef RIT_ONLY
    }
#endif
#endif

    //
    // Configure the I2C SCL and SDA pins for I2C operation.
    //
    ulTmp = (1 << 2) | (1 << 3);
    GPIOB->DIR   &= ~ulTmp;
    GPIOB->AFSEL |= ulTmp;
    GPIOB->DR2R  |= ulTmp;      /* set 2mA drive, DR4R and DR8R are cleared */
    GPIOB->SLR   &= ~ulTmp;
    GPIOB->ODR   |= ulTmp;
    GPIOB->PUR   |= ulTmp;              /* set weak pull-up; PDR is cleared */
    GPIOB->DEN   |= ulTmp;
    GPIOB->AMSEL &= ~ulTmp;

    //
    // Initialize the I2C master.
    //
    I2C0_MASTER->MCR |= (1 << 4);                      /* I2C master enable */
    if (bFast) {
        ulTmp = 400000;
    }
    else {
        ulTmp = 100000;
    }
    I2C0_MASTER->MTPR = ((SystemFrequency + (2 * 10 * ulTmp) - 1)
                          / (2 * 10 * ulTmp)) - 1;

    //
    // Compute the inter-byte delay for the display controller.  This delay is
    // dependent upon the I2C bus clock rate; the slower the clock the longer
    // the delay required.
    //
    // The derivation of this formula is based on a measured delay of
    // SysCtlDelay(1700) for a 100 kHz I2C bus with the CPU running at 50 MHz
    // (referred to as C).  To scale this to the delay for a different CPU
    // speed (since this is just a CPU-based delay loop) is:
    //
    //           f(CPU)
    //     C * ----------
    //         50,000,000
    //
    // To then scale this to the actual I2C rate (since it won't always be
    // precisely 100 kHz):
    //
    //           f(CPU)     100,000
    //     C * ---------- * -------
    //         50,000,000    f(I2C)
    //
    // This equation will give the inter-byte delay required for any
    // configuration of the I2C master.  But, as arranged it is impossible to
    // directly compute in 32-bit arithmetic (without loosing a lot of
    // accuracy).  So, the equation is simplified.
    //
    // Since f(I2C) is generated by dividing down from f(CPU), replace it with
    // the equivalent (where TPR is the value programmed into the Master Timer
    // Period Register of the I2C master, with the 1 added back):
    //
    //                        100,000
    //           f(CPU)       -------
    //     C * ---------- *    f(CPU)
    //         50,000,000   ------------
    //                      2 * 10 * TPR
    //
    // Inverting the dividend in the last term:
    //
    //           f(CPU)     100,000 * 2 * 10 * TPR
    //     C * ---------- * ----------------------
    //         50,000,000          f(CPU)
    //
    // The f(CPU) now cancels out.
    //
    //         100,000 * 2 * 10 * TPR
    //     C * ----------------------
    //               50,000,000
    //
    // Since there are no clock frequencies left in the equation, this equation
    // also works for 400 kHz bus operation as well, since the 100,000 in the
    // numerator becomes 400,000 but C is 1/4, which cancel out each other.
    // Reducing the constants gives:
    //
    //         TPR              TPR             TPR
    //     C * ---   =   1700 * ---   =   340 * ---   = 68 * TPR
    //         25               25               5
    //
    // Note that the constant C is actually a bit larger than it needs to be in
    // order to provide some safety margin.
    //
    g_ulDelay = 68 * (I2C0_MASTER->MTPR + 1);

    //
    // Initialize the display controller.  Loop through the initialization
    // sequence doing a single I2C transfer for each command.
    //
    for(ulTmp = 0; ulTmp < SIZE_INIT_CMDS; ulTmp += g_pucDisplayInit[ulTmp] + 1)
    {
        //
        // Send this command.
        //
        Display96x16x1WriteFirst(g_pucDisplayInit[ulTmp + 1]);
        Display96x16x1WriteArray(g_pucDisplayInit + ulTmp + 2,
                                 g_pucDisplayInit[ulTmp] - 2);
        Display96x16x1WriteFinal(g_pucDisplayInit[ulTmp
                                 + g_pucDisplayInit[ulTmp]]);
    }

    //
    // Clear the frame buffer.
    //
    Display96x16x1Clear();
}
//*****************************************************************************
//
//! Initialize the OLED display.
//!
//! \param bFast is a boolean that is \e true if the I2C interface should be
//! run at 400 kbps and \e false if it should be run at 100 kbps.
//!
//! This function initializes the I2C interface to the OLED display and
//! configures the SSD0303 or SSD1300 controller on the panel.
//!
//! \return None.
//
//*****************************************************************************
void
Display96x16x1Init(tBoolean bFast)
{
    unsigned long ulIdx;

    //
    // The power supply for the OLED display comes from the motor power
    // supply, which must be turned on.  If the application is using the
    // motor then this is taken care of when the motor driver is initialized.
    // But if the motor driver is not used, then the motor power supply needs
    // to be turned on here so the OLED works properly.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
    GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, GPIO_PIN_5);
    GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_5, GPIO_PIN_5);

    //
    // Enable the I2C and GPIO peripherals needed for the display.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C1);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOG);

    //
    // Deassert the display controller reset signal (active low)
    //
    GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_0);
    GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0, GPIO_PIN_0);

    //
    // Wait a short delay, then drive the pin low to reset the controller
    //
    SysCtlDelay(32);
    GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0, 0);

    //
    // Leave it is reset for a short delay, then drive it high to deassert
    // reset.  Then the controller should be out of reset.
    //
    SysCtlDelay(32);
    GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0, GPIO_PIN_0);

    //
    // Configure the GPIO pins needed for the display as I2C
    //
    GPIOPinConfigure(GPIO_PG0_I2C1SCL);
    GPIOPinConfigure(GPIO_PG1_I2C1SDA);
    GPIOPinTypeI2C(GPIO_PORTG_BASE, GPIO_PIN_0 | GPIO_PIN_1);

    //
    // Reset the I2C1 peripheral.
    //
    SysCtlPeripheralReset(SYSCTL_PERIPH_I2C1);

    //
    // Initialize the I2C master.
    //
    I2CMasterInitExpClk(I2C1_MASTER_BASE, SysCtlClockGet(), bFast);

    //
    // Initialize the display controller.  Loop through the initialization
    // sequence doing a single I2C transfer for each command.
    //
    for(ulIdx = 0; ulIdx < sizeof(g_pucRITInit);
        ulIdx += g_pucRITInit[ulIdx] + 1)
    {
        //
        // Send this command.
        //
        Display96x16x1WriteFirst(g_pucRITInit[ulIdx + 1]);
        Display96x16x1WriteArray(g_pucRITInit + ulIdx + 2,
                                 g_pucRITInit[ulIdx] - 2);
        Display96x16x1WriteFinal(g_pucRITInit[ulIdx + g_pucRITInit[ulIdx]]);
    }

    //
    // Clear the frame buffer.
    //
    Display96x16x1Clear();

    //
    // Turn the display on.
    //
    Display96x16x1DisplayOn();
}