// ----------------------------------------------------------------------------------------
//
//  avr/lcd/cfax/cfax.h
//    
//  Bob Cook Development, Robotics Library
//  http://www.bobcookdev.com/rl/
//    
//  This file implements support for the Crystal Fontz CFAX 128x64.
//
//  Copyright (C) 2007 Bob Cook
//
//      This program is free software; you can redistribute it and/or modify it
//      under the terms of the GNU General Public License as published by the
//      Free Software Foundation; either version 2 of the License, or (at your
//      option) any later version.
//    
//      This program is distributed in the hope that it will be useful, but 
//      WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
//      or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License 
//      for more details.
//    
//      You should have received a copy of the GNU General Public License along
//      with this program; if not, write to the Free Software Foundation, Inc., 
//      51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
//
// ----------------------------------------------------------------------------------------


#if !defined( BCDRL_AVR_CFAX_H )
#define BCDRL_AVR_CFAX_H


#include <stdint.h>
#include <avr/pgmspace.h>


// ----------------------------------------------------------------------------------------
//  definitions from "project_defs.h"
//
//  enable support for the CFAX 128x64 Graphic LCD with:
//
//      + PRJ_CFAX_ENABLE
//
//  specify the data port (all eight pins) for communications with:
//
//      + PRJ_CFAX_DATA_PORT
//      + PRJ_CFAX_DATA_PORT_PINS
//      + PRJ_CFAX_DATA_DDR
//
//  specify the control pins with:
//
//      + PRJ_CFAX_CONTROL_RSTB_PORT
//      + PRJ_CFAX_CONTROL_RSTB_PIN
//      + PRJ_CFAX_CONTROL_RSTB_DDR
//
//      + PRJ_CFAX_CONTROL_RS_PORT
//      + PRJ_CFAX_CONTROL_RS_PIN
//      + PRJ_CFAX_CONTROL_RS_DDR
//
//      + PRJ_CFAX_CONTROL_RD_PORT
//      + PRJ_CFAX_CONTROL_RD_PIN
//      + PRJ_CFAX_CONTROL_RD_DDR
//
//      + PRJ_CFAX_CONTROL_RW_PORT
//      + PRJ_CFAX_CONTROL_RW_PIN
//      + PRJ_CFAX_CONTROL_RW_DDR
//
//  specify the (optional) touch screen with:
//
//      + PRJ_CFAX_INC_TOUCHSCREEN
//
//      + PRJ_CFAX_TOUCHSCREEN_PORT
//      + PRJ_CFAX_TOUCHSCREEN_DDR
//      + PRJ_CFAX_TOUCHSCREEN_PIN_1
//      + PRJ_CFAX_TOUCHSCREEN_PIN_2
//      + PRJ_CFAX_TOUCHSCREEN_PIN_3
//      + PRJ_CFAX_TOUCHSCREEN_PIN_4
//      + PRJ_CFAX_TOUCHSCREEN_ADC_CH_1
//      + PRJ_CFAX_TOUCHSCREEN_ADC_CH_2
//      + PRJ_CFAX_TOUCHSCREEN_ADC_CH_3
//      + PRJ_CFAX_TOUCHSCREEN_ADC_CH_4
//
//  optional features can be enabled with one or more of the following:
//
//      + PRJ_CFAX_DEFAULT_CONTRAST <int>
//
//      + PRJ_CFAX_INC_ICONS
//      + PRJ_CFAX_INC_WRITE_CHAR
//      + PRJ_CFAX_INC_WRITE_CSTRINGS
//      + PRJ_CFAX_INC_WRITE_PGM_CSTRINGS
//      + PRJ_CFAX_INC_SET_PIXEL
//      + PRJ_CFAX_INC_DRAWING
//      + PRJ_CFAX_INC_BITMAPS
//
// ----------------------------------------------------------------------------------------


// ----------------------------------------------------------------------------------------
//  typedefs and constants
//

const uint8_t CFAX_NUM_ICONS = 17;

typedef enum
{
    cfax_icon_maple_leaf,
    cfax_icon_document,
    cfax_icon_battery_outline,
    cfax_icon_battery_level_1,
    cfax_icon_battery_level_2,
    cfax_icon_battery_level_3,
    cfax_icon_terminal,
    cfax_icon_antenna,
    cfax_icon_speaker,
    cfax_icon_speaker_tick_1,
    cfax_icon_speaker_tick_2,
    cfax_icon_telephone,
    cfax_icon_letter,
    cfax_icon_bell,
    cfax_icon_clock,
    cfax_icon_caution,
    cfax_icon_spinner

}   cfax_icon_t;


// ----------------------------------------------------------------------------------------
//  cfax_init
//
//      Initialize the CFAX driver package, setting up the hardware and clearing the
//      display. Call this function prior to any other.
//

void cfax_init();


// ----------------------------------------------------------------------------------------
//  cfax_clear_screen
//
//      Erase the entire screen. If icon support is enabled this does *not* clear them
//      automatically. If text lcd emulation support is enabled then the cursor position
//      is reset to the upper left corner (0,0).
//

void cfax_clear_screen();


