Rohm / Mbed OS kionix-kx123-hello

Dependencies:   RegisterWriter kionix-kx123-driver

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*   Copyright 2016 Rohm Semiconductor
00002 
00003    Licensed under the Apache License, Version 2.0 (the "License");
00004    you may not use this file except in compliance with the License.
00005    You may obtain a copy of the License at
00006 
00007        http://www.apache.org/licenses/LICENSE-2.0
00008 
00009    Unless required by applicable law or agreed to in writing, software
00010    distributed under the License is distributed on an "AS IS" BASIS,
00011    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012    See the License for the specific language governing permissions and
00013    limitations under the License.
00014 */
00015 
00016 #include "RegisterWriter/RegisterWriter/rohm_hal2.h"
00017 
00018 #include "kionix-kx123-driver/kx123_registers.h"
00019 #include "kionix-kx123-driver/kx123.h"
00020 
00021 DigitalOut led1(LED1);
00022 DigitalOut led2(LED2);
00023 Serial pc(USBTX, USBRX);
00024 
00025 I2C i2c(I2C_SDA, I2C_SCL);
00026 RegisterWriter i2c_rw(i2c);
00027 
00028 int main() {
00029     bool error;
00030 
00031     KX123 acc(i2c_rw);
00032     float xmax = 0, xmin = 0;
00033 
00034     do{ //init
00035         error = acc.set_defaults();
00036         Thread::wait(50);
00037         led1 = !led1;   //Red led toggle
00038         Thread::wait(200);
00039         }
00040     while (error);
00041     led1 = 0; //Red off
00042 
00043     while (true) {
00044         float res[3];
00045 
00046         Thread::wait(50);
00047         error = acc.getresults_g(&res[0]);
00048         if (error){
00049             led2 = 0;   //Green off
00050             led1 = 1;   //Red on
00051             xmax = 0;
00052             xmin = 0;
00053             pc.printf("Reattach sensor. Note that if reconnecting also sensor config pins\r\n");
00054             pc.printf("(f.ex. SPI/I2C selection) connection should be made in correct pin order.\r\n");
00055 
00056             while (error){
00057                 error = acc.set_defaults();
00058                 led1 = !led1;   //Red off
00059                 Thread::wait(100);
00060                 }
00061             led1 = 0; //Red off
00062             continue; //loop again if read failed
00063             }
00064         //else no error, printout values
00065         
00066         //pc.printf("X[%0.2f], Y[%0.2f], Z[%0.2f]\r\n", res[0], res[1], res[2]);
00067         if (xmax < res[0]){
00068             xmax = res[0]; 
00069             pc.printf("Xmax[%0.2f], Xmin[%0.2f]\r\n", xmax, xmin);
00070             }
00071         if (xmin > res[0]){
00072             xmin = res[0]; 
00073             pc.printf("Xmax[%0.2f], Xmin[%0.2f]\r\n", xmax, xmin);
00074             }
00075 
00076         led2 = !led2;   //Toggle green after each successfull read
00077     }
00078 }
00079