OSDN Git Service

[SCHEMATIC] Modify SW/MW/LW Preamp, insert galbanic-isolator replace of common-mode...
[openi2cradio/OpenI2CRadio.git] / backlight.c
1 /*
2  * OpenI2CRADIO
3  * Backlight Handler
4  * Copyright (C) 2013-08-31 K.Ohta <whatisthis.sowhat ai gmail.com>
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2,
9  *  or (at your option) any later version.
10  *  This library / program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  *  See the GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this library; see the file COPYING. If not, write to the
17  *  Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
18  *  MA 02110-1301, USA.
19  *
20  *  As a special exception, if you link this(includeed from sdcc) library
21  *  with other files, some of which are compiled with SDCC,
22  *  to produce an executable, this library does not by itself cause
23  *  the resulting executable to be covered by the GNU General Public License.
24  *  This exception does not however invalidate any other reasons why
25  *  the executable file might be covered by the GNU General Public License.
26  */
27
28 #include "backlight.h"
29 #include "idle.h"
30 #include "menu.h"
31 #include "power.h"
32
33 int backlight_counter;
34 unsigned char l_backlight_level;
35
36 void lcd_setbacklight(unsigned char flag, unsigned char level)
37 {
38     __bitops_t  b;
39     __bitops_t d;
40    l_backlight_level = level;
41    if(flag == 0) {
42         d.byte = _LCD_BACKLIGHT_TRIS;
43         d._LCD_BACKLIGHT_BIT = 1;
44         _LCD_BACKLIGHT_TRIS = d.byte;
45         PSTRCONbits.STRA = 0;
46         T2CONbits.TMR2ON = 0;
47         TMR2 = 0x00;
48         CCP2CON = 0x00;
49         CCPR2L = 0;
50         CCPR2H = 0;
51     } else {
52       unsigned char h,l;
53       h = level >> 1;
54       if(h == CCPR1L) return;
55  //     l = 2 << 4; //(lv & 0x0300) >> 4;
56       d.byte = _LCD_BACKLIGHT_TRIS;
57       d._LCD_BACKLIGHT_BIT = 1;
58       _LCD_BACKLIGHT_TRIS = d.byte;
59       //PSTRCON = 0;
60       CCPR2L = h;
61       CCPR2H = h;
62       b.b2 = 1;
63       b.b3 = 1;
64       CCP2CON = b.byte;
65       PR2 = 50;
66       PIR1bits.TMR2IF = 0;
67       T2CON = 0b01111000; // Pre-scaler=1/1,Post-scaler = 1/16
68       // -> Freq = 2.44KHz * 16 = 39.4KHz.
69       TMR2 = 0x00;
70       T2CONbits.TMR2ON = 1;
71       do {
72           idle_time_ms(2); // 
73       } while(PIR1bits.TMR2IF == 0);
74       d.byte = _LCD_BACKLIGHT_TRIS;
75       d._LCD_BACKLIGHT_BIT = 0;
76       _LCD_BACKLIGHT_TRIS = d.byte;
77
78 //      _LCD_PORT |= _LCD_BACKLIGHT;
79     }
80 }
81
82 void backlight_reset(unsigned char ctlword)
83 {
84     if(ctlword == charcode_e){
85         if(backlight_counter != 0){
86            backlight_counter = 0;
87            lcd_setbacklight(0x00, 0); // Turn OFF
88         } else {
89            backlight_counter = setup.backlight_long;
90            lcd_setbacklight(0xff, setup.backlight_level); // Turn ON
91         }
92         return;
93     }
94    if((ctlword >= charcode_1) && (ctlword <= charcode_f)) {
95             backlight_counter = setup.backlight_long;
96             lcd_setbacklight(0xff, setup.backlight_level); // Turn ON
97     }
98 }
99
100
101 unsigned char backlight_dec(unsigned char dispf)
102 {
103     unsigned char pbutton;
104     unsigned char stat = dispf;
105
106     pbutton = chk_powerbutton(); // 48ms
107     if(pbutton != 0) shutdown(1); // Button pressed.
108     ClrWdt();
109
110        if(backlight_counter > 0) {
111           if(dispf == 0) {
112               acm1602_resume(LCD_I2CADDR);
113                stat = 0xff;
114                lcd_setbacklight(0xff, setup.backlight_level);
115           }
116
117            backlight_counter--;
118            //lcd_setbacklight(0xff, setup.backlight_level); // Turn ON
119        } else {
120            lcd_setbacklight(0x00, 0); // Turn OFF
121            acm1602_suspend(LCD_I2CADDR);
122            stat = 0;
123         }
124     return stat;
125 }