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

Committer:
MikkoZ
Date:
Thu Sep 29 15:14:31 2016 +0000
Revision:
0:bb054fb43c57
Child:
3:76f82d0d3c8a
Initial version with max/min G -value printing using 8G range.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MikkoZ 0:bb054fb43c57 1 /* Copyright 2016 Rohm Semiconductor
MikkoZ 0:bb054fb43c57 2
MikkoZ 0:bb054fb43c57 3 Licensed under the Apache License, Version 2.0 (the "License");
MikkoZ 0:bb054fb43c57 4 you may not use this file except in compliance with the License.
MikkoZ 0:bb054fb43c57 5 You may obtain a copy of the License at
MikkoZ 0:bb054fb43c57 6
MikkoZ 0:bb054fb43c57 7 http://www.apache.org/licenses/LICENSE-2.0
MikkoZ 0:bb054fb43c57 8
MikkoZ 0:bb054fb43c57 9 Unless required by applicable law or agreed to in writing, software
MikkoZ 0:bb054fb43c57 10 distributed under the License is distributed on an "AS IS" BASIS,
MikkoZ 0:bb054fb43c57 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
MikkoZ 0:bb054fb43c57 12 See the License for the specific language governing permissions and
MikkoZ 0:bb054fb43c57 13 limitations under the License.
MikkoZ 0:bb054fb43c57 14 */
MikkoZ 0:bb054fb43c57 15
MikkoZ 0:bb054fb43c57 16 #include "RegisterWriter/RegisterWriter/rohm_hal2.h"
MikkoZ 0:bb054fb43c57 17
MikkoZ 0:bb054fb43c57 18 #include "kionix-kx123-driver/kx123_registers.h"
MikkoZ 0:bb054fb43c57 19 #include "kionix-kx123-driver/kx123.h"
MikkoZ 0:bb054fb43c57 20
MikkoZ 0:bb054fb43c57 21 DigitalOut led1(LED1);
MikkoZ 0:bb054fb43c57 22 DigitalOut led2(LED2);
MikkoZ 0:bb054fb43c57 23 Serial pc(USBTX, USBRX);
MikkoZ 0:bb054fb43c57 24
MikkoZ 0:bb054fb43c57 25 I2C i2c(I2C_SDA, I2C_SCL);
MikkoZ 0:bb054fb43c57 26 RegisterWriter i2c_rw(i2c);
MikkoZ 0:bb054fb43c57 27
MikkoZ 0:bb054fb43c57 28 int main() {
MikkoZ 0:bb054fb43c57 29 bool error;
MikkoZ 0:bb054fb43c57 30
MikkoZ 0:bb054fb43c57 31 KX123 acc(i2c_rw);
MikkoZ 0:bb054fb43c57 32 float xmax = 0, xmin = 0;
MikkoZ 0:bb054fb43c57 33
MikkoZ 0:bb054fb43c57 34 do{ //init
MikkoZ 0:bb054fb43c57 35 error = acc.set_defaults();
MikkoZ 0:bb054fb43c57 36 Thread::wait(50);
MikkoZ 0:bb054fb43c57 37 led1 = !led1; //Red led toggle
MikkoZ 0:bb054fb43c57 38 Thread::wait(200);
MikkoZ 0:bb054fb43c57 39 }
MikkoZ 0:bb054fb43c57 40 while (error);
MikkoZ 0:bb054fb43c57 41 led1 = 0; //Red off
MikkoZ 0:bb054fb43c57 42
MikkoZ 0:bb054fb43c57 43 while (true) {
MikkoZ 0:bb054fb43c57 44 float res[3];
MikkoZ 0:bb054fb43c57 45
MikkoZ 0:bb054fb43c57 46 Thread::wait(50);
MikkoZ 0:bb054fb43c57 47 error = acc.getresults_g(&res[0]);
MikkoZ 0:bb054fb43c57 48 if (error){
MikkoZ 0:bb054fb43c57 49 led2 = 0; //Green off
MikkoZ 0:bb054fb43c57 50 led1 = 1; //Red on
MikkoZ 0:bb054fb43c57 51 xmax = 0;
MikkoZ 0:bb054fb43c57 52 xmin = 0;
MikkoZ 0:bb054fb43c57 53 pc.printf("Reattach sensor. Note that if reconnecting also sensor config pins\r\n");
MikkoZ 0:bb054fb43c57 54 pc.printf("(f.ex. SPI/I2C selection) connection should be made in correct pin order.\r\n");
MikkoZ 0:bb054fb43c57 55
MikkoZ 0:bb054fb43c57 56 while (error){
MikkoZ 0:bb054fb43c57 57 error = acc.set_defaults();
MikkoZ 0:bb054fb43c57 58 led1 = !led1; //Red off
MikkoZ 0:bb054fb43c57 59 Thread::wait(100);
MikkoZ 0:bb054fb43c57 60 }
MikkoZ 0:bb054fb43c57 61 led1 = 0; //Red off
MikkoZ 0:bb054fb43c57 62 continue; //loop again if read failed
MikkoZ 0:bb054fb43c57 63 }
MikkoZ 0:bb054fb43c57 64 //else no error, printout values
MikkoZ 0:bb054fb43c57 65
MikkoZ 0:bb054fb43c57 66 //pc.printf("X[%0.2f], Y[%0.2f], Z[%0.2f]\r\n", res[0], res[1], res[2]);
MikkoZ 0:bb054fb43c57 67 if (xmax < res[0]){
MikkoZ 0:bb054fb43c57 68 xmax = res[0];
MikkoZ 0:bb054fb43c57 69 pc.printf("Xmax[%0.2f], Xmin[%0.2f]\r\n", xmax, xmin);
MikkoZ 0:bb054fb43c57 70 }
MikkoZ 0:bb054fb43c57 71 if (xmin > res[0]){
MikkoZ 0:bb054fb43c57 72 xmin = res[0];
MikkoZ 0:bb054fb43c57 73 pc.printf("Xmax[%0.2f], Xmin[%0.2f]\r\n", xmax, xmin);
MikkoZ 0:bb054fb43c57 74 }
MikkoZ 0:bb054fb43c57 75
MikkoZ 0:bb054fb43c57 76 led2 = !led2; //Toggle green after each successfull read
MikkoZ 0:bb054fb43c57 77 }
MikkoZ 0:bb054fb43c57 78 }
MikkoZ 0:bb054fb43c57 79