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
Diff: main.cpp
- Revision:
- 3:76f82d0d3c8a
- Parent:
- 0:bb054fb43c57
- Child:
- 4:50eff3af63c7
diff -r 7fe25750b23d -r 76f82d0d3c8a main.cpp --- a/main.cpp Thu Oct 06 10:33:43 2016 +0000 +++ b/main.cpp Fri Jun 16 11:59:58 2017 +0000 @@ -1,38 +1,60 @@ -/* Copyright 2016 Rohm Semiconductor - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at +/* +The MIT License (MIT) +Copyright (c) 2017 Rohm Semiconductor - http://www.apache.org/licenses/LICENSE-2.0 +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. +The above copyright notice and this permission notice shall be included +in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "RegisterWriter/RegisterWriter/rohm_hal2.h" -#include "kionix-kx123-driver/kx123_registers.h" -#include "kionix-kx123-driver/kx123.h" +#include "rohm-bh1790glc-driver/bh1790glc_registers.h" +#include "rohm-bh1790glc-driver/bh1790glc.h" DigitalOut led1(LED1); -DigitalOut led2(LED2); Serial pc(USBTX, USBRX); I2C i2c(I2C_SDA, I2C_SCL); RegisterWriter i2c_rw(i2c); +BH1790GLC bh1790glc(i2c_rw); -int main() { - bool error; +/* Global variables for timer */ +bool interval_elapsed; +Ticker ticker; + +void timer_isr() +{ + interval_elapsed = true; +} - KX123 acc(i2c_rw); - float xmax = 0, xmin = 0; +int main() +{ + uint16_t data[2]; + int error; - do{ //init - error = acc.set_defaults(); + pc.baud(115200); + i2c.frequency(400000); + + wait(0.1); + + do{ //until init OK. + error = bh1790glc.set_default_on(); Thread::wait(50); led1 = !led1; //Red led toggle Thread::wait(200); @@ -40,40 +62,18 @@ while (error); led1 = 0; //Red off - while (true) { - float res[3]; - - Thread::wait(50); - error = acc.getresults_g(&res[0]); - if (error){ - led2 = 0; //Green off - led1 = 1; //Red on - xmax = 0; - xmin = 0; - pc.printf("Reattach sensor. Note that if reconnecting also sensor config pins\r\n"); - pc.printf("(f.ex. SPI/I2C selection) connection should be made in correct pin order.\r\n"); + interval_elapsed = false; + ticker.attach(&timer_isr, 0.03125); //32Hz - while (error){ - error = acc.set_defaults(); - led1 = !led1; //Red off - Thread::wait(100); - } - led1 = 0; //Red off - continue; //loop again if read failed + while(true) { + if (interval_elapsed) { //Wait until ISR + error = bh1790glc.getresults(data); + if (!error) { + pc.printf("%d, %d\r\n", data[1], data[0]); + //pc.printf("STX,%d,ETX\r\n",data[1]); //for lazurite graph } - //else no error, printout values - - //pc.printf("X[%0.2f], Y[%0.2f], Z[%0.2f]\r\n", res[0], res[1], res[2]); - if (xmax < res[0]){ - xmax = res[0]; - pc.printf("Xmax[%0.2f], Xmin[%0.2f]\r\n", xmax, xmin); - } - if (xmin > res[0]){ - xmin = res[0]; - pc.printf("Xmax[%0.2f], Xmin[%0.2f]\r\n", xmax, xmin); - } + interval_elapsed = false; + } + } +} /* main() */ - led2 = !led2; //Toggle green after each successfull read - } -} -