Operácie

Si1143.h: Rozdiel medzi revíziami

Z SensorWiki

(Vytvorená stránka „<source lang="c"> Si1143 Example: #define sleep 100 #include <Wire.h> #include "Si1143.h"; int bias1,bias2,bias3; unsigned int PS1,PS2,PS3; int blinktime,count...“)
 
 
Riadok 3: Riadok 3:
 
/* Si1143 Example */
 
/* Si1143 Example */
  
 +
#ifndef SI1143_h
 +
#define SI1143_h
  
#define sleep  100
+
#define IR_ADDRESS 0x5A
#include <Wire.h>
 
#include "Si1143.h";
 
  
int bias1,bias2,bias3;
+
// register addresses
unsigned int PS1,PS2,PS3;
+
#define PART_ID        0x00
int blinktime,counter,counter1,counter2,Ledposition;
+
#define REV_ID        0x01
unsigned int Light_Reading;
+
#define SEQ_ID        0x02  //Si114x-A11 (MAJOR_SEQ=1, MINOR_SEQ=1)
byte LowB,HighB;
+
#define INT_CFG        0x03
bool selected;
+
#define IRQ_ENABLE    0x04
unsigned long time;
+
#define IRQ_MODE1      0x05
 +
#define IRQ_MODE2      0x06
 +
#define HW_KEY        0x07
  
void setup()
+
#define MEAS_RATE      0x08
{
+
#define ALS_RATE      0x09
  Serial.begin(9600);
+
#define PS_RATE        0x0A
  delay(25);
 
  Wire.begin(); // join i2c bus (address optional for master)
 
  delay(25);
 
 
  write_reg(HW_KEY, 0x17); // Setting up LED Power to full
 
  write_reg(PS_LED21,0xFF);
 
  write_reg(PS_LED3, 0x0F);
 
  param_set(CHLIST,0b00010111);
 
 
  char parameter = read_reg(PARAM_RD,1);
 
  delay(1000);
 
 
  bias();
 
 
  counter = 0;
 
  counter1 = 0;
 
  counter2 = 0;
 
  selected = 0;
 
  StartGame();
 
}
 
  
void loop()
+
#define ALS_LOW_TH0   0x0B
{
+
#define ALS_LOW_TH1   0x0C
  write_reg(COMMAND,0b00000101); // Get a reading
+
#define ALS_HI_TH0     0x0D
  delay(5);
+
#define ALS_HI_TH1     0x0E
 
  LowB = read_reg(PS1_DATA0,1); // Read the data for the first LED
 
  HighB = read_reg(PS1_DATA1,1);
 
  PS1 = ((HighB * 255) + LowB) - bias1;
 
 
  LowB = read_reg(PS2_DATA0,1);  // Read the data for the second LED
 
  HighB = read_reg(PS2_DATA1,1);
 
  PS2 = (HighB * 255) + LowB - bias2;
 
 
  LowB = read_reg(PS3_DATA0,1);  // Read the data for the third LED
 
  HighB = read_reg(PS3_DATA1,1);
 
  PS3 = (HighB * 255) + LowB - bias3;
 
 
  Light_Reading = read_light(); 
 
    if(selected != 1) {
 
    if (counter > 5 || counter1 > 5 || counter2 > 5){
 
      selected = 0; 
 
     
 
  }
 
  }
 
 
 
   do_magic();
 
    
 
 
                                               
 
  if (PS1 < 1000 || PS2 < 1000 || PS3 < 1000){
 
      if (PS1 < PS2 && PS1 < PS3){
 
          if (selected == 0){
 
          counter++;
 
          Ledposition = 1;
 
          counter1 = 0;
 
          counter2 = 0;
 
         
 
          }
 
      }else if(PS2 > PS1 && PS2 > PS3){
 
          if (selected == 0){
 
          counter1++;
 
          Ledposition = 2;
 
          counter = 0;
 
          counter2 = 0;
 
        }
 
      }else if(PS3 > PS1 && PS3 > PS2){
 
          if (selected == 0){
 
          counter2++;
 
          Ledposition = 3;
 
          counter = 0;
 
          counter1 = 0;
 
        }
 
       
 
      }
 
    }else{
 
    counter = 0;
 
    counter1 = 0;
 
     counter2 = 0;
 
     selected = 0;
 
  }
 
}
 
  
unsigned int read_light(){  // Read light sensor
+
#define PS_LED21      0x0F
  write_reg(COMMAND,0b00000110);
+
#define PS_LED3        0x10
  delay(sleep);
 
  byte LowB = read_reg(ALS_VIS_DATA0,1);
 
  byte HighB = read_reg(ALS_VIS_DATA1,1);
 
  return (HighB * 255) + LowB;
 
}
 
  
void param_set(byte address, byte val)  // Set Parameter
+
#define PS1_TH0        0x11
{
+
#define PS1_TH1        0x12
  write_reg(PARAM_WR, val);
+
#define PS2_TH0        0x13
  write_reg(COMMAND, 0xA0|address);
+
#define PS2_TH1        0x14
}
+
#define PS3_TH0        0x15
  
char read_reg(unsigned char address, int num_data) // Read a Register
+
#define PS3_TH1        0x16
{
+
#define PARAM_WR      0x17
  unsigned char data;
+
#define COMMAND        0x18
  
  Wire.beginTransmission(IR_ADDRESS);
+
#define RESPONSE      0x20
  Wire.write(address);
+
#define IRQ_STATUS    0x21
  Wire.endTransmission();
 
  
  Wire.requestFrom(IR_ADDRESS, num_data);
+
#define ALS_VIS_DATA0  0x22
   
+
#define ALS_VIS_DATA1 0x23
   while(Wire.available() < num_data);
+
#define ALS_IR_DATA0   0x24
+
#define ALS_IR_DATA1   0x25
   return Wire.read();
 
}
 
  
void write_reg(byte address, byte val) {  // Write a resigter
+
#define PS1_DATA0      0x26
  Wire.beginTransmission(IR_ADDRESS);
+
#define PS1_DATA1      0x27
  Wire.write(address);   
+
#define PS2_DATA0      0x28
  Wire.write(val);     
+
#define PS2_DATA1      0x29
  Wire.endTransmission();   
+
#define PS3_DATA0      0x2A
}
+
#define PS3_DATA1      0x2B
  
void bias(void){  // Bias during start up
 
 
  for (int i=0; i<20; i++){
 
  write_reg(COMMAND,0b00000101);
 
  delay(50);
 
 
  byte LowB = read_reg(PS1_DATA0,1);
 
  byte HighB = read_reg(PS1_DATA1,1);
 
 
  bias1 += ((HighB * 255) + LowB) / 20;
 
 
  LowB = read_reg(PS2_DATA0,1);
 
  HighB = read_reg(PS2_DATA1,1);
 
 
  bias2 += ((HighB * 255) + LowB) / 20;
 
 
  LowB = read_reg(PS3_DATA0,1);
 
  HighB = read_reg(PS3_DATA1,1);
 
 
  bias3 += ((HighB * 255) + LowB) / 20;
 
}
 
}
 
  
void touch_select(){    // just a blink routine for when something is selected
+
#define AUX_DATA0      0x2C
    switch (Ledposition) {
+
#define AUX_DATA1      0x2D
       
 
      case 1:
 
        Serial.println("Priloz prst k LED1");
 
        time = millis();
 
      break;
 
     
 
      case 2:
 
        Serial.println("Priloz prst k LED2");
 
      break;
 
     
 
      case 3:
 
        Serial.println("Priloz prst k LED3");   
 
      break;
 
     
 
    }
 
}
 
unsigned int PS1_old=0,PS2_old=0, PS3_old=0;
 
int diff1=0,diff2=0,diff3=0, sum1=0,sum2=0,sum3=0;
 
int rand1 ;
 
int aktual=0,timing=0;
 
boolean succes=true;
 
  
void do_magic(){
+
#define PARAM_RD       0x2E
 
+
#define CHIP_STAT     0x30
 
+
#define ANA_IN_KEY     0x3B
 
  diff1 = PS1-PS1_old;
 
  diff2 = PS2-PS2_old;
 
  diff3 = PS3-PS3_old;
 
  diff1=abs(diff1);
 
  diff2=abs(diff2);
 
  diff3=abs(diff3);
 
 
 
  PS1_old=PS1;
 
  PS2_old=PS2;
 
  PS3_old=PS3;
 
 
 
 
 
 
  if(diff1>diff2 && diff1 >diff3){
 
    if(sum2>0 || sum3 >0){
 
       sum2=0; sum3=0;
 
    }
 
    sum1=sum1+1; 
 
   
 
  }else if(diff2> diff1 && diff2 >diff3){
 
     if(sum1>0 || sum2 >0){
 
      sum1=0; sum3=0;
 
     }
 
    sum2=sum2+1;
 
   
 
  }else if(diff3>diff1 && diff3>diff2){   
 
    if(sum1>0 || sum2 >0){
 
      sum1=0; sum2=0;
 
    }
 
    sum3=sum3+1;
 
  }
 
if(sum1>0 || sum2>0 || sum3 > 0){
 
  if(sum1>4 && rand1==1){
 
    Serial.println("vybrata je LED1");
 
    sum1=0;
 
    succes=true;
 
    Serial.println("Vyhra");
 
  }
 
  if(sum2>4 && rand1==2){
 
    Serial.println("vybrata je LED2");
 
    sum2=0;
 
    succes=true;
 
    Serial.println("Vyhra");
 
  }
 
  if(sum3>4 && rand1==3){
 
      Serial.println("vybrata je LED3");
 
      sum3=0;
 
      succes=true;
 
      Serial.println("Vyhra");
 
  }
 
  if(succes){
 
  rand1=random(1,4);
 
 
 
  if(rand1>=1 && rand1<2){
 
    rand1=1;
 
  }else if(rand1>=2 && rand1<3){
 
    rand1 =2;
 
  }else{
 
    rand1=3;
 
  }
 
 
 
  time = millis();
 
  aktual=time-timing;
 
  timing=time;
 
  switch(rand1){
 
  case 1:
 
    Serial.println("Priloz prst k LED cislo 1");
 
  break;
 
  case 2:
 
    Serial.println("Priloz prst k LED cislo 2");
 
  break; 
 
  case 3:
 
    Serial.println("Priloz prst k LED cislo 3");
 
  break;
 
 
 
  }
 
  Serial.print("Time: ");
 
  Serial.println(aktual);
 
  succes=false;
 
  }
 
}
 
delay(10);
 
}
 
  
 +
// ram addresses
  
void StartGame(){
+
#define I2C_ADDR                  0x00
Serial.println("                            HRA: VYBER LED               ");
+
#define CHLIST                    0x01
Serial.println("CIEL: Priloz prst co najrychlejsie k spravnej LEDke senzora podla  zobrazenia na monitore");
+
#define PSLED12_SELECT            0x02
Serial.println("INSTRUKCIE: Rozlozenie LEDiek na snimaci:");
+
#define PSLED3_SELECT            0x03
Serial.println("        LED2");
+
#define PS_ENCODING              0x05
Serial.println("        - ");
+
#define ALS_ENCODING              0x06
Serial.println("        - ");
+
#define PS1_ADCMUX               0x07
Serial.println("        - ");
+
#define PS2_ADCMUX                0x08
Serial.println("        - ");
+
#define PS3_ADCMUX                0x09
Serial.println("        - ");
+
#define PS_ADC_COUNTER            0x0A
Serial.println("        - ");
+
#define PS_ADC_GAIN              0x0B
Serial.println("        - ");
+
#define PS_ADC_MISC              0x0C
Serial.println("        - ");
+
#define ALS_IR_ADCMUX            0x0E
Serial.println("        - ");
+
#define AUX_ADCMUX                0x0F
Serial.println("       LED1 -------------------- LED3 ");
+
#define ALS_VIS_ADC_COUNTER      0x10
Serial.println(" ");
+
#define ALS_VIS_ADC_GAIN          0x11
Serial.println("Hra zacne prilozenim prstu k  LED3 na senzore ");
+
#define ALS_VIS_ADC_MISC          0x12
 +
#define ALS_HYST                  0x16
 +
#define PS_HYST                  0x17
 +
#define PS_HISTORY                0x18
 +
#define ALS_HISTORY              0x19
 +
#define ADC_OFFSET                0x1A
 +
#define LED_REC                  0x1C
 +
#define ALS_IR_ADC_COUNTER       0x1D
 +
#define ALS_IR_ADC_GAIN          0x1E
 +
#define ALS_IR_ADC_MISC          0x1F
 +
 
 +
#endif
  
}
 
  
 
</source>
 
</source>

Aktuálna revízia z 08:24, 19. máj 2016

/* Si1143 Example */

#ifndef SI1143_h
#define SI1143_h

#define IR_ADDRESS 0x5A

// register addresses
#define PART_ID        0x00
#define REV_ID         0x01
#define SEQ_ID         0x02  //Si114x-A11 (MAJOR_SEQ=1, MINOR_SEQ=1)
#define INT_CFG        0x03
#define IRQ_ENABLE     0x04
#define IRQ_MODE1      0x05
#define IRQ_MODE2      0x06
#define HW_KEY         0x07

#define MEAS_RATE      0x08
#define ALS_RATE       0x09
#define PS_RATE        0x0A

#define ALS_LOW_TH0    0x0B
#define ALS_LOW_TH1    0x0C
#define ALS_HI_TH0     0x0D
#define ALS_HI_TH1     0x0E

#define PS_LED21       0x0F
#define PS_LED3        0x10

#define PS1_TH0        0x11
#define PS1_TH1        0x12
#define PS2_TH0        0x13
#define PS2_TH1        0x14
#define PS3_TH0        0x15

#define PS3_TH1        0x16
#define PARAM_WR       0x17
#define COMMAND        0x18

#define RESPONSE       0x20
#define IRQ_STATUS     0x21

#define ALS_VIS_DATA0  0x22
#define ALS_VIS_DATA1  0x23
#define ALS_IR_DATA0   0x24
#define ALS_IR_DATA1   0x25

#define PS1_DATA0      0x26
#define PS1_DATA1      0x27
#define PS2_DATA0      0x28
#define PS2_DATA1      0x29
#define PS3_DATA0      0x2A
#define PS3_DATA1      0x2B


#define AUX_DATA0      0x2C
#define AUX_DATA1      0x2D

#define PARAM_RD       0x2E
#define CHIP_STAT      0x30
#define ANA_IN_KEY     0x3B

// ram addresses

#define I2C_ADDR                  0x00
#define CHLIST                    0x01
#define PSLED12_SELECT            0x02 
#define PSLED3_SELECT             0x03
#define PS_ENCODING               0x05
#define ALS_ENCODING              0x06
#define PS1_ADCMUX                0x07
#define PS2_ADCMUX                0x08
#define PS3_ADCMUX                0x09
#define PS_ADC_COUNTER            0x0A
#define PS_ADC_GAIN               0x0B
#define PS_ADC_MISC               0x0C
#define ALS_IR_ADCMUX             0x0E
#define AUX_ADCMUX                0x0F
#define ALS_VIS_ADC_COUNTER       0x10
#define ALS_VIS_ADC_GAIN          0x11
#define ALS_VIS_ADC_MISC          0x12
#define ALS_HYST                  0x16
#define PS_HYST                   0x17
#define PS_HISTORY                0x18
#define ALS_HISTORY               0x19
#define ADC_OFFSET                0x1A
#define LED_REC                   0x1C
#define ALS_IR_ADC_COUNTER        0x1D
#define ALS_IR_ADC_GAIN           0x1E
#define ALS_IR_ADC_MISC           0x1F

#endif