// ----------------------------------------------------------------------------------------
//  cfax_clear_icons
//
//      Turn off all icons.
//
//      This function is only available if PRJ_CFAX_INC_ICONS is defined.
//

void cfax_clear_icons();


// ----------------------------------------------------------------------------------------
//  cfax_set_display
//
//      Turn on or off the display. This does not clear the screen, just disables the
//      active display.
//

void cfax_set_display( bool state );


// ----------------------------------------------------------------------------------------
//  cfax_set_contrast
//
//      Set the contrast of the display.
//

void cfax_set_contrast( uint8_t contrast );


// ----------------------------------------------------------------------------------------
//  cfax_set_icon
//
//      Turn on or off the specified icon.
//
//      This function is only available if PRJ_CFAX_INC_ICONS is defined.
//

void cfax_set_icon( cfax_icon_t icon_id, bool state );


// ----------------------------------------------------------------------------------------
//  cfax_write
//
//      Write a character to the display in the *character* row and column. The display
//      can support 8 rows by 20 columns.
//
//      This function is only available if PRJ_CFAX_INC_WRITE_CHAR is defined.
//

void cfax_write( char c, uint8_t x, uint8_t y );


// ----------------------------------------------------------------------------------------
//  cfax_write
//
//      Write a string to the display in the *character* row and column. The display
//      can support 8 rows by 20 columns.
//
//      This function is only available if PRJ_CFAX_INC_WRITE_CSTRINGS is defined.
//

void cfax_write( const char* str, uint8_t x, uint8_t y );


// ----------------------------------------------------------------------------------------
//  cfax_write_pgm
//
//      Write a string from flash memory to the display in the *character* row and column.
//      The display can support 8 rows by 20 columns.
//
//      This function is only available if PRJ_CFAX_INC_WRITE_PGM_CSTRINGS is defined.
//

void cfax_write_pgm( const char* str, uint8_t x, uint8_t y );


// ----------------------------------------------------------------------------------------
//  cfax_set_pixel
//
//      Turn on or off a specific pixel. The display supports 64 rows by 128 columns.
//
//      This function is only available if PRJ_CFAX_INC_SET_PIXEL is defined.
//

void cfax_set_pixel( uint8_t x, uint8_t y, bool state );


// ----------------------------------------------------------------------------------------
//  cfax_line_h
//
//      Turn on or off a row of pixels.
//
//      This function is only available if PRJ_CFAX_INC_DRAWING is defined.
//

void cfax_line_h( uint8_t x, uint8_t y, uint8_t len, bool state );


// ----------------------------------------------------------------------------------------
//  cfax_line_v
//
//      Turn on or off a column of pixels.
//
//      This function is only available if PRJ_CFAX_INC_DRAWING is defined.
//

void cfax_line_v( uint8_t x, uint8_t y, uint8_t len, bool state );


// ----------------------------------------------------------------------------------------
//  cfax_rect
//
//      Turn on the frame (edge) pixels of a rectangular region.
//
//      This function is only available if PRJ_CFAX_INC_DRAWING is defined.
//

void cfax_rect( uint8_t x, uint8_t y, uint8_t w, uint8_t h );


// ----------------------------------------------------------------------------------------
//  cfax_fill
//
//      Turn on all the pixels inside of a rectangular region.
//
//      This function is only available if PRJ_CFAX_INC_DRAWING is defined.
//

void cfax_fill( uint8_t x, uint8_t y, uint8_t w, uint8_t h );


// ----------------------------------------------------------------------------------------
//  cfax_erase
//
//      Turn off all the pixels inside of a rectangular region.
//
//      This function is only available if PRJ_CFAX_INC_DRAWING is defined.
//

void cfax_erase( uint8_t x, uint8_t y, uint8_t w, uint8_t h );


// ----------------------------------------------------------------------------------------
//  cfax_line
//
//      Turn on or off the pixels in a line between two points. This is much more
//      expensive for horizontal or vertical lines; prefer to use the optimized
//      functions (cfax_line_h, cfax_line_v) for those cases when possible.
//
//      This function is only available if PRJ_CFAX_INC_DRAWING is defined.
//

void cfax_line( uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1, bool state );


// ----------------------------------------------------------------------------------------
//  cfax_bitmap
//
//      Paint an image at the specified pixel location. The pixel data must be stored in
//      flash memory and be organized by rows, padded to whole bytes per row.
//
//      This function is only available if PRJ_CFAX_INC_BITMAPS is defined.
//

void cfax_bitmap
    (
        uint8_t x,
        uint8_t y,
        uint8_t w,
        uint8_t h,
        const prog_uint8_t* pixels
    );


// ----------------------------------------------------------------------------------------
//  cfax_touchscreen
//
//      Read the touch screen. Returns false if the screen is not pressed. The resulting
//      (x, y) pair is clamped to (0-64, 0-32).
//
//      This function is only available if PRJ_CFAX_INC_TOUCHSCREEN is defined.
//

bool cfax_touchscreen( uint8_t* x, uint8_t* y );


#endif  // #if !defined( BCDRL_AVR_CFAX_H )
// ----------------------------------------------------------------------------------------
//  end of cfax.h
// ----------------------------------------------------------------------------------------

