HW code
HW code
int main()
{
mraa_gpio_context d_pin = NULL;
d_pin = mraa_gpio_init(13);
if (d_pin == NULL) {
fprintf(stderr, "MRAA couldn't initialize GPIO, exiting");
return MRAA_ERROR_UNSPECIFIED;
}
return MRAA_SUCCESS;
}
Code:
#include "mraa.h"//p3
#include <stdio.h>
#include <unistd.h>
#define BUTTON_PIN 2
#define LED_PIN 13
int main()
{
mraa_init();
mraa_gpio_context button = mraa_gpio_init(BUTTON_PIN);
mraa_gpio_dir(button, MRAA_GPIO_IN);
for(;;) {
if (mraa_gpio_read(button)) mraa_gpio_write(led, 1);
else mraa_gpio_write(led, 0);
usleep(100000);
}
mraa_gpio_close(led);
mraa_gpio_close(button);
mraa_deinit();
return MRAA_SUCCESS;
}
Code:
#include <buzzer.hpp> //p4
#include <stdio.h>
#include <unistd.h>
int main() {
int chord[] = {DO, RE, MI, FA, SOL, LA, SI, DO, SI};
upm::Buzzer* sound = new upm::Buzzer(5);
printf("Volume = %f\n", sound->getVolume());
sound->setVolume(0.1);
printf("Volume = %f\n", sound->getVolume());
fflush(stdout);
printf("\nPlaying notes, pausing for 0.1 seconds between notes...\n");
fflush(stdout);
for (int chord_ind = 0; chord_ind < 7; chord_ind++) {
printf(" %d\n", sound->playSound(chord[chord_ind], 500000));
usleep(100000);
}
printf("Exiting, bbye!\n");
delete sound;
}
Code:
#include "mraa/aio.h" //for mraa_aio_read() p5
#include <math.h> // for math functions
#include <stdio.h> // for printf()
#include <unistd.h> //for sleep()
int main(){
mraa_aio_context adc_a0;
uint16_t adc_value = 0;
const int B=4275; // B value of the thermistor
const int R0 = 100000; // R0 = 100k
adc_a0 = mraa_aio_init(0);
if (adc_a0 == NULL) {
return 1;
}
for (int i=5; i>0;i--) {
adc_value = mraa_aio_read(adc_a0); //Max value @ 5V = 1024
printf("ADC A0 read value : %d\n", adc_value);
float R = 1023.0/((float)adc_value)-1.0;
R = 100000.0*R;
float temperature=1.0/(log(R/100000.0)/B+1/298.15)-273.15; //convert to temperature
as per datasheet
void touchISR(void*)
{
count--;
printf("\nHello World from ISR, will exit after %d touch events", count);
fflush(stdout);
}
int main()
{
upm::TTP223* touch = new upm::TTP223(4);
touch->installISR(mraa::EDGE_FALLING, &touchISR, NULL);
while(count>0);
printf("\nExiting .. Bbye!");
delete touch;
return MRAA_SUCCESS;
}
Code:
#include "grove.h" //for light->name(), light->value() p7
int main()
{
upm::GroveLight* light = new upm::GroveLight(0);
for (int i=5;i>0;i--) {
printf(" Light value is %f which is roughly %d lux \n", light->raw_value(),
light->value());
fflush(stdout);
sleep(1);
}
// Delete the light sensor object
printf("Exiting .. bbye!");
delete light;
}
Code:
#include "mic.h" // for p8
#include <stdio.h> // for printf()
#include <unistd.h> //for sleep()
#include <signal.h>
#include <sys/time.h>
int is_running = 1;
uint16_t buffer [128]; //define buffer to store captures values
thresholdContext ctx;
ctx.averageReading = 0;
ctx.runningAverage = 0;
ctx.averagedOver = 2;
while (is_running) {
int len = mic->getSampledWindow (2, 128, buffer);
if (len) {
int thresh = mic->findThreshold (&ctx, 30, buffer, len);
mic->printGraph(&ctx);
if (thresh) {
// do something ....
}
}
}
printf ("exiting application\n");
delete mic;
return 0;
}
Code:
int main()
{
upm::GroveRotary* knob = new upm::GroveRotary(1);
while( 1 ) {
float abs_value = knob->abs_value(); // Absolute raw value
float abs_deg = knob->abs_deg(); // Absolute degrees
float abs_rad = knob->abs_rad(); // Absolute radians
float rel_value = knob->rel_value(); // Relative raw value
float rel_deg = knob->rel_deg(); // Relative degrees
float rel_rad = knob->rel_rad(); // Relative radians
printf("Absolute: %4d raw %5.2f deg = %3.2f rad Relative: %4d raw %5.2f deg
%3.2f rad\n",
(int16_t)abs_value, abs_deg, abs_rad, (int16_t)rel_value, rel_deg,
rel_rad);
sleep(1);
}
delete knob;
return MRAA_SUCCESS;
}