// The main function for the example program
int main() {
  // Handles ctrl-c or other orderly exits
  signal(SIGINT, exit_handler);

  // check that we are running on Galileo or Edison
  mraa_platform_t platform = mraa_get_platform_type();
  if ((platform != MRAA_INTEL_GALILEO_GEN1) &&
    (platform != MRAA_INTEL_GALILEO_GEN2) &&
    (platform != MRAA_INTEL_EDISON_FAB_C)) {
    std::cerr << "ERROR: Unsupported platform" << std::endl;
    return MRAA_ERROR_INVALID_PLATFORM;
  }

  // create and initialize UPM devices
  devices.init();

  // start worker thread for device communication
  std::thread t1(runner, std::ref(devices));

  // define web server & routes
  crow::SimpleApp app;

  CROW_ROUTE(app, "/")
  ([]() {
    std::stringstream text;
    text << index_html;
    return text.str();
  });

  CROW_ROUTE(app, "/data.json")
  ([](const crow::request& req) {
    crow::json::wvalue result;
    for (int i = 0; i < 360; i++) {
      result[i] = degrees[i];
    }

    return crow::response{result};
  });

  CROW_ROUTE(app, "/styles.css")
  ([]() {
    std::stringstream text;
    text << styles_css;
    return text.str();
  });


  // start web server
  app.port(3000).multithreaded().run();

  // wait forever for the thread to exit
  t1.join();

  return MRAA_SUCCESS;
}
Beispiel #2
0
int
mraa_get_platform_combined_type()
{
    int type = mraa_get_platform_type();
    int sub_type = mraa_has_sub_platform() ? plat->sub_platform->platform_type : MRAA_UNKNOWN_PLATFORM;
    return type | (sub_type << 8);
}
Beispiel #3
0
int
main(int argc, char **argv)
{
    mraa_platform_t platform = mraa_get_platform_type();
    mraa_gpio_context gpio;
    char board_name[] = "Some weird devboard that isn't recognised...";
    int ledstate = 0;

    switch (platform) {
        case MRAA_INTEL_GALILEO_GEN1:
            strcpy(board_name, "Intel Galileo Gen1");
            gpio = mraa_gpio_init_raw(3);
            break;
        case MRAA_INTEL_GALILEO_GEN2:
            strcpy(board_name, "Intel Galileo Gen2");
        default:
            gpio = mraa_gpio_init(13);
    }

    fprintf(stdout, "Welcome to libmraa\n Version: %s\n Running on %s\n",
        mraa_get_version(), board_name);

    mraa_gpio_dir(gpio, MRAA_GPIO_OUT);
    for (;;) {
        ledstate = !ledstate;
        mraa_gpio_write(gpio, !ledstate);
        sleep(1);
    }

    return 0;
}
Beispiel #4
0
int
main(int argc, char** argv)
{
    mraa_platform_t platform = mraa_get_platform_type();
    mraa_gpio_context gpio, gpio_in = NULL;
    const char* board_name = mraa_get_platform_name();
    int ledstate = 0;

    switch (platform) {
        case MRAA_INTEL_GALILEO_GEN1:
            gpio = mraa_gpio_init_raw(3);
            break;
        case MRAA_INTEL_MINNOWBOARD_MAX:
            // there is no onboard LED that we can flash on the minnowboard max
            // but on the calamari lure pin 21 is an LED. If you don't have the
            // lure put an LED on pin 21
            gpio = mraa_gpio_init(21);
            break;
        case MRAA_INTEL_JOULE_EXPANSION:
            gpio = mraa_gpio_init(101);
            break;
        default:
            gpio = mraa_gpio_init(13);
    }

    fprintf(stdout, "Welcome to libmraa\n Version: %s\n Running on %s\n", mraa_get_version(), board_name);


    if (gpio == NULL) {
        fprintf(stdout, "Could not initilaize gpio\n");
        return 1;
    }

    // on platforms with physical button use gpio_in
    if (platform == MRAA_INTEL_MINNOWBOARD_MAX) {
        gpio_in = mraa_gpio_init(14);
        if (gpio_in != NULL) {
            mraa_gpio_dir(gpio_in, MRAA_GPIO_IN);
            // S1 on minnowboardmax's calamari lure maps to pin 14, SW1 != S1
            fprintf(stdout, "Press and hold S1 to stop, Press SW1 to shutdown!\n");
        }
    }

    mraa_gpio_dir(gpio, MRAA_GPIO_OUT);

    for (;;) {
        if (gpio_in != NULL && mraa_gpio_read(gpio_in) == 0) {
            return 0;
        }
        ledstate = !ledstate;
        mraa_gpio_write(gpio, !ledstate);
        sleep(1);
    }

    return 0;
}
Beispiel #5
0
void startMRAA( void ) {

//	printf( "\n  I/O is enabled\n" );
	mraa_init();

	printf( "  MRAA library Version: %s\n", mraa_get_version() );

	mraa_platform_t platformType = mraa_get_platform_type();
    printf( "  Platform type: %d\n", platformType );
    char *platformName = mraa_get_platform_name();
    printf( "  Platform name: %s\n", platformName );
}
Beispiel #6
0
int
main(int argc, char **argv)
{
    mraa_platform_t platform = mraa_get_platform_type();
    mraa_gpio_context gpio, gpio_in = NULL;
    char* board_name = mraa_get_platform_name();
    int ledstate = 0;

    switch (platform) {
        case MRAA_INTEL_GALILEO_GEN1:
            gpio = mraa_gpio_init_raw(3);
            break;
        case MRAA_INTEL_MINNOWBOARD_MAX:
            gpio = mraa_gpio_init(21);
            break;
        default:
            gpio = mraa_gpio_init(13);
    }

    fprintf(stdout, "Welcome to libmraa\n Version: %s\n Running on %s\n",
        mraa_get_version(), board_name);


    if (gpio == NULL) {
        fprintf(stdout, "Could not initilaize gpio\n");
        return 1;
    }

    // on platforms with physical button use gpio_in
    if (platform == MRAA_INTEL_MINNOWBOARD_MAX) {
        gpio_in = mraa_gpio_init(14);
	if (gpio_in != NULL) {
            mraa_gpio_dir(gpio_in, MRAA_GPIO_IN);
            fprintf(stdout, "Press and hold S1 to stop\n");
        }
    }

    mraa_gpio_dir(gpio, MRAA_GPIO_OUT);

    for (;;) {
        if (gpio_in != NULL && mraa_gpio_read(gpio_in) == 0) {
            return 0;
        }
        ledstate = !ledstate;
        mraa_gpio_write(gpio, !ledstate);
        sleep(1);
    }

    return 0;
}
Beispiel #7
0
bool mkInterface::setupGPIO( int pinNumber ) {

    on = 0;

    iopin = pinNumber;
    gpio = new mraa::Gpio( iopin, true, false );

    fprintf(stdout, "\nHello mraa\n  Version: %s", mraa_get_version());

    mraa_platform_t platform = mraa_get_platform_type();
    fprintf(stdout, "\n  Platform type: %d\n", platform );

    response = gpio->dir(mraa::DIR_OUT);
    if (response != MRAA_SUCCESS) {
        mraa_result_print((mraa_result_t) response);
        return false;
    }
   	return true;
}
Beispiel #8
0
/**
 * Whilst the actual mraa init function is now called imraa_init, it's only
 * callable externally if IMRAA is enabled
 */
