OSDN Git Service

[Schematic][v2.0] Add common-mode transformer to input-line of SW/MW/LF AMP.
[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
35
36 void lcd_setbacklight(unsigned char flag, unsigned char level)
37 {
38     __bitops_t  b;
39     __bitops_t d;
40 #if 0
41     d.byte = _LCD_BACKLIGHT_TRIS;
42     d._LCD_BACKLIGHT_BIT = 0;
43    _LCD_BACKLIGHT_TRIS = d.byte;
44
45     b.byte = _LCD_BACKLIGHT;
46     b._LCD_BACKLIGHT_BIT = 0;
47     if(flag != 0){
48        b._LCD_BACKLIGHT_BIT = 1;
49     }
50     _LCD_BACKLIGHT = b.byte;
51 #else
52     if(flag == 0) {
53         d.byte = _LCD_BACKLIGHT_TRIS;
54         d._LCD_BACKLIGHT_BIT = 1;
55         _LCD_BACKLIGHT_TRIS = d.byte;
56         PSTRCONbits.STRA = 0;
57         T2CONbits.TMR2ON = 0;
58         TMR2 = 0x00;
59         CCP2CON = 0x00;
60         CCPR2L = 0;
61         CCPR2H = 0;
62     } else {
63       unsigned char h,l;
64 //      lv = level;
65 //      lv <<= 2;
66 //      level = (level * 25) / 10;
67 //      level = 255;
68       h = level >> 1;
69       if(h == CCPR1L) return;
70  //     l = 2 << 4; //(lv & 0x0300) >> 4;
71       d.byte = _LCD_BACKLIGHT_TRIS;
72       d._LCD_BACKLIGHT_BIT = 1;
73       _LCD_BACKLIGHT_TRIS = d.byte;
74       //PSTRCON = 0;
75       CCPR2L = h;
76       CCPR2H = h;
77       b.b2 = 1;
78       b.b3 = 1;
79       CCP2CON = b.byte;
80       PR2 = 50;
81       PIR1bits.TMR2IF = 0;
82       T2CON = 0b01111000; // Pre-scaler=1/1,Post-scaler = 1/16
83       // -> Freq = 2.44KHz * 16 = 39.4KHz.
84       TMR2 = 0x00;
85       T2CONbits.TMR2ON = 1;
86       do {
87           idle_time_ms(1);
88       } while(PIR1bits.TMR2IF ==0);
89       d.byte = _LCD_BACKLIGHT_TRIS;
90       d._LCD_BACKLIGHT_BIT = 0;
91       _LCD_BACKLIGHT_TRIS = d.byte;
92
93 //      _LCD_PORT |= _LCD_BACKLIGHT;
94     }
95 #endif
96 }
97
98 void backlight_reset(unsigned char ctlword)
99 {
100     if(ctlword == charcode_e){
101         if(backlight_counter != 0){
102            backlight_counter = 0;
103         } else {
104            backlight_counter = setup.backlight_long;
105         }
106         return;
107     }
108    if((ctlword >= charcode_1) && (ctlword <= charcode_f)) {
109             backlight_counter = setup.backlight_long;
110             lcd_setbacklight(0xff, setup.backlight_level); // Turn ON
111     }
112 }
113
114
115 unsigned char backlight_dec(unsigned char dispf)
116 {
117     unsigned char pbutton;
118     unsigned char stat = dispf;
119
120     pbutton = chk_powerbutton(); // 48ms
121     if(pbutton != 0) shutdown(1); // Button pressed.
122     ClrWdt();
123
124        if(backlight_counter > 0) {
125           if(dispf == 0) {
126               acm1602_resume(LCD_I2CADDR);
127                stat = 0xff;
128            }
129
130            backlight_counter--;
131            lcd_setbacklight(0xff, setup.backlight_level); // Turn ON
132        } else {
133            lcd_setbacklight(0x00, 0); // Turn OFF
134            acm1602_suspend(LCD_I2CADDR);
135            stat = 0;
136         }
137     return stat;
138 }