/*******************************************************************************
MPLAB Harmony Graphics Composer Generated Implementation File

File Name:
libaria_events.c

Summary:
Build-time generated implementation from the MPLAB Harmony
Graphics Composer.

Description:
Build-time generated implementation from the MPLAB Harmony
Graphics Composer.

Created with MPLAB Harmony Version 2.04
*******************************************************************************/
// DOM-IGNORE-BEGIN
/*******************************************************************************
Copyright (c) 2013-2014 released Microchip Technology Inc. All rights reserved.

Microchip licenses to you the right to use, modify, copy and distribute
Software only when embedded on a Microchip microcontroller or digital signal
controller that is integrated into your product or third party product
(pursuant to the sublicense terms in the accompanying license agreement).

You should refer to the license agreement accompanying this Software for
additional information regarding your rights and obligations.

SOFTWARE AND DOCUMENTATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF
MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
IN NO EVENT SHALL MICROCHIP OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER
CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR
OTHER LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE OR
CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT OF
SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
(INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
*******************************************************************************/
// DOM-IGNORE-END

#include "gfx/libaria/libaria_events.h"

#include <stdio.h>

bool LED; //LED点滅モード
SYS_TMR_HANDLE handleTimer1; //SYS_TMR_CallbackSingle()関数のハンドル
bool ChangeNG; //LED切替わり不可の時、1となるフラグ
char Buf[32]; //液晶表示用バッファー



void MyLibariaEvents(void) //APP_Tasks ()からの呼び出し
{
short int tempX,tempY;

//追加部分
//----------------------------------------------------
int myColor1 = 0xf800; //赤色
int myColor2 = 0x001f; //青色
int myColor3 = 0x0000; //黒色
int myColor4 = 0xffff; //白色
int myColor5 = 0xffe0; //黄色

int x0 = 20,
y0 = 20;


//ピクセル描画
//赤色ピクセルを座標(20,20)に描画
ys_pixelSet(x0, y0, myColor1);

//赤色ピクセルを座標(40,40),(40 +1,40),(40,40 +1),(40 + 1,40 +1)に描画 //合計4個
ys_pixelSet(x0 *2, y0 *2, myColor1);
ys_pixelSet(x0 *2 -1, y0 *2, myColor1);
ys_pixelSet(x0 *2, y0 *2 -1, myColor1);
ys_pixelSet(x0 *2 -1, y0 *2 -1, myColor1);

//赤色ピクセルを 9個を座標(30,40) 及び同座標周辺に作成 //合計9個
ys_pixelSet(x0 *3, y0*3, myColor1);
ys_pixelSet(x0 *3, y0*3 - 1, myColor1);
ys_pixelSet(x0 *3, y0*3 + 1, myColor1);
ys_pixelSet(x0 *3 - 1, y0*3 - 1, myColor1);
ys_pixelSet(x0 *3 - 1, y0*3 , myColor1);
ys_pixelSet(x0 *3 - 1, y0*3 + 1, myColor1);
ys_pixelSet(x0 *3 + 1, y0*3 - 1, myColor1);
ys_pixelSet(x0 *3 + 1, y0*3 , myColor1);
ys_pixelSet(x0 *3 + 1, y0*3 + 1, myColor1);

//直線を描画 に作成 //但し座標原点は画面左下
ys_drawLine(0, 0, 200, 210, myColor2); //座標(0,0) と(200,210)の間の直線 //色は青色

ys_drawLine(0, 0, 200, 140, myColor2); //座標(0,0) と(200,140)の間の直線 //色は青色

ys_drawLine(0, 0, 200, 70, myColor2); //座標(0,0) と(200,70)の間の直線 //色は青色


//円を描画 //但し座標原点は画面左上 
ys_drawCircle(50, 120, 25, myColor3); //中心座標(50,120) 半径:25 //色は黒色

ys_drawCircle(100, 120, 50, myColor3); //中心座標(100,120) 半径:50 //色は黒色

ys_drawCircle(150, 120, 75, myColor3); //中心座標(150,120) 半径:75 //色は黒色


//長方形を描画
// ys_fillRect(int left, int top, int right, int bottom, int color);
ys_fillRect(240, 30, 310, 30 +40, myColor1); //左上座標(240, 30) 左下座標(310, 30 +40)の長方形 塗りつぶし色は赤色

ys_fillRect(240, 100, 310, 100 +40, myColor4); //左上座標(240, 100) 左下座標(310, 100 +40)の長方形 塗りつぶし色は白色

ys_fillRect(240, 170, 310, 170 +40, myColor5);//左上座標(240, 170) 左下座標(310, 170 +40)の長方形 塗りつぶし色は黄色

//--------------------------------------------------------
//以上、追加部分



tempX = ys_TouchGetX();
tempY = ys_TouchGetY();

lcd_cmd(0x80); //1行目の先頭へ
sprintf(Buf,"X = %d ",tempX);//タッチX座標表示
lcd_str(Buf); //液晶表示
lcd_cmd(0xC0); //2行目の先頭へ
sprintf(Buf,"Y = %d ",tempY); //タッチY座標表示
lcd_str(Buf); // 開始メッセージ1行目表示
}




void LedFunc(void) //LEDオンオフ制御
{
if(LED == false)
{
LED = true;

PLIB_PORTS_PinSet( PORTS_ID_0, PORT_CHANNEL_G, 15 ); //RG15: LED1点灯
//LATGbits.LATG15 = 1;
PLIB_PORTS_PinSet( PORTS_ID_0, PORT_CHANNEL_D, 4 ); //RD4: LED2点灯
//LATDbits.LATD4 = 1;
PLIB_PORTS_PinSet( PORTS_ID_0, PORT_CHANNEL_A, 15 ); //RA15: LED3点灯
//LATAbits.LATA15 = 1;
PLIB_PORTS_PinSet( PORTS_ID_0, PORT_CHANNEL_A, 14 ); //RA14: LED4点灯
//LATAbits.LATA14 = 1;
}
else
{
LED = false;

PLIB_PORTS_PinClear( PORTS_ID_0, PORT_CHANNEL_G, 15 );//RG15: LED1消灯
// LATGbits.LATG15 = 0;
PLIB_PORTS_PinClear( PORTS_ID_0, PORT_CHANNEL_D, 4 );//RD4: LED2消灯
// LATDbits.LATD4 = 0;
PLIB_PORTS_PinClear( PORTS_ID_0, PORT_CHANNEL_A, 15 );//RA15: LED3消灯
// LATAbits.LATA15 = 0;
PLIB_PORTS_PinClear( PORTS_ID_0, PORT_CHANNEL_A, 14 );//RA14: LED4消灯
// LATAbits.LATA14 = 0;
}

}



//500msec後のコールバック関数
void Timer_Callback1 ( uintptr_t context, uint32_t currTick )
{
ChangeNG = false;
}



// LedButton - ReleasedEvent
void LedButton_ReleasedEvent(laButtonWidget* btn)
{
// Led_Control
//
//チャタリング対策
if(ChangeNG == false) //切り替わり可の場合
{
ChangeNG = true;
LedFunc(); //LEDオンオフ制御
handleTimer1 = SYS_TMR_CallbackSingle(500, 1, Timer_Callback1);//500msecの非繰り返しコールバックイベントセット
}

}