BH1792: Add examples for all languages

- Examples for C/C++/Python/Java/JavaScript
- Add build line for java BH1792_Example

Signed-off-by: Assam Boudjelthia <assam.boudjelthia@fi.rohmeurope.com>
This commit is contained in:
Assam Boudjelthia 2018-08-06 10:38:00 +03:00
parent 88284d96de
commit d4c274ab61
6 changed files with 341 additions and 0 deletions

79
examples/c++/bh1792.cxx Executable file
View File

@ -0,0 +1,79 @@
/*
* The MIT License (MIT)
*
* Author: Assam Boudjelthia
* Copyright (c) 2018 Rohm Semiconductor.
*
* 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:
*
* 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 <iostream>
#include <signal.h>
#include <sys/time.h>
#include "bh1792.hpp"
#include "upm_utilities.h"
bool isStopped;
upm::BH1792 dev;
void signal_int_handler(int signo)
{
if (signo == SIGINT)
isStopped = true;
}
void PrintData(void *args)
{
std::vector<std::vector<int>> fifo;
struct timespec now;
clock_gettime(CLOCK_MONOTONIC_RAW, &now);
try {
fifo = dev.GetFifoData();
std::cout << "\nHeart beat sensor FIFO data:" << std::endl;
for(int i = 0; i < 32; i++)
std::cout << i << ": off: " << fifo.at(i).at(0) << ", on: " <<
fifo.at(i).at(1) << std::endl;
std::cout << "timestamp " << now.tv_sec << " sec, " << now.tv_nsec <<
" nsec" << std::endl;
} catch (std::exception& e) {
std::cout << "Failed to read FIFO data" << std::endl;
throw;
}
}
int main(int argc, char **argv)
{
float measTime;
signal(SIGINT, signal_int_handler);
dev.SoftReset();
dev.EnableSyncMode(256, 32);
std::cout << "Installing ISR" << std::endl;
dev.InstallISR(MRAA_GPIO_EDGE_FALLING, 33, &PrintData, NULL);
measTime = dev.GetMeasurementTimeMS();
dev.StartMeasurement();
while(!isStopped) {
usleep(measTime * 1000);
}
return 0;
}

86
examples/c/bh1792.c Executable file
View File

@ -0,0 +1,86 @@
/*
* The MIT License (MIT)
*
* Author: Assam Boudjelthia
* Copyright (c) 2018 Rohm Semiconductor.
*
* 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:
*
* 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 <stdio.h>
#include <signal.h>
#include "bh1792.h"
#include <sys/time.h>
#include "upm_utilities.h"
bool isStopped;
#define SENSOR_ADDR 0x5b
#define I2C_BUS 0
void signal_int_handler(int signo)
{
if (signo == SIGINT)
isStopped = true;
}
void print_data(void *args)
{
bh1792_context dev = (bh1792_context)args;
uint16_t led_on[32], led_off[32];
struct timespec now;
clock_gettime(CLOCK_MONOTONIC_RAW, &now);
if(bh1792_get_fifo_data(dev, led_off, led_on) == UPM_SUCCESS) {
printf("\nFIFO data:\n");
for(int i = 0; i < 32; i++)
printf("%d: off: %d, on: %d\n", i, led_off[i], led_on[i]);
printf("timestamp %ld sec, %ld nsec\n", now.tv_sec, now.tv_nsec);
}
}
int main(int argc, char **argv)
{
float meas_time;
signal(SIGINT, signal_int_handler);
bh1792_context dev = bh1792_init(I2C_BUS, SENSOR_ADDR);
if (!dev) {
printf("bh1792_init() failed.\n");
return -1;
}
printf("Software reset\n");
bh1792_soft_reset(dev);
bh1792_enable_sync_mode(dev, 256, 32);
bh1792_install_isr(dev, MRAA_GPIO_EDGE_FALLING, 33, &print_data, dev);
bh1792_get_meas_time_ms(dev, &meas_time);
bh1792_start_measurement(dev);
printf("Heart beat sensor data\n");
while(!isStopped) {
usleep(meas_time * 1000);
}
bh1792_close(dev);
return 0;
}

View File

@ -0,0 +1,67 @@
/*
* The MIT License (MIT)
*
* Author: Assam Boudjelthia
* Copyright (c) 2018 Rohm Semiconductor.
*
* 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:
*
* 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.
*/
public class BH1792_Example {
public static void main(String[] args) throws InterruptedException {
int sampleCounter = 20;
float waitTime;
upm_bh1792.BH1792 bh1792;
bh1792 = new upm_bh1792.BH1792();
BH1792ISR callback = new BH1792ISR(bh1792);
bh1792.SoftReset();
bh1792.EnableSyncMode(256, 32);
bh1792.InstallISR(33, callback);
waitTime = bh1792.GetMeasurementTimeMS();
bh1792.StartMeasurement();
System.out.println("Heart rate sensor readings");
while (true) {
Thread.sleep((long) waitTime);
}
}
}
class BH1792ISR implements Runnable {
upm_bh1792.intVector2D result;
upm_bh1792.BH1792 bh1792;
public BH1792ISR(upm_bh1792.BH1792 bh1792) {
super();
this.bh1792 = bh1792;
}
public void run() {
result = this.bh1792.GetFifoData();
for(int i = 0; i < 32; i++)
System.out.println(i + ": off: " + result.get(i).get(0) +
", on: " + result.get(i).get(1));
System.out.println("Timestamp: " + System.nanoTime() + " nsec\n");
}
}

