KB SEM6 HardwarePractical Codes
KB SEM6 HardwarePractical Codes
cpp
/* MRAA library includes */
#include "mraa.h"
#include <stdio.h>
int main()
{
// create a GPIO object from MRAA using it
mraa_gpio_context d_pin = NULL;
d_pin = mraa_gpio_init(13);
if (d_pin == NULL)
{ // error check
fprintf(stderr, "MRAA couldn't initialize GPIO, exiting");
return MRAA_ERROR_UNSPECIFIED;
}
// configure the GPIO pin as output
if (mraa_gpio_dir(d_pin, MRAA_GPIO_OUT) != MRAA_SUCCESS)
{
fprintf(stderr, "Can't set digital pin as output, exiting");
return MRAA_ERROR_UNSPECIFIED;
};
// loop to toggle on-board every second for 10 times
for (int i = 10; i > 0; i--)
{
printf("LED OFF\n");
mraa_gpio_write(d_pin, 0); // turn off LED
sleep(2); // wait 1 second
printf("LED ON\n");
mraa_gpio_write(d_pin, 1); // turn on LED
sleep(1); // wait 1 second
}
return MRAA_SUCCESS;
}
03.cpp
/* UPM library includes */
#include "grove.h" // for button->value()
/* MRAA library includes */
#include "mraa.h"
#include <stdio.h>
int main()
{
// create a GPIO object from MRAA using it
mraa_gpio_context d_pin = NULL;
d_pin = mraa_gpio_init(13);
// Create the button object using GPIO pin D4
upm::GroveButton *button = new upm::GroveButton(3);
if (d_pin == NULL)
{ // error check
fprintf(stderr, "MRAA couldn't initialize GPIO, exiting");
return MRAA_ERROR_UNSPECIFIED;
}
// configure the GPIO pin as output
if (mraa_gpio_dir(d_pin, MRAA_GPIO_OUT) != MRAA_SUCCESS)
{
fprintf(stderr, "Can't set digital pin as output, exiting");
return MRAA_ERROR_UNSPECIFIED;
};
// Run Code for 1 min
for (int i = 60; i > 0; i--)
{
button_val = button->value();
if (button_val)
{
printf("LED ON\n");
mraa_gpio_write(d_pin, 1); // turn on LED
}
else
{
printf("LED OFF\n");
mraa_gpio_write(d_pin, 0); // turn off LED
}
sleep(1);
}
return MRAA_SUCCESS;
}
04.cpp
/* UPM library includes */
#include <buzzer.hpp>
#include <stdio.h> // for printf()
#include <unistd.h> //for sleep()
int main()
{
int chord[] = {DO, RE, MI, FA, SOL, LA, SI, DO, SI};
// create Buzzer instance
upm::Buzzer *sound = new upm::Buzzer(5);
printf("Volume = %f\n", sound->getVolume());
sound->setVolume(30.1);
printf("Volume = %f\n", sound->getVolume());
fflush(stdout);
// play sound (DO, RE, MI, etc...), pausing for 0.1 seconds between notes
printf("\nPlaying notes, pausing for 0.1 seconds between notes...\n");
fflush(stdout);
for (int chord_ind = 0; chord_ind < 13; chord_ind++)
{
// play each note for one second
printf(" %d\n", sound->playSound(chord[chord_ind], 5000000));
usleep(100000);
}
printf("Exiting, bbye!\n");
delete sound;
}
05.cpp
#include "mraa/aio.h" //for mraa_aio_read()
#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
// create object of analog input A0 class using mraa lib
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 ;
printf("Temperature value : %.2f Degree Celsius\n", temperature);
sleep(1);
}
mraa_aio_close(adc_a0);
printf("Exiting .. Bbye!");
return MRAA_SUCCESS;
}
06.cpp
/* UPM library includes */
#include "ttp223.h" // for button->value()
/* Standard IO includes */
#include <stdio.h> // for printf()
#include <unistd.h> //for sleep()
void touchISR(void *);
int count = 5;
void touchISR(void *)
{
count--;
printf("\nHello World from ISR, will exit after %d touch events", count);
fflush(stdout);
}
int main()
{
// Create the button object using GPIO pin 4
upm::TTP223 *touch = new upm::TTP223(4);
// Read the input and print, waiting one second between readings
touch->installISR(mraa::EDGE_FALLING, &touchISR, NULL);
printf("\nWelcome, waiting for touch event.\nWill exit after 5 events");
fflush(stdout);
while (count > 0)
;
printf("\nExiting .. Bbye!");
delete touch; // Delete the button object
}
07.cpp
/* UPM library includes */
#include "grove.h" //for light->name(), light->value()
/* Standard IO includes */
#include <stdio.h> // for printf()
#include <unistd.h> //for sleep()
int main()
{
// Create the light sensor object using AIO pin 0
upm::GroveLight *light = new upm::GroveLight(0);
// Read the input and print both the raw value and a rough lux value,
// waiting one second between readings
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;
}
08.cpp
/* UPM library includes */
#include "mic.h" // for
/* Standard IO includes */
#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
upm::Microphone *mic = NULL; // create microphone object
void sig_handler(int signo)
{
printf("got signal\n");
if (signo == SIGINT)
{
is_running = 0;
}
}
int main(int argc, char **argv)
{
// Attach microphone to analog port A0
mic = new upm::Microphone(0);
if (signal(SIGINT, sig_handler) == SIG_ERR)
printf("\ncan't catch SIGINT\n");
thresholdContext ctx;
ctx.averageReading = 0;
ctx.runningAverage = 0;
ctx.averagedOver = 2;
// Infinite loop, ends when program is cancelled
// Repeatedly, take a sample every 2 microseconds;
// find the average of 128 samples; and
// print a running graph of the averages
while (is_running)
{
int len = mic->getSampledWindow(2, 128, buffer);
if (len)
{
int thresh = mic->findThreshold(&ctx, 30, buffer, len);
mic->printGraph(&ctx);
}
}
printf("exiting application\n");
delete mic;
return 0;
}
09.cpp
/* UPM library includes */
#include "jhd1313m1.h"
/* Standard IO includes */
#include <stdio.h> // for printf()
#include <unistd.h> //for sleep()
int main(void)
{
// 0x62 RGB_ADDRESS, 0x3E LCD_ADDRESS
upm::Jhd1313m1 *lcd;
lcd = new upm::Jhd1313m1(0, 0x3E, 0x62); // Create lcd instance
// arguments: I2C addresses of LCD controller and LED backlight controller
printf("Display text on LCD\n");
lcd->setCursor(0, 0); // bring cursor to top left corner
lcd->write("CSE 2026!!!! "); // print text
lcd->setCursor(1, 2); // bring cursor to second row
lcd->write(" This Practical is performed by CSE 2026... !"); // print text
printf("Sleeping for 5 seconds\n");
sleep(5);
printf("Starting Color loop...\n");
// Run loop for toggling backlight color between Red->Green->Blue x 5 times
for (int i = 10; i > 0; i--)
{
lcd->setColor(255, 0, 0); // set backlight color to Red
sleep(1);
lcd->setColor(0, 255, 0); // set backlight color to Green
sleep(1);
lcd->setColor(0, 0, 255); // set backlight color to Blue
sleep(1);
}
printf("Exiting .. bbye!\n");
delete lcd; // free up memory.
return 0;
}
10.cpp
/* UPM library includes */
#include "grove.h" // for button->value()
/* Standard IO includes */
#include <stdio.h> // for printf()
#include <unistd.h> //for sleep()
int main()
{
// Instantiate a rotary sensor on analog pin A1
upm::GroveRotary *knob = new upm::GroveRotary(1);
// Read the input and print, waiting one second between readings for 1 min
for (int i = 60; i >= 0; i++)
{
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); // Sleep for 1s
}
// Delete the button object
delete knob;
}