mraa_result_t
imraa_init()
{
    if (plat != NULL) {
        return MRAA_SUCCESS;
    }

    uid_t proc_euid = geteuid();
    struct passwd* proc_user = getpwuid(proc_euid);

#ifdef DEBUG
    setlogmask(LOG_UPTO(LOG_DEBUG));
#else
    setlogmask(LOG_UPTO(LOG_NOTICE));
#endif

    openlog("libmraa", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);
    syslog(LOG_NOTICE, "libmraa version %s initialised by user '%s' with EUID %d",
           mraa_get_version(), (proc_user != NULL) ? proc_user->pw_name : "<unknown>", proc_euid);

    mraa_platform_t platform_type;
#if defined(X86PLAT)
    // Use runtime x86 platform detection
    platform_type = mraa_x86_platform();
#elif defined(ARMPLAT)
    // Use runtime ARM platform detection
    platform_type = mraa_arm_platform();
#elif defined(MOCKPLAT)
    // Use mock platform
    platform_type = mraa_mock_platform();
#else
#error mraa_ARCH NOTHING
#endif

    if (plat != NULL) {
        plat->platform_type = platform_type;
    } else {
        platform_name = NULL;
    }

    // Create null base platform if one doesn't already exist
    if (plat == NULL) {
        plat = (mraa_board_t*) calloc(1, sizeof(mraa_board_t));
        if (plat != NULL) {
            plat->platform_type = MRAA_NULL_PLATFORM;
            plat->platform_name = "Unknown platform";
        }
    }

#if defined(USBPLAT)
    // Now detect sub platform, note this is not an else since we could be in
    // an error case and fall through to MRAA_ERROR_PLATFORM_NOT_INITIALISED
    if (plat != NULL) {
        mraa_platform_t usb_platform_type = mraa_usb_platform_extender(plat);
        // if we have no known platform just replace usb platform with platform
        if (plat->platform_type == MRAA_UNKNOWN_PLATFORM && usb_platform_type != MRAA_UNKNOWN_PLATFORM) {
            plat->platform_type = usb_platform_type;
        }
    }
    if (plat == NULL) {
        printf("mraa: FATAL error, failed to initialise platform\n");
        return MRAA_ERROR_PLATFORM_NOT_INITIALISED;
    }
#endif

#if defined(IMRAA)
    const char* subplatform_lockfile = "/tmp/imraa.lock";
    mraa_add_from_lockfile(subplatform_lockfile);
#endif

    // Look for IIO devices
    mraa_iio_detect();

    if (plat != NULL) {
        int length = strlen(plat->platform_name) + 1;
        if (mraa_has_sub_platform()) {
            // Account for ' + ' chars
            length += strlen(plat->sub_platform->platform_name) + 3;
        }
        platform_name = calloc(length, sizeof(char));
        if (mraa_has_sub_platform()) {
            snprintf(platform_name, length, "%s + %s", plat->platform_name, plat->sub_platform->platform_name);
        } else {
            strncpy(platform_name, plat->platform_name, length);
        }
    }

    lang_func = (mraa_lang_func_t*) calloc(1, sizeof(mraa_lang_func_t));
    if (lang_func == NULL) {
        return MRAA_ERROR_NO_RESOURCES;
    }

    syslog(LOG_NOTICE, "libmraa initialised for platform '%s' of type %d", mraa_get_platform_name(), mraa_get_platform_type());
    return MRAA_SUCCESS;
}
int main(int argc, char** argv)
{
    mraa_platform_t platform;                /* Type of platform we are on */
    mraa_gpio_context gpio_led = NULL;       /* LED GPIO descriptor */
    mraa_gpio_context gpio_button = NULL;    /* BUTTON GPIO descriptor */
    mraa_aio_context adc_a0 = NULL;          /* Analog-Digital descriptor */
    uint32_t adc_val;                       /* ADC value */
    float delay;                             /* Delay between blinks */
    int ledstate = 0;                        /* LED state, 0=off, 1=on */


    /*
     * Print banner.
     */
     platform = mraa_get_platform_type();
     fprintf(stdout, "MRAA C Demonstration\n");
     fprintf(stdout, "LIBMRAA Version: %s\n", mraa_get_version());
     fprintf(stdout, "Board: %s\n", mraa_get_platform_name());

     /*
      * Check that we are running on the correct board
      */
     platform = mraa_get_platform_type();
     if (platform != MRAA_INTEL_GALILEO_GEN2)
        {
        fprintf(stderr, "ERROR: This program is for a Intel Galileo Gen 2 Board\n");
        exit (-1);
        }

    /*
     * Initialize the LED gpio pin and set it as an output
     */
    gpio_led = mraa_gpio_init(LED);
    if (gpio_led == NULL)
        {
        fprintf(stderr, "Could not initialize LED gpio\n");
        exit (-1);
        }
    mraa_gpio_dir(gpio_led, MRAA_GPIO_OUT);

    /*
     * Initialize the Button gpio pin and set it as an input
     */
     gpio_button = mraa_gpio_init(BUTTON);
     if (gpio_button == NULL)
         {
         fprintf(stderr, "Could not initialize BUTTON gpio\n");
         }
    mraa_gpio_dir(gpio_button, MRAA_GPIO_IN);

    /*
     * Initialize the Analog-Digital converter channel 0
     */

    adc_a0 = mraa_aio_init(0);
    if (adc_a0 == NULL)
        {
        fprintf(stderr, "Failed to initalize ADC channel A0\n");
        exit (-1);
        }

    printf("Vary the rate of blink by turning the poteniometer on A0\n");
    printf("Exit the program by pressing the button on D%d\n\n", BUTTON);

    /*
     * Run the loop until the BUTTON is held down.
     * Each time through the loop, read the potentiometer on A0 and
     * adjust the blink rate.
     */
    while (1)
        {
        if (mraa_gpio_read(gpio_button) == 1)
            break;

        if (adc_a0 != NULL)
            {
            adc_val = mraa_aio_read(adc_a0);
            /* fprintf(stdout, "ADC A0 = %d\n", adc_val); */
            if (adc_val != 0)
                delay = 1000000 * ((float)adc_val/1024);
            else
                delay = 1000000 * (1/1024);
            }

        ledstate = !ledstate;

        mraa_gpio_write(gpio_led, ledstate);

        usleep((long)delay);
        }

    /*
     * Clean up and exit
     */
    (void)mraa_gpio_close(gpio_led);
    (void)mraa_gpio_close(gpio_button);
    (void)mraa_aio_close(adc_a0);

    exit(0);
}
int main()
{
	// check that we are running on Galileo or Edison
	mraa_platform_t platform = mraa_get_platform_type();
	if ((platform != MRAA_INTEL_EDISON_FAB_C))
	{
		std::cerr << "Unsupported platform, exiting" << std::endl;
		return MRAA_ERROR_INVALID_PLATFORM;
	}

	// temperature sensor connected to A0 (analog in)
	upm::GroveLoudness* loudness = new upm::GroveLoudness(0);
	// LCD connected to the default I2C bus
	upm::Jhd1313m1* lcd = new upm::Jhd1313m1(0);

	//word count
	int word_count = 0;
	const int LOUDNESS_THRESHOLD = 200;
	const int BUFFER_SIZE = 1100;

	// other helper variables
	uint8_t r, g, b; // resulting LCD backlight color components [0 .. 255]
	std::stringstream row_1, row_2; // LCD rows


	// simple error checking
	if ((loudness == NULL) || (lcd == NULL))
	{
		std::cerr << "Can't create all objects, exiting" << std::endl;
		return MRAA_ERROR_UNSPECIFIED;
	}


	while (true)
	{
		bool word_found = false;
		long average = 0;

		while(true)
		{
			for (int i = 0; i < BUFFER_SIZE; i++)
			{
				average += loudness->value();
			}

			average /= BUFFER_SIZE;

			if (average > LOUDNESS_THRESHOLD)
			{
				word_count++;
				word_found = true;
//				row_1 << "Word Found! ";
	//			row_2 << "Wordcount " << word_count;
				//color is someone is talking
				r = (int)(0);
				g = (int)(255);
				b = (int)(0);


				// apply the calculated result
				lcd->setColor(r, g, b);
			}

			if (average < LOUDNESS_THRESHOLD)
			{

				if (word_found == true)
					word_count++;

				r = 255;
				g = 0;
				b = 0;

				// apply the calculated result
				lcd->setColor(r, g, b);
			}
		}
	}

	return MRAA_SUCCESS;
}
/*
 * main
 */
