view main/demos/avr/adafruit-lcd-1480/main.cpp @ 220:74381e5dd204 main

Demo for the Adafruit LCD product number 1480.
author Bob Cook <bob@bobcookdev.com>
date Mon, 07 Jul 2014 18:42:15 -0700
parents
children
line wrap: on
line source

// ----------------------------------------------------------------------------------------
//
//  Copyright (C) 2014 Bob Cook
//    
//  Bob Cook Development, Robotics Library
//  http://www.bobcookdev.com/rl/
// 
//  Demonstration of the Adafruit 2.2" TFT LCD display (product #1480)
//
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
//  The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
//
// ----------------------------------------------------------------------------------------

#include <avr/interrupt.h>
#include <avr/io.h>
#include <avr/sleep.h>

#include "project_defs.h"

#include "packages/avr/device/int_helpers.h"
#include "packages/avr/device/spinwait.h"

#include "packages/avr/lcd/ili9340/ili9340.h"

#include "packages/common/util/printutils.h"

// always after other includes
#include "packages/avr/device/workaround34734.h"

// ----------------------------------------------------------------------------------------

inline void led_red_on()
{
    PORTB &= ~( 1 << PB5 );
}

inline void led_red_off()
{
    PORTB |= ( 1 << PB5 );
}

inline void led_green_on()
{
    PORTC &= ~( 1 << PC0 );
    PORTC &= ~( 1 << PC7 );
}

inline void led_green_off()
{
    PORTC |= ( 1 << PC0 );
    PORTC |= ( 1 << PC7 );
}

inline void leds_off()
{
    PORTB |= ( 1 << PB5 );
    PORTC |= ( 1 << PC0 );
    PORTC |= ( 1 << PC7 );
}

// ----------------------------------------------------------------------------------------

void hw_init()
{
    //--    Turn off all interrupts.

    cli();
    interrupts_clear_all();

    //--    Indicator LEDs on PORTB.5, PORTC.0

    DDRB |= ( 1 << PB5 );
    DDRC |= ( 1 << PC0 );
    DDRC |= ( 1 << PC7 );
    leds_off();

    //--    Set the SS pin as an output; this prevents the SPI machinery from blocking.

    DDRD |= ( 1 << PD3 );

    //--    Blink the LEDs just to say hello.
    
    for ( uint8_t i = 0; i < 5; i++ )
    {
        led_red_on();
        led_green_on();
        spinwait_delay_ms( 75 );
        leds_off();
        spinwait_delay_ms( 75 );
    }

    //--    Re-enable interrupts.

    sei();

    //--    Initialize the display package.

    ili9340_init();

    PORTC |= ( 1 << PC1 ); // backlight
}

// ----------------------------------------------------------------------------------------

int main()
{
    //--    Initialize the hardware and send a "hello we started" message.

    hw_init();

    led_green_on();
       
    //--    Draw some stuff.

    ili9340_fill_screen( ili9340_color_blue );

    ili9340_write( 'A', 1, 1, ili9340_color_white, ili9340_color_blue );
    ili9340_write( 'B', 2, 1, ili9340_color_white, ili9340_color_blue );

    ili9340_write_pgm( PSTR("Adafruit 2.2\" TFT LCD"),
                       3, 5,
                       ili9340_color_white, ili9340_color_blue );

    for ( uint8_t i = 0; i < 200; ++i )
    {
        char number_str[ 16 ];
        char* eos = print_bits( number_str, i );
        *eos = '\0';

        ili9340_write( number_str,
                       10, 6,
                       ili9340_color_white, ili9340_color_blue );

        //spinwait_delay_ms( 250 );
    }

    for ( ;; )
    {
        led_red_on();
        spinwait_delay_ms( 1000 );
        led_red_off();
        spinwait_delay_ms( 1000 );
    }

    return 0;
}

// ----------------------------------------------------------------------------------------