program to test the veml7700 class to readout the veml7700 light sensor via i2c

Dependencies:   mbed DevInterfaces I2Cinterfaces VEML7700

Committer:
wbeaumont
Date:
Tue Sep 10 11:20:56 2019 +0000
Revision:
0:37c7223b15ea
Child:
1:2de7e66b1757
test program for the class velm7700  , light sensor i2c

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wbeaumont 0:37c7223b15ea 1 /**
wbeaumont 0:37c7223b15ea 2 * example program for the use of the hts221 class
wbeaumont 0:37c7223b15ea 3 * for more info see the README.MD of the repository
wbeaumont 0:37c7223b15ea 4 * https://github.com/wimbeaumont/peripheral_dev_tst.git
wbeaumont 0:37c7223b15ea 5 * copied from the mbed online compiler envionment
wbeaumont 0:37c7223b15ea 6 * V 0.1 : initial version for software development ( dummy i2c interface
wbeaumont 0:37c7223b15ea 7 *
wbeaumont 0:37c7223b15ea 8 * (C) Wim Beaumont Universiteit Antwerpen 2017 2019
wbeaumont 0:37c7223b15ea 9 *
wbeaumont 0:37c7223b15ea 10 * License see
wbeaumont 0:37c7223b15ea 11 * https://github.com/wimbeaumont/PeripheralDevices/blob/master/LICENSE
wbeaumont 0:37c7223b15ea 12 */
wbeaumont 0:37c7223b15ea 13
wbeaumont 0:37c7223b15ea 14 #define VEML7700EXAMPLEVER "1.56"
wbeaumont 0:37c7223b15ea 15
wbeaumont 0:37c7223b15ea 16 #ifdef __MBED__
wbeaumont 0:37c7223b15ea 17
wbeaumont 0:37c7223b15ea 18 #include "mbed.h"
wbeaumont 0:37c7223b15ea 19
wbeaumont 0:37c7223b15ea 20
wbeaumont 0:37c7223b15ea 21 #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z)
wbeaumont 0:37c7223b15ea 22 PinName const SDA = PTE0;
wbeaumont 0:37c7223b15ea 23 PinName const SCL = PTE1;
wbeaumont 0:37c7223b15ea 24 #elif defined (TARGET_KL05Z)
wbeaumont 0:37c7223b15ea 25 PinName const SDA = PTB4;
wbeaumont 0:37c7223b15ea 26 PinName const SCL = PTB3;
wbeaumont 0:37c7223b15ea 27 #elif defined (TARGET_K20D50M)
wbeaumont 0:37c7223b15ea 28 PinName const SDA = PTB1;
wbeaumont 0:37c7223b15ea 29 PinName const SCL = PTB0;
wbeaumont 0:37c7223b15ea 30 #else
wbeaumont 0:37c7223b15ea 31 #error TARGET NOT DEFINED
wbeaumont 0:37c7223b15ea 32 #endif
wbeaumont 0:37c7223b15ea 33 Serial pc(USBTX, USBRX);
wbeaumont 0:37c7223b15ea 34
wbeaumont 0:37c7223b15ea 35 #include "I2C.h"
wbeaumont 0:37c7223b15ea 36 #include "I2CInterface.h"
wbeaumont 0:37c7223b15ea 37 #include "MBEDI2CInterface.h"
wbeaumont 0:37c7223b15ea 38
wbeaumont 0:37c7223b15ea 39 #else
wbeaumont 0:37c7223b15ea 40
wbeaumont 0:37c7223b15ea 41 #include <cstdio>
wbeaumont 0:37c7223b15ea 42 #include "DummyI2CInterface.h"
wbeaumont 0:37c7223b15ea 43
wbeaumont 0:37c7223b15ea 44 #endif //__MBED__
wbeaumont 0:37c7223b15ea 45
wbeaumont 0:37c7223b15ea 46 #include "dev_interface_def.h"
wbeaumont 0:37c7223b15ea 47 #include "veml7700.h"
wbeaumont 0:37c7223b15ea 48
wbeaumont 0:37c7223b15ea 49
wbeaumont 0:37c7223b15ea 50
wbeaumont 0:37c7223b15ea 51
wbeaumont 0:37c7223b15ea 52 #ifdef __MBED__
wbeaumont 0:37c7223b15ea 53 MBEDI2CInterface mbedi2c( SDA, SCL);
wbeaumont 0:37c7223b15ea 54 MBEDI2CInterface* mbedi2cp= &mbedi2c ;
wbeaumont 0:37c7223b15ea 55 #else
wbeaumont 0:37c7223b15ea 56 DummyI2CInterface mbedi2c;
wbeaumont 0:37c7223b15ea 57 DummyI2CInterface* mbedi2cp= &mbedi2c;
wbeaumont 0:37c7223b15ea 58 #endif //__MBED__
wbeaumont 0:37c7223b15ea 59
wbeaumont 0:37c7223b15ea 60
wbeaumont 0:37c7223b15ea 61 I2CInterface* i2cdev= mbedi2cp;
wbeaumont 0:37c7223b15ea 62
wbeaumont 0:37c7223b15ea 63
wbeaumont 0:37c7223b15ea 64 int main(void) {
wbeaumont 0:37c7223b15ea 65
wbeaumont 0:37c7223b15ea 66 // get the version of getVersion
wbeaumont 0:37c7223b15ea 67 char dummystr[50];
wbeaumont 0:37c7223b15ea 68 getVersion gv;
wbeaumont 0:37c7223b15ea 69 int err;
wbeaumont 0:37c7223b15ea 70 printf("VEML7700 lux meter example program version %s,\n\rcompile date %s time %s\n\r",
wbeaumont 0:37c7223b15ea 71 VEML7700EXAMPLEVER,__DATE__,__TIME__);
wbeaumont 0:37c7223b15ea 72 printf("getVersion :%s\n\r",gv.getversioninfo());
wbeaumont 0:37c7223b15ea 73 gv.dev_interface_def_version(dummystr);
wbeaumont 0:37c7223b15ea 74 printf("%s\n\r ",dummystr);
wbeaumont 0:37c7223b15ea 75 printf( "i2c dev info:%s\n\r",i2cdev->getversioninfo());
wbeaumont 0:37c7223b15ea 76 VEML7700 luxm ( i2cdev);
wbeaumont 0:37c7223b15ea 77 printf ( "VEML7700 lib version :%s\n\r ",luxm.getversioninfo());
wbeaumont 0:37c7223b15ea 78 err=luxm.get_status( );
wbeaumont 0:37c7223b15ea 79 if( err) {
wbeaumont 0:37c7223b15ea 80 printf("get error %d after init \n\r", err);
wbeaumont 0:37c7223b15ea 81 }
wbeaumont 0:37c7223b15ea 82
wbeaumont 0:37c7223b15ea 83
wbeaumont 0:37c7223b15ea 84 luxm.set_integrationtime(1000); // > max
wbeaumont 0:37c7223b15ea 85 float gain=.125;//default
wbeaumont 0:37c7223b15ea 86 bool sd ,ie; u16 pers_protect;int IntT=8; float gainrb;
wbeaumont 0:37c7223b15ea 87 while ( 1) {
wbeaumont 0:37c7223b15ea 88 int alsbits = luxm.get_als_bits ( );
wbeaumont 0:37c7223b15ea 89 err=luxm.get_status( );
wbeaumont 0:37c7223b15ea 90 if( err) {
wbeaumont 0:37c7223b15ea 91 printf("get error %d after get als init \n\r", err);
wbeaumont 0:37c7223b15ea 92 }else {
wbeaumont 0:37c7223b15ea 93 if ( alsbits < 0xFF ) {
wbeaumont 0:37c7223b15ea 94 if (gain < 2.0 ) {
wbeaumont 0:37c7223b15ea 95 gain=gain*2;
wbeaumont 0:37c7223b15ea 96 printf("increase gain to %f \n\r", gain);
wbeaumont 0:37c7223b15ea 97 luxm.set_gain(gain);
wbeaumont 0:37c7223b15ea 98 if( gain > 2.0 ) gain =2.0;
wbeaumont 0:37c7223b15ea 99
wbeaumont 0:37c7223b15ea 100 } //else max gain
wbeaumont 0:37c7223b15ea 101 }
wbeaumont 0:37c7223b15ea 102 if ( alsbits > 0x2FFF) {
wbeaumont 0:37c7223b15ea 103 if( gain > .125 ) {
wbeaumont 0:37c7223b15ea 104 gain = gain/2;
wbeaumont 0:37c7223b15ea 105 printf("decrease gain to %f \n\r", gain);
wbeaumont 0:37c7223b15ea 106 luxm.set_gain(gain);
wbeaumont 0:37c7223b15ea 107 if (gain < .125) gain=0.125;
wbeaumont 0:37c7223b15ea 108 } // else min gain
wbeaumont 0:37c7223b15ea 109 }
wbeaumont 0:37c7223b15ea 110 } // else if err
wbeaumont 0:37c7223b15ea 111 int whitebits = luxm.get_white_ch_bits ( );
wbeaumont 0:37c7223b15ea 112 if( err) {
wbeaumont 0:37c7223b15ea 113 printf("get error %d after get white init \n\r", err);
wbeaumont 0:37c7223b15ea 114 }
wbeaumont 0:37c7223b15ea 115 luxm.decode_Reg0( sd , ie, pers_protect, IntT, gainrb );
wbeaumont 0:37c7223b15ea 116 err=luxm.get_status( );
wbeaumont 0:37c7223b15ea 117 if( err) {
wbeaumont 0:37c7223b15ea 118 printf("get error %d after read cmd reg \n\r", err);
wbeaumont 0:37c7223b15ea 119 }
wbeaumont 0:37c7223b15ea 120 printf( " als %04x , white %04x @gain %f reg0: sd %d, ie %d , Pers_prot %d, IntT %d, gain %f \n\r",
wbeaumont 0:37c7223b15ea 121 alsbits, whitebits,gain,(int) sd ,(int) ie,(int) pers_protect, IntT, gainrb );
wbeaumont 0:37c7223b15ea 122 i2cdev->wait_for_ms(1000);
wbeaumont 0:37c7223b15ea 123 }
wbeaumont 0:37c7223b15ea 124 return 1;
wbeaumont 0:37c7223b15ea 125 }
wbeaumont 0:37c7223b15ea 126
wbeaumont 0:37c7223b15ea 127