int main(int argc, char *argv[])
{
	// vector with dispense events
	std::vector<DispenseEvent> dispense_events;

	mraa::Result response;

	// check command line arguments
	if (argc != 2) {
		std::cerr << "Usage: " << argv[0] <<
			" <sqlite3_events_file> " << std::endl;
		exit(1);
	}
	events_file = argv[1];

#if GALILEO == 1
	// check that we are running on Galileo
	mraa_platform_t platform = mraa_get_platform_type();
	if ((platform != MRAA_INTEL_GALILEO_GEN1) &&
			(platform != MRAA_INTEL_GALILEO_GEN2)) {
		std::cerr << "Unsupported platform, exiting" << std::endl;
		return MRAA_ERROR_INVALID_PLATFORM;
	}
#endif

#if PROTOTYPE == 1
	// temperature sensor connected to A0 (analog in)
	upm::GroveTemp* temp_sensor = new upm::GroveTemp(TEMP_SENSOR_PIN);

	// Grove kit stepper motor
	upm::ULN200XA* motor = new upm::ULN200XA(4096,
			MOTOR_A1_PIN, MOTOR_A2_PIN,
			MOTOR_B1_PIN, MOTOR_B2_PIN);

	mraa::Gpio* motor_sense = new mraa::Gpio(MOTOR_SENSE_PIN);
	if (motor_sense == NULL) {
		return mraa::ERROR_UNSPECIFIED;
	}
	response = motor_sense->dir(mraa::DIR_IN);
	if (response != mraa::SUCCESS) {
		mraa::printError(response);
		return 1;
	}
#else
	// AM2315 temperature sensor
	std::cout << "Initializing temperature sensor" << std::endl;
	upm::AM2315 *temp_sensor = new upm::AM2315(MRAA_OFFSET);
	temp_sensor->testSensor();
	std::cout << "Temperature: " << temp_sensor->getTemperature() << std::endl;

	// Quadstepper Motor Driver + sense switches 
	mraa::Gpio* motor_enable[NUM_MOTORS];
	mraa::Gpio* motor_sense[NUM_MOTORS];
	int motor_enable_gpios[] = {
			MOTOR_ENA1_PIN,
			MOTOR_ENA2_PIN,
			MOTOR_ENA3_PIN
	};
	int motor_sense_gpios[] = {
			MOTOR_SENSE1_PIN,
			MOTOR_SENSE2_PIN,
			MOTOR_SENSE3_PIN
	};
	for (int i = 0; i < NUM_MOTORS; i++) {
		// configure motor enable GPIO
		motor_enable[i] = new mraa::Gpio(motor_enable_gpios[i]);
		if (motor_enable[i] == NULL) {
			return mraa::ERROR_UNSPECIFIED;
		}
		response = motor_enable[i]->dir(mraa::DIR_OUT);
		if (response != mraa::SUCCESS) {
			mraa::printError(response);
			return 1;
		}
		// disable motor by default
		response = motor_enable[i]->write(1);

		// configure motor sense GPIO
		motor_sense[i] = new mraa::Gpio(motor_sense_gpios[i]);
		if (motor_sense[i] == NULL) {
			return mraa::ERROR_UNSPECIFIED;
		}
		response = motor_sense[i]->dir(mraa::DIR_IN);
		if (response != mraa::SUCCESS) {
			mraa::printError(response);
			return 1;
		}
#if GALILEO == 1
		response = motor_sense[i]->mode(mraa::MODE_PULLUP);
		if (response != mraa::SUCCESS) {
			mraa::printError(response);
			return 1;
		}
#endif
	}
	upm::StepMotor* motor = new upm::StepMotor(
			MOTOR_DIR_PIN, MOTOR_STEP_PIN, 200);
#endif

	// machine open sensor connected to D7 (digital in)
	mraa::Gpio* machine_open_sensor = new mraa::Gpio(TRAY_SENSE_PIN);
	if (machine_open_sensor == NULL) {
		return mraa::ERROR_UNSPECIFIED;
	}
	response = machine_open_sensor->dir(mraa::DIR_IN);
	if (response != mraa::SUCCESS) {
		mraa::printError(response);
		return 1;
	}
	// Grove Button is used in prototype
	// it has a built-in pull-down resistor
#if PROTOTYPE == 0 && GALILEO == 1
	response = machine_open_sensor->mode(mraa::MODE_PULLUP);
	if (response != mraa::SUCCESS) {
		mraa::printError(response);
		return 1;
	}
#endif

	// red led connected to D2 (digital out)
	upm::GroveLed* red_led = new upm::GroveLed(RED_LED_PIN);

	// green led connected to D3 (digital out)
	upm::GroveLed* green_led = new upm::GroveLed(GREEN_LED_PIN);
	if (NULL == green_led) {
		std::cerr << "Unable to initialize the Green LED" << std::endl;
		return MRAA_ERROR_UNSPECIFIED;
	}

	// LCD connected to the default I2C bus
	upm::Jhd1313m1* lcd = new upm::Jhd1313m1(MRAA_OFFSET);

	// check for errors
	if (motor == NULL || red_led == NULL || 
			temp_sensor == NULL || lcd == NULL) {
		std::cerr << "Unable to create all UPM objects, exiting" << std::endl;
		return MRAA_ERROR_UNSPECIFIED;
	}

#if PROTOTYPE != 1
	for (int i = 0; i < NUM_MOTORS; i++) {
		if (reset_motor(motor, motor_enable[i], motor_sense[i])) {
			std::cerr << "Error resetting motor " << i + 1 << std::endl;
		}
	}
#endif

	// loop forever
	int previous_temperature = TEMPERATURE_RANGE_MIN_VAL + 1;
	for (;;) {
		// measure temperature
#if PROTOTYPE == 1
		int temperature = temp_sensor->value();
#else
		int temperature = temp_sensor->getTemperature();
#endif
		// avoid temperature glitches
		if (temperature != 0 && temperature < 150) {
			previous_temperature = temperature;
		} else {
			temperature = previous_temperature;
		}
		// monitor the machine for failures
		monitor_machine(machine_open_sensor, temperature, red_led, green_led, lcd);
		// get dispense events from the database
		db_get_dispence_events(dispense_events);
		// check if any dispense events available
		while (dispense_events.size() > 0) {
			// process the first event from the vector
#if PROTOTYPE == 1
			int result = dispense_product(motor, motor_sense, lcd, dispense_events[0].get_tray());
#else
			int result = dispense_product_stepper(motor, motor_enable, motor_sense, lcd, dispense_events[0].get_tray());
#endif
			dispense_events[0].dispensed(result);
			dispense_events.erase(dispense_events.begin());
		}
		sleep(1);
	}

	return 0;
}
Beispiel #12
0
/**
 * Get platform type, board must be initialised.
 *
 * @return mraa_platform_t Platform type enum
 */
