Private
Public Access
2
0

examples: make main loops finite to ensure proper cleanup

Signed-off-by: Alex Tereschenko <alext.mkrs@gmail.com>
Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
This commit is contained in:
Alex Tereschenko
2017-06-05 18:22:29 +02:00
committed by Brendan Le Foll
parent 32340f6819
commit c36e4add5a
15 changed files with 112 additions and 28 deletions

View File

@@ -1,5 +1,6 @@
/* /*
* Author: Nandkishor Sonar * Author: Nandkishor Sonar
* Contributors: Alex Tereschenko <alext.mkrs@gmail.com>
* Copyright (c) 2014 Intel Corporation. * Copyright (c) 2014 Intel Corporation.
* *
* Permission is hereby granted, free of charge, to any person obtaining * Permission is hereby granted, free of charge, to any person obtaining
@@ -23,30 +24,49 @@
*/ */
#include <unistd.h> #include <unistd.h>
//! [Interesting] #include <signal.h>
#include "mraa/aio.h" #include "mraa/aio.h"
int running = 0;
void
sig_handler(int signo)
{
if (signo == SIGINT) {
printf("closing down nicely\n");
running = -1;
}
}
//! [Interesting]
int int
main() main()
{ {
mraa_aio_context adc_a0; mraa_aio_context adc_a0;
uint16_t adc_value = 0; uint16_t adc_value = 0;
float adc_value_float = 0.0; float adc_value_float = 0.0;
mraa_result_t r = MRAA_SUCCESS;
adc_a0 = mraa_aio_init(0); adc_a0 = mraa_aio_init(0);
if (adc_a0 == NULL) { if (adc_a0 == NULL) {
return 1; return 1;
} }
for (;;) { signal(SIGINT, sig_handler);
while (running == 0) {
adc_value = mraa_aio_read(adc_a0); adc_value = mraa_aio_read(adc_a0);
adc_value_float = mraa_aio_read_float(adc_a0); adc_value_float = mraa_aio_read_float(adc_a0);
fprintf(stdout, "ADC A0 read %X - %d\n", adc_value, adc_value); fprintf(stdout, "ADC A0 read %X - %d\n", adc_value, adc_value);
fprintf(stdout, "ADC A0 read float - %.5f\n", adc_value_float); fprintf(stdout, "ADC A0 read float - %.5f (Ctrl+C to exit)\n", adc_value_float);
} }
mraa_aio_close(adc_a0); r = mraa_aio_close(adc_a0);
if (r != MRAA_SUCCESS) {
mraa_result_print(r);
}
return MRAA_SUCCESS; return r;
} }
//! [Interesting] //! [Interesting]

View File

