New I2C devices
[owSlave2.git] / DS2450_APDS9960 / DS2450_APDS9960.c
1
2 // Copyright (c) 2017, Tobias Mueller tm(at)tm3d.de
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 //  * Redistributions of source code must retain the above copyright
10 //    notice, this list of conditions and the following disclaimer.
11 //  * Redistributions in binary form must reproduce the above copyright
12 //    notice, this list of conditions and the following disclaimer in the
13 //    documentation and/or other materials provided with the
14 //    distribution.
15 //  * All advertising materials mentioning features or use of this
16 //    software must display the following acknowledgement: This product
17 //    includes software developed by tm3d.de and its contributors.
18 //  * Neither the name of tm3d.de nor the names of its contributors may
19 //    be used to endorse or promote products derived from this software
20 //    without specific prior written permission.
21 //
22 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
34 #define F_CPU 8000000UL
35 #include <avr/io.h>
36 #include <avr/interrupt.h>
37 #include <util/delay.h>
38 #include <avr/wdt.h>
39 #include <avr/sleep.h>
40 #include <avr/pgmspace.h>
41 #include "../common/I2C/USI_TWI_Master.h"
42 #include "../common/I2C/APDS9960.h"
43
44 extern void OWINIT();
45 extern void EXTERN_SLEEP();
46
47 uint8_t owid[8]={0x20, 0xA2, 0xD9, 0x84, 0x00, 0x16, 0x23, 0x20};/**/
48 //uint8_t config_info[26]={0x03,13,0x03,13,0x03,13,0x3,15,0x02,20,20,20,20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
49 uint8_t config_info[26]={16,15,17,15,18,15,3,15,0x02,23,23,23,23,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
50
51 #if (owid>128) 
52 #error "Variable not correct"
53 #endif
54
55 extern uint8_t mode;
56 extern uint8_t gcontrol;
57 extern uint8_t reset_indicator;
58 extern uint8_t alarmflag;
59
60
61 typedef union {
62         volatile uint8_t bytes[0x20];
63         struct {
64                 //Page0
65                 uint16_t A;  //0
66                 uint16_t B;  //2
67                 uint16_t C;  //4
68                 uint16_t D;  //6
69                 //Page1
70                 uint8_t CSA1;
71                 uint8_t CSA2;
72                 uint8_t CSB1;
73                 uint8_t CSB2;
74                 uint8_t CSC1;
75                 uint8_t CSC2;
76                 uint8_t CSD1;
77                 uint8_t CSD2;
78                 //Page2
79                 uint8_t LA;
80                 uint8_t HA;
81                 uint8_t LB;
82                 uint8_t HB;
83                 uint8_t LC;
84                 uint8_t HC;
85                 uint8_t LD;
86                 uint8_t HD;
87                 //Page3
88                 uint8_t FC1;
89                 uint8_t FC2;
90                 uint8_t FC3;
91                 uint8_t FC4;
92                 uint8_t VCCP;
93                 uint8_t FC5;
94                 uint8_t FC6;
95                 uint8_t FC7;
96                 uint8_t convc1;
97                 uint8_t convc2;
98                 
99                 
100         };
101 } pack_t;
102 volatile pack_t pack;
103
104
105 uint16_t light[4];
106 double rlight[4];
107 uint8_t gain=2;
108 uint8_t atime=192;
109 //uint8_t atime=255;
110 double r_gain;
111 double r_atime;
112
113 void getLight(uint8_t setparameterr) {
114         if (setparameterr) {
115                 r_gain=(1<<(2*gain));
116                 r_atime=(256.0-atime)*2.78;
117                 APDS9960setGain(gain);
118                 APDS9960setATime(atime);
119                 _delay_ms(700);
120         }
121         APDS9960getRGBC(&light[0],&light[1],&light[2],&light[3]);
122 }
123
124
125
126
127 int main(void){
128         pack.A=0;
129         pack.B=0;
130         pack.C=0;
131         pack.D=0;
132         pack.CSA1=0x08;
133         pack.CSA2=0x8C;
134         pack.CSB1=0x08;
135         pack.CSB2=0x8C;
136         pack.CSC1=0x08;
137         pack.CSC2=0x8C;
138         pack.CSD1=0x08;
139         pack.CSD2=0x8C;
140         pack.HA=0xFF;
141         pack.LA=0x00;
142         pack.HB=0xFF;
143         pack.LB=0x00;
144         pack.HC=0xFF;
145         pack.LC=0x00;
146         pack.HD=0xFF;
147         pack.LD=0x00;
148         pack.VCCP=0;
149
150         PORTA=0xFF;
151         PORTB=0xFF;
152
153         OWINIT();
154
155         MCUCR &=~(1<<PUD); //All Pins Pullup...
156         MCUCR |=(1<<BODS);
157         //PORTA&=~((1<<PINA0)|(1<<PINA1)|(1<<PINA2)|(1<<PINA3));
158
159         ADCSRA=(1<<ADEN)|(1<ADPS0)|(1<<ADPS2);
160
161         
162         
163         gcontrol=1;
164         ADCSRB|=(1<<ADLAR); 
165         USI_TWI_Master_Initialise();
166         
167         initAPDS9960();
168         //gain=2;
169         //atime=253;
170         getLight(1);
171         sei();
172         
173         //DDRB|=(1<<PINB1);
174
175     while(1)   {
176
177
178                 if (gcontrol) {
179                         //PORTB|=(1<<PINB1);
180                         uint8_t bb=1;
181                         uint8_t bb1=1;
182                         for(uint8_t i=0;i<4;i++){
183                                 if (pack.convc1&bb1) {
184                                         if (pack.convc2&(bb)) {pack.bytes[i*2]=0;pack.bytes[i*2+1]=0;}
185                                         bb=bb<<1;
186                                         if (pack.convc2&(bb)) {pack.bytes[i*2]=0xFF;pack.bytes[i*2+1]=0xFF;}
187                                         bb=bb<<1;
188                                 } else bb=bb<<2;
189                                 bb1=bb1<<1;                             
190                         }
191                         //CHanel A
192                         if (pack.convc1&1) {
193                                 getLight(0);
194                                 while (light[3]>0xF000) {
195                                 //while (light[3]>900) {
196                                         if (gain!=0) {
197                                                 gain--;
198                                                 getLight(1);
199                                         }  else break;
200                                 } 
201                                 if (light[3]<0x2222) {
202                                 //if (light[3]<0x22) {
203                                         if (gain<3) {
204                                                 gain++;
205                                                 getLight(1);
206                                         }
207
208                                 }
209                                 for(uint8_t i=0;i<4;i++) {
210                                         rlight[i]=(double)light[i]/r_atime/r_gain*70.0;
211                                         rlight[i]=(log(rlight[i])*1000)+32767.0;
212                                 }
213                                 cli();pack.A=rlight[0];sei();
214                                 //cli();pack.A=r_gain;sei();
215                                 alarmflag=0;
216                                 if (pack.CSA2&0x08)  //AEH
217                                         if (pack.bytes[1]>pack.HA) {alarmflag=1;pack.CSA2|=0x20;}
218                                 if (pack.CSA2&0x04)  //AEL
219                                         if (pack.bytes[1]<pack.LA) {alarmflag=1;pack.CSA2|=0x10;}
220                         }
221
222                         if (pack.convc1&2) {
223                                 cli();pack.B=rlight[1];sei();
224                                 //cli();pack.B=atime;sei();
225                                 if (pack.CSB2&0x08)  //AEH
226                                         if (pack.bytes[1]>pack.HB) {alarmflag=1;pack.CSB2|=0x20;}
227                                 if (pack.CSB2&0x04)  //AEL
228                                         if (pack.bytes[1]<pack.LB) {alarmflag=1;pack.CSB2|=0x10;}
229                         }
230
231                         if (pack.convc1&4) {
232                                 cli();pack.C=rlight[2];sei();
233                                 //cli();pack.C=light[3];sei();
234                                 if (pack.CSC2&0x08)  //AEH
235                                         if (pack.bytes[1]>pack.HC) {alarmflag=1;pack.CSC2|=0x20;}
236                                 if (pack.CSC2&0x04)  //AEL
237                                         if (pack.bytes[1]<pack.LC) {alarmflag=1;pack.CSC2|=0x10;}
238                         } 
239                         if (pack.convc1&8) {
240                                 cli();pack.D=rlight[3];sei();
241                                 if (pack.CSD2&0x08)  //AEH
242                                         if (pack.bytes[1]>pack.HD) {alarmflag=1;pack.CSD2|=0x20;}
243                                 if (pack.CSD2&0x04)  //AEL
244                                         if (pack.bytes[1]<pack.LD) {alarmflag=1;pack.CSD2|=0x10;}
245                         }
246                         
247                         EXTERN_SLEEP();
248                         //PORTB&=~(1<<PINB1);
249                 }
250
251                 uint8_t bb=1;
252                 for(uint8_t i=0;i<4;i++) {
253                         if (pack.bytes[8+i*2]&0x80) {  //Chanel as output
254                                 if (pack.bytes[8+i*2]&0x40) {
255                                         DDRA|=bb;
256                                 } else {
257                                         DDRA&=~bb;
258                                 }
259                         } else {
260                                 DDRA&=~bb;
261                         }
262                         bb=bb*2;
263                 }
264                 
265 #if  defined(__AVR_ATtiny25__)||defined(__AVR_ATtiny45__)  || defined(__AVR_ATtiny85__)
266                         if (((TIMSK & (1<<TOIE0))==0)&& (mode==0))
267 #endif                  
268 #if  defined(__AVR_ATtiny24__)||defined(__AVR_ATtiny44__)  || defined(__AVR_ATtiny84__) ||defined(__AVR_ATtiny24A__)||defined(__AVR_ATtiny44A__)  || defined(__AVR_ATtiny84A__)
269                         if (((TIMSK0 & (1<<TOIE0))==0)&& (mode==0))
270 #endif
271                           {
272
273                         MCUCR|=(1<<SE)|(1<<SM1);
274                         MCUCR&=~(1<<ISC01);
275                 } else {
276                         MCUCR|=(1<<SE);
277                         MCUCR&=~(1<<SM1);
278                 }
279         //      asm("SLEEP");
280    }
281
282
283 }