mraa_platform_t getPlatformType()
{
    return mraa_get_platform_type();
}
int main()
{
    // check that we are running on Galileo or Edison
    mraa_platform_t platform = mraa_get_platform_type();
    if ((platform != MRAA_INTEL_GALILEO_GEN1) &&
            (platform != MRAA_INTEL_GALILEO_GEN2) &&
            (platform != MRAA_INTEL_EDISON_FAB_C)) {
        std::cerr << "Unsupported platform, exiting" << std::endl;
        return MRAA_ERROR_INVALID_PLATFORM;
    }


    // temperature sensor connected to A0 (analog in)
    upm::GroveTemp* temp_sensor = new upm::GroveTemp(0);

    // LCD connected to the default I2C bus
    upm::Jhd1313m1* lcd = new upm::Jhd1313m1(0);
    std::stringstream row_1, row_2;
    upm::GroveRelay* relay = new upm::GroveRelay(4);

    // simple error checking
    if ((temp_sensor == NULL) || (lcd == NULL) || (relay == NULL)) {
        std::cerr << "Can't create all objects, exiting" << std::endl;
        return MRAA_ERROR_UNSPECIFIED;
    }

    // loop forever updating the temperature values every second
    for (;;) {

        float temperature = MySensors::load_temperature(0);
        float humidity = MySensors::load_humidity(1);

        row_1.str(std::string());
        row_2.str(std::string());
        row_2 << "Temperature " << std::fixed << std::setprecision(2) <<temperature << "Cº";
        row_1 << "Humidity "<< std::fixed << std::setprecision(0) << humidity << "\%";

        //write in the LCD
        lcd->setCursor(0,0);
        lcd->write(row_1.str());
        lcd->setCursor(1,0);
        lcd->write(row_2.str());


        //clear LCD
        row_1.str(std::string());
        row_2.str(std::string());

        Check_pump_status(humidity,relay);

        //Prepare data to upload to Fi-Ware
        FiWareConnector::Table measures = prepareData(humidity,temperature,relay->isOn());
        //Upload data to Fi-Ware
        FiWareConnector::post_measures(measures);

        sleep(20);

    }

    return MRAA_SUCCESS;
}