Rohm optical pulse wave sensor bh1790glc hello world. Printing values of wave to serial.

Dependencies:   RegisterWriter rohm-bh1790glc-driver

Fork of kionix-kx123-hello by Rohm

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002 The MIT License (MIT)
00003 Copyright (c) 2017 Rohm Semiconductor
00004 
00005 Permission is hereby granted, free of charge, to any person obtaining a
00006 copy of this software and associated documentation files (the
00007 "Software"), to deal in the Software without restriction, including
00008 without limitation the rights to use, copy, modify, merge, publish,
00009 distribute, sublicense, and/or sell copies of the Software, and to
00010 permit persons to whom the Software is furnished to do so, subject to
00011 the following conditions:
00012 
00013 The above copyright notice and this permission notice shall be included
00014 in all copies or substantial portions of the Software.
00015 
00016 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00017 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00018 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00019 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
00020 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
00021 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
00022 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00023 */
00024 
00025 #include "RegisterWriter/RegisterWriter/rohm_hal2.h"
00026 
00027 #include "rohm-bh1790glc-driver/bh1790glc_registers.h"
00028 #include "rohm-bh1790glc-driver/bh1790glc.h"
00029 
00030 DigitalOut led1(LED1);
00031 Serial pc(USBTX, USBRX);
00032 
00033 I2C i2c(I2C_SDA, I2C_SCL);
00034 RegisterWriter i2c_rw(i2c);
00035 BH1790GLC bh1790glc(i2c_rw);
00036 
00037 /* Global variables for timer */
00038 bool interval_elapsed;
00039 Ticker ticker;
00040 
00041 void timer_isr()
00042 {
00043     interval_elapsed = true;
00044 }
00045 
00046 int main()
00047 {
00048     uint16_t data[2];
00049     int error;
00050 
00051     pc.baud(115200);
00052     i2c.frequency(400000);
00053 
00054     wait(0.1);
00055 
00056     do{ //until init OK.
00057         error = bh1790glc.set_default_on();
00058         Thread::wait(50);
00059         led1 = !led1;   //Red led toggle
00060         Thread::wait(200);
00061         }
00062     while (error);
00063     led1 = 0; //Red off
00064 
00065     interval_elapsed = false;
00066     ticker.attach(&timer_isr, 0.03125);                 //32Hz
00067 
00068     while(true) {
00069         if (interval_elapsed) {                         //Wait until ISR
00070             error = bh1790glc.getresults(data);
00071             if (!error) {
00072                 int i;
00073                 pc.printf("%d, \t%d", data[1], data[0]);
00074                 for (i=0; i < (data[1]/200); i++) {
00075                     pc.printf(" ");
00076                     }
00077                 pc.printf("I\r\n");
00078             }
00079             interval_elapsed = false;
00080         }
00081     }
00082 } /* main() */
00083