View File

@ -75,6 +75,7 @@ add_example(AM2315_Example am2315)
add_example(APA102_Example apa102)
add_example(Apds9002_Example apds9002)
add_example(BH1750_Example bh1750)
add_example(BH1792_Example bh1792)
add_example(BISS0001_Example biss0001)
add_example(BMA250E_Example bma250e)
add_example(BMC150_Example bmx055)

50
examples/javascript/bh1792.js Executable file
View File

@ -0,0 +1,50 @@
/*
* The MIT License (MIT)
*
* Author: Assam Boudjelthia
* Copyright (c) 2018 Rohm Semiconductor.
*
* 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:
*
* 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.
*/
var bh1792 = require("/usr/include/nodejs/lib/node_modules/jsupm_bh1792");
var bh1792_sensor = new bh1792.BH1792();
bh1792_sensor.SoftReset();
bh1792_sensor.EnableSyncMode(256, 32);
bh1792_sensor.StartMeasurement();
var waitTime = bh1792_sensor.GetMeasurementTimeMS();
var counter = 10;
console.log("Heart beat sensor readings: ");
var interval = setInterval(function() {
data = bh1792_sensor.GetGreenData();
console.log("off: " + data.get(0) + ", on: " + data.get(1) +
", timestamp: " + Date.now() + " msec");
counter--;
if (counter == 0) {
clearInterval(interval);
}
}, waitTime);
process.on("SIGINT", function() {
clearInterval(interval);
console.log("Exiting...");
process.exit(0);
});

58
examples/python/bh1792.py Executable file
View File

@ -0,0 +1,58 @@
# The MIT License (MIT)
#
# Author: Assam Boudjelthia
# Copyright (c) 2018 Rohm Semiconductor.
#
# 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:
#
# 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.
import time, datetime
import sys
import signal
import atexit
from upm import pyupm_bh1792
def main():
bh1792_sensor = pyupm_bh1792.BH1792()
bh1792_sensor.SoftReset()
bh1792_sensor.EnableSyncMode(256, 32)
bh1792_sensor.StartMeasurement()
# Prevent stack printing on CTRL^C
def SIGINTHandler(signum, frame):
raise SystemExit
def exitHandler():
print("Exiting")
sys.exit(0)
atexit.register(exitHandler)
signal.signal(signal.SIGINT, SIGINTHandler)
sampleCounter = 10
waitTime = bh1792_sensor.GetMeasurementTimeMS()
print("Heart beat sensor readings: ")
while sampleCounter > 0:
[off, on] = bh1792_sensor.GetGreenData()
print ("off: %d, on: %d, timestamp: %d msec" % (off, on, time.time() * 1000))
time.sleep(waitTime / 1000.0)
sampleCounter -= 1
if __name__ == '__main__':
main()