@@ -1,5 +1,6 @@
/* /*
* Author: Brendan Le Foll <brendan.le.foll@intel.com> * Author: Brendan Le Foll <brendan.le.foll@intel.com>
* Contributors: Alex Tereschenko <alext.mkrs@gmail.com>
* Copyright (c) 2014 Intel Corporation. * Copyright (c) 2014 Intel Corporation.
* *
* Permission is hereby granted, free of charge, to any person obtaining * Permission is hereby granted, free of charge, to any person obtaining
@@ -25,9 +26,21 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <signal.h>
#include "mraa/gpio.h" #include "mraa/gpio.h"
int running = 0;
void
sig_handler(int signo)
{
if (signo == SIGINT) {
printf("closing down nicely\n");
running = -1;
}
}
int int
main(int argc, char** argv) main(int argc, char** argv)
{ {
@@ -35,6 +48,7 @@ main(int argc, char** argv)
mraa_gpio_context gpio, gpio_in = NULL; mraa_gpio_context gpio, gpio_in = NULL;
const char* board_name = mraa_get_platform_name(); const char* board_name = mraa_get_platform_name();
int ledstate = 0; int ledstate = 0;
mraa_result_t r = MRAA_SUCCESS;
switch (platform) { switch (platform) {
case MRAA_INTEL_GALILEO_GEN1: case MRAA_INTEL_GALILEO_GEN1:
@@ -53,7 +67,7 @@ main(int argc, char** argv)
gpio = mraa_gpio_init(13); gpio = mraa_gpio_init(13);
} }
fprintf(stdout, "Welcome to libmraa\n Version: %s\n Running on %s\n", mraa_get_version(), board_name); fprintf(stdout, "Welcome to libmraa\n Version: %s\n Running on %s (Ctrl+C to exit)\n", mraa_get_version(), board_name);
if (gpio == NULL) { if (gpio == NULL) {
@@ -73,8 +87,11 @@ main(int argc, char** argv)
mraa_gpio_dir(gpio, MRAA_GPIO_OUT); mraa_gpio_dir(gpio, MRAA_GPIO_OUT);
for (;;) { signal(SIGINT, sig_handler);
while (running == 0) {
if (gpio_in != NULL && mraa_gpio_read(gpio_in) == 0) { if (gpio_in != NULL && mraa_gpio_read(gpio_in) == 0) {
mraa_gpio_close(gpio_in);
return 0; return 0;
} }
ledstate = !ledstate; ledstate = !ledstate;
@@ -82,5 +99,10 @@ main(int argc, char** argv)
sleep(1); sleep(1);
} }
return 0; r = mraa_gpio_close(gpio);
if (r != MRAA_SUCCESS) {
mraa_result_print(r);
}
return r;
} }

View File

@@ -1,5 +1,6 @@
/* /*
* Author: Brendan Le Foll <brendan.le.foll@intel.com> * Author: Brendan Le Foll <brendan.le.foll@intel.com>
* Contributors: Alex Tereschenko <alex.mkrs@gmail.com>
* Copyright (c) 2014 Intel Corporation. * Copyright (c) 2014 Intel Corporation.
* *
* Permission is hereby granted, free of charge, to any person obtaining * Permission is hereby granted, free of charge, to any person obtaining
@@ -22,9 +23,22 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
//! [Interesting] #include <signal.h>
#include "mraa.hpp" #include "mraa.hpp"
int running = 0;
void
sig_handler(int signo)
{
if (signo == SIGINT) {
printf("closing down nicely\n");
running = -1;
}
}
//! [Interesting]
int int
main() main()
{ {
@@ -34,13 +48,16 @@ main()
a0 = new mraa::Aio(0); a0 = new mraa::Aio(0);
for (;;) { signal(SIGINT, sig_handler);
while (running == 0) {
adc_value = a0->read(); adc_value = a0->read();
adc_value_float = a0->readFloat(); adc_value_float = a0->readFloat();
fprintf(stdout, "ADC A0 read %X - %d\n", adc_value, adc_value); fprintf(stdout, "ADC A0 read %X - %d\n", adc_value, adc_value);
fprintf(stdout, "ADC A0 read float - %.5f\n", adc_value_float); fprintf(stdout, "ADC A0 read float - %.5f (Ctrl+C to exit)\n", adc_value_float);
} }
delete a0;
return MRAA_SUCCESS; return MRAA_SUCCESS;
} }
//! [Interesting] //! [Interesting]

View File

@@ -1,5 +1,6 @@
/* /*
* Author: Brendan Le Foll * Author: Brendan Le Foll
* Contributors: Alex Tereschenko <alext.mkrs@gmail.com>
* Copyright (c) 2015 Intel Corporation. * Copyright (c) 2015 Intel Corporation.
* *
* Permission is hereby granted, free of charge, to any person obtaining * Permission is hereby granted, free of charge, to any person obtaining
@@ -44,7 +45,8 @@ main()
x->isr(mraa::EDGE_BOTH, &interrupt, NULL); x->isr(mraa::EDGE_BOTH, &interrupt, NULL);
for (;;) { int i = 100;
for (; i > 0; --i) {
if (counter != oldcounter) { if (counter != oldcounter) {
fprintf(stdout, "timeout counter == %d\n", counter); fprintf(stdout, "timeout counter == %d\n", counter);
oldcounter = counter; oldcounter = counter;
@@ -54,5 +56,5 @@ main()
} }
delete x; delete x;
return EXIT_SUCCESS; return MRAA_SUCCESS;
} }

View File

@@ -1,5 +1,6 @@
/* /*
* Author: Thomas Ingleby <thomas.c.ingleby@intel.com> * Author: Thomas Ingleby <thomas.c.ingleby@intel.com>
* Contributors: Alex Tereschenko <alext.mkrs@gmail.com>
* Copyright (c) 2014 Intel Corporation. * Copyright (c) 2014 Intel Corporation.
* *
* Permission is hereby granted, free of charge, to any person obtaining * Permission is hereby granted, free of charge, to any person obtaining
@@ -24,14 +25,28 @@
#include "stdio.h" #include "stdio.h"
#include "unistd.h" #include "unistd.h"
#include <signal.h>
#include "mraa.h" #include "mraa.h"
int running = 0;
void
sig_handler(int signo)
{
if (signo == SIGINT) {
printf("closing down nicely\n");
running = -1;
}
}
int int
main(int argc, char** argv) main(int argc, char** argv)
{ {
mraa_result_t r = MRAA_SUCCESS;
mraa_init(); mraa_init();
fprintf(stdout, "MRAA Version: %s\nStarting Read on IO6\n", mraa_get_version()); fprintf(stdout, "MRAA Version: %s\nStarting Read on IO6 (Ctrl+C to exit)\n", mraa_get_version());
//! [Interesting] //! [Interesting]
mraa_gpio_context gpio; mraa_gpio_context gpio;
@@ -40,13 +55,18 @@ main(int argc, char** argv)
mraa_gpio_dir(gpio, MRAA_GPIO_IN); mraa_gpio_dir(gpio, MRAA_GPIO_IN);
for (;;) { signal(SIGINT, sig_handler);
while (running == 0) {
fprintf(stdout, "Gpio is %d\n", mraa_gpio_read(gpio)); fprintf(stdout, "Gpio is %d\n", mraa_gpio_read(gpio));
sleep(1); sleep(1);
} }
mraa_gpio_close(gpio); r = mraa_gpio_close(gpio);
//! [Interesting] //! [Interesting]
if (r != MRAA_SUCCESS) {
mraa_result_print(r);
}
return 0; return r;
} }

View File

@@ -1,5 +1,6 @@
/* /*
* Author: Brendan Le Foll * Author: Brendan Le Foll
* Contributors: Alex Tereschenko <alext.mkrs@gmail.com>
* Copyright (c) 2014 Intel Corporation. * Copyright (c) 2014 Intel Corporation.
* *
* Permission is hereby granted, free of charge, to any person obtaining * Permission is hereby granted, free of charge, to any person obtaining
@@ -52,7 +53,8 @@ main()
mraa_gpio_isr(x, edge, &interrupt, NULL); mraa_gpio_isr(x, edge, &interrupt, NULL);
for (;;) { int i = 100;
for (; i > 0; --i) {
if (counter != oldcounter) { if (counter != oldcounter) {
fprintf(stdout, "timeout counter == %d\n", counter); fprintf(stdout, "timeout counter == %d\n", counter);
oldcounter = counter; oldcounter = counter;

View File

@@ -44,11 +44,12 @@ public class AioA0 {
public static void main(String[] args) { public static void main(String[] args) {
Aio a0 = new Aio(0); Aio a0 = new Aio(0);
while (true) { for (int i = 100; i > 0; --i) {
int adc_value = a0.read(); int adc_value = a0.read();
float adc_value_float = a0.readFloat(); float adc_value_float = a0.readFloat();
System.out.println(String.format("ADC A0 read %X - %d", adc_value, adc_value)); System.out.println(String.format("ADC A0 read %X - %d", adc_value, adc_value));
System.out.println(String.format("ADC A0 read %.5f", adc_value_float)); System.out.println(String.format("ADC A0 read %.5f", adc_value_float));
Thread.sleep(500);
} }
} }

View File

@@ -61,7 +61,7 @@ public class BlinkIO {
System.exit(1); System.exit(1);
} }
while (true) { for (int i = 100; i > 0; --i) {
gpio.write(1); gpio.write(1);
Thread.sleep(1000); Thread.sleep(1000);
gpio.write(0); gpio.write(0);

View File

@@ -64,7 +64,7 @@ public class BlinkOnboard {
} }
boolean state = false; boolean state = false;
while (true) { for (int i = 100; i > 0; --i) {
if (gpio_in != null && gpio_in.read() == 0) { if (gpio_in != null && gpio_in.read() == 0) {
return; return;
} }

View File

@@ -44,7 +44,7 @@ public class CyclePwm3 {
pwm.enable(true); pwm.enable(true);
float value = 0; float value = 0;
while (true) { for (int i = 100; i > 0; --i) {
value += 0.01; value += 0.01;
pwm.write(value); pwm.write(value);
Thread.sleep(50); Thread.sleep(50);

View File

@@ -50,8 +50,8 @@ public class FTDITest {
/* Blink FTDI board LED */ /* Blink FTDI board LED */
Gpio led = new Gpio(514); Gpio led = new Gpio(514);
led.dir(Dir.DIR_OUT); led.dir(Dir.DIR_OUT);
for (int i = 0;; i = (i + 1) % 2) { for (int i = 100; i > 0; --i) {
led.write(i); led.write(i % 2);
Thread.sleep(500); Thread.sleep(500);
} }
} else { } else {

View File

@@ -50,7 +50,7 @@ public class GpioMmapped {
gpio.useMmap(true); gpio.useMmap(true);
while (true) { for (int i = 1000; i > 0; --i) {
gpio.write(1); gpio.write(1);
Thread.sleep(50); Thread.sleep(50);
gpio.write(0); gpio.write(0);

View File

@@ -46,7 +46,7 @@ public class GpioRead6 {
gpio.dir(Dir.DIR_IN); gpio.dir(Dir.DIR_IN);
while (true) { for (int i = 100; i > 0; --i) {
System.out.format("Gpio is %d\n", gpio.read()); System.out.format("Gpio is %d\n", gpio.read());
Thread.sleep(1000); Thread.sleep(1000);
} }

View File

@@ -110,7 +110,7 @@ public class I2cCompass {
conf_buf[1] = HMC5883L_CONT_MODE; conf_buf[1] = HMC5883L_CONT_MODE;
i2c.write(conf_buf); i2c.write(conf_buf);
while (true) { for (int i = 100; i > 0; --i) {
i2c.address(HMC5883L_I2C_ADDR); i2c.address(HMC5883L_I2C_ADDR);
i2c.writeByte(HMC5883L_DATA_REG); i2c.writeByte(HMC5883L_DATA_REG);

View File

@@ -43,7 +43,7 @@ public class SpiMCP4261 {
System.out.println("Hello, SPI initialised"); System.out.println("Hello, SPI initialised");
byte data[] = {0x00, 100}; byte data[] = {0x00, 100};
while (true) { for (int cnt = 100; cnt > 0; --cnt) {
for (int i = 90; i < 130; i++) { for (int i = 90; i < 130; i++) {
data[1] = (byte) i; data[1] = (byte) i;
byte[] recv = spi.write(data); byte[] recv = spi.write(data);