Demo application of reading 6 different sensors and outputting values on UART. This demo can be used with Rohm tileshield using tiles for BH1726, BD1020, RPR0521, BH1745, ML8511 and BD7411. Developed and tested using Renesas GR-Peach -board.

Dependencies:   rohm-bh1726 rohm-bh1745 rohm-rpr0521 rohm-sensor-hal

Fork of rohm-bd7411g-hello by Rohm

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 "rohm-sensor-hal/rohm-sensor-hal/rohm_hal.h"       //mbed.h, types, DEBUG_print*
00017 #include "rohm-sensor-hal/rohm-sensor-hal/I2CCommon.h"
00018 
00019 #include "rohm-bh1726/rohm-bh1726/bh1726_driver.h"
00020 #include "rohm-bh1726/rohm-bh1726/bh1726.h"
00021 #include "rohm-rpr0521/rohm-rpr0521/rpr0521_driver.h"
00022 #include "rohm-rpr0521/rohm-rpr0521/rpr0521.h"
00023 #include "rohm-bh1745/rohm-bh1745/bh1745_driver.h"
00024 #include "rohm-bh1745/rohm-bh1745/bh1745.h"
00025 
00026 
00027 Serial pc(USBTX, USBRX);
00028 DigitalOut led1(LED1);
00029 DigitalIn hall_sensor(USER_BUTTON0);
00030 AnalogIn ml8511_input(A0);
00031 AnalogIn bd1020_input(A2);
00032 
00033 
00034 void bh1726_print_one_value(){
00035     bool error;
00036     uint16_t data[2];       //2 channels of 16-bit data
00037 
00038     error = bh1726_read_data(&data[0]);
00039     if (!error) {
00040         pc.printf("BH1726 ALS Data0[%4u], Data1[%4u]\n\r", data[0], data[1]);
00041         }
00042     else {
00043         pc.printf("\n\r");
00044         }
00045     return;
00046 }
00047 
00048 
00049 void bd1020_print_one_value(){
00050     float temperature;
00051 
00052     temperature = -(1000 * (bd1020_input * 3.3f) - 1546) / 8.2;
00053     pc.printf("BD1020 Temperature=%5.3f\r\n", temperature);
00054     return;
00055 }
00056 
00057 
00058 void rpr0521_print_one_value(){
00059     bool error;
00060     uint16_t data[3];
00061     
00062     error = rpr0521_read_data(&data[0]);
00063     if (!error) {
00064         pc.printf("RPR0521 PS[%4u], Als0[%4u], Als1[%4u]\n\r", data[0], data[1], data[2]);
00065         }
00066     else {
00067         pc.printf("\n\r");
00068         }
00069 }
00070 
00071 
00072 void bh1745_print_one_value(){
00073     bool error;
00074     uint16_t data[4];       //4 channels of 16-bit data
00075 
00076     error = bh1745_read_data(&data[0]);
00077     if (!error) {
00078         pc.printf("BH1745 Red[%4u], Green[%4u], Blue[%4u], Clear[%4u]\n\r", data[0], data[1], data[2], data[3]);
00079         }
00080     else {
00081         pc.printf("\n\r");
00082         }
00083 }
00084 
00085 
00086 void ml8511_print_one_value(){
00087     float uvraw, uv;
00088     #define MIN (0.300)
00089     #define MAX (0.900)
00090     #define UVONMAX 15
00091     uvraw = ml8511_input;
00092     uv = ( (UVONMAX/(MAX-MIN)) * (uvraw - MIN) );
00093     pc.printf("ML8511 UV Intensity %2.2fmW/cm^2 (raw[%2.3f])\r\n", uv, uvraw);
00094 }
00095 
00096 
00097 void bd7411_print_one_value(){
00098         if (!hall_sensor){
00099             pc.printf("BD7411 Magnet detected.\n\r");
00100             }
00101         else{
00102             pc.printf("BD7411 No magnet.\n\r");
00103             }
00104         led1 = hall_sensor;
00105 }
00106 
00107 
00108 // main() runs in its own thread in the OS
00109 // (note the calls to Thread::wait below for delays)
00110 int main() {
00111     pc.printf("\n\r");
00112     pc.printf("\n\r");
00113     pc.printf("\n\r");
00114     pc.printf("6 sensor test on Rohm tileshield with 6 tiles.\r\n");
00115     pc.printf("\n\r");
00116 
00117     I2CCommonBegin();
00118 
00119     bh1726_wait_until_found();
00120     pc.printf("BH1726 Sensor found.\n\r");
00121     bh1726_initial_setup();
00122 
00123     pc.printf("BD1020  Analog sensor assumed to be connected.\n\r");
00124 
00125     rpr0521_wait_until_found();
00126     pc.printf("RPR0521 Sensor found.\n\r");
00127     rpr0521_initial_setup();
00128 
00129     bh1745_wait_until_found();
00130     pc.printf("BH1745 Sensor found.\n\r");
00131     bh1745_initial_setup();
00132 
00133     pc.printf("ML8511  Analog sensor assumed to be connected.\n\r");
00134     pc.printf("BD7411 Digital sensor assumed to be connected.\n\r");
00135 
00136     pc.printf("\n\r");
00137     pc.printf("\n\r");
00138 
00139 
00140     while (true) {
00141         bh1726_print_one_value();
00142         bd1020_print_one_value();
00143         rpr0521_print_one_value();
00144         bh1745_print_one_value();
00145         ml8511_print_one_value();
00146         bd7411_print_one_value();
00147         
00148         Thread::wait(200);
00149     }
00150 }
00151