]> sigrok.org Git - sigrok-firmware-fx2lafw.git/blob - fx2lib/examples/i2c/i2c.c
Import fx2lib into fx2lafw directly.
[sigrok-firmware-fx2lafw.git] / fx2lib / examples / i2c / i2c.c
1 /**
2  * Copyright (C) 2009 Ubixum, Inc. 
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17  **/
18
19
20
21 #include <fx2regs.h>
22
23 #include <i2c.h>
24
25 #define LED_ADDR 0x21
26 #define BTN_ADDR 0x20
27
28 #define KEY_F1 0
29 #define KEY_F2 1
30 #define KEY_F3 2
31 #define KEY_F4 3
32
33
34 BYTE digits[] = { 0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x98, 0x88, 0x83, 0xc6, 0xa1, 0x86, 0x8e  };
35
36
37 /**
38  * This is pretty much the same function that is provided as the dev_io example from Cypress
39  * Except is uses i2c read and write functions not from their library
40  **/
41 void main(void) {
42
43     BYTE    num = 0;
44     //BYTE xdata        buttons;
45     BYTE    buttons;
46         BYTE    kstates = 0xff;
47         BYTE    kdeltas;
48         BYTE    key;
49         BYTE    display = 1;
50
51         while(1)
52         {
53                 i2c_read ( BTN_ADDR, 1, &buttons );
54
55                 kdeltas = kstates ^ buttons;                    // 
56                 kstates = buttons;
57                 key = 0;
58
59                 while(kdeltas)
60                 {
61                         if(kdeltas & 0x01)
62                         {
63                                 if(!((kstates >> key) & 0x01))
64                                         switch(key)
65                                         {
66                         case KEY_F1: // wakeup?
67                                                         num = 0;
68                                                         break;
69                                                 case KEY_F2:
70                                                         if(--num > 0x0f)
71                                                                 num = 0x0f;
72                                                         break;
73                                                 case KEY_F3:
74                                                         if(++num > 0x0f)
75                                                                 num = 0;
76                                                         break;                                          
77                                                 case KEY_F4:
78                                                         num = 0x0f;
79                                         }
80                                 display = 1;
81                         }
82                         kdeltas = kdeltas >> 1;
83                         ++key;
84                 }
85                 if(display)
86                 {
87             i2c_write ( LED_ADDR, 1, &digits[num], 0, NULL );
88                         display = 0;
89                 }
90     }
91
92 }