<commit_before>//   Copyright 2016-2020 <NAME>
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.

/*
 * Simple ranger example, using VL53L0X Time-of-flight range sensor I2C device.
 * This program uses FastArduino VL53L0X support API.
 * It checks conditions of use of GPIO pin.
 * 
 * Wiring:
 * - on ATmega328P based boards (including Arduino UNO):
 *   - A4 (PC4, SDA): connected to VL53L0X SDA pin
 *   - A5 (PC5, SCL): connected to VL53L0X SCL pin
 *   - D2 (EXT0): connected to VL53L0X GPIO pin
 */

#include <fastarduino/gpio.h>
#include <fastarduino/int.h>
#include <fastarduino/realtime_timer.h>
#include <fastarduino/time.h>
#include <fastarduino/uart.h>
#include <fastarduino/i2c_handler.h>
#include <fastarduino/devices/vl53l0x.h>

// #define FORCE_SYNC

static constexpr const i2c::I2CMode MODE = i2c::I2CMode::FAST;
static constexpr const board::ExternalInterruptPin GPIO = board::ExternalInterruptPin::D2_PD2_EXT0;

static constexpr const board::USART UART = board::USART::USART0;
static constexpr const uint8_t OUTPUT_BUFFER_SIZE = 128;
static char output_buffer[OUTPUT_BUFFER_SIZE];

REGISTER_RTT_ISR(0)
REGISTER_UATX_ISR(0)

#if I2C_TRUE_ASYNC and not defined(FORCE_SYNC)
using MANAGER = i2c::I2CAsyncManager<MODE, i2c::I2CErrorPolicy::CLEAR_ALL_COMMANDS>;
static constexpr const uint8_t I2C_BUFFER_SIZE = 32;
static MANAGER::I2CCOMMAND i2c_buffer[I2C_BUFFER_SIZE];
REGISTER_I2C_ISR(MANAGER)
#else
using MANAGER = i2c::I2CSyncManager<MODE>;
#endif

using streams::dec;
using streams::hex;
using streams::fixed;
using streams::endl;
using streams::flush;

using namespace devices::vl53l0x;
using TOF = VL53L0X<MANAGER>;

#define CHECK_OK(STATEMENT) if (! STATEMENT) out << F(#STATEMENT) << F(" ERROR!") << endl

class ToFController
{
public:
	ToFController(TOF& tof) : tof_{tof}
	{
		interrupt::register_handler(*this);
		signal_.enable();
	}

	void await_gpio()
	{
		while (!gpio_)
		{
			time::yield();
		}
		gpio_ = false;
	}

private:
	void gpio_raised()
	{
		gpio_ = true;
	}

	TOF& tof_;
	gpio::FAST_EXT_PIN<GPIO> gpio_pin_{gpio::PinMode::INPUT};
	interrupt::INTSignal<GPIO> signal_{interrupt::InterruptTrigger::FALLING_EDGE};
	volatile bool gpio_ = false;

	DECL_INT_ISR_HANDLERS_FRIEND
};

REGISTER_INT_ISR_METHOD(0, GPIO, ToFController, &ToFController::gpio_raised)

static void loop_gpio(
	streams::ostream& out, timer::RTT<board::Timer::TIMER0>& rtt, ToFController& controller, TOF& tof)
{
	out << F("await GPIO") << endl;
	// Limit loop to 1'
	for (uint8_t i = 0; i < 60; ++i)
	{
		uint16_t range = 0;
		rtt.millis(0);
		controller.await_gpio();
		time::RTTTime time1 = rtt.time();
<commit_msg>Shorten timing of VL53L0X example<commit_after>//   Copyright 2016-2020 <NAME>
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.

/*
 * Simple ranger example, using VL53L0X Time-of-flight range sensor I2C device.
 * This program uses FastArduino VL53L0X support API.
 * It checks conditions of use of GPIO pin.
 * 
 * Wiring:
 * - on ATmega328P based boards (including Arduino UNO):
 *   - A4 (PC4, SDA): connected to VL53L0X SDA pin
 *   - A5 (PC5, SCL): connected to VL53L0X SCL pin
 *   - D2 (EXT0): connected to VL53L0X GPIO pin
 */

#include <fastarduino/gpio.h>
#include <fastarduino/int.h>
#include <fastarduino/realtime_timer.h>
#include <fastarduino/time.h>
#include <fastarduino/uart.h>
#include <fastarduino/i2c_handler.h>
#include <fastarduino/devices/vl53l0x.h>

// #define FORCE_SYNC

static constexpr const i2c::I2CMode MODE = i2c::I2CMode::FAST;
static constexpr const board::ExternalInterruptPin GPIO = board::ExternalInterruptPin::D2_PD2_EXT0;

static constexpr const board::USART UART = board::USART::USART0;
static constexpr const uint8_t OUTPUT_BUFFER_SIZE = 128;
static char output_buffer[OUTPUT_BUFFER_SIZE];

REGISTER_RTT_ISR(0)
REGISTER_UATX_ISR(0)

#if I2C_TRUE_ASYNC and not defined(FORCE_SYNC)
using MANAGER = i2c::I2CAsyncManager<MODE, i2c::I2CErrorPolicy::CLEAR_ALL_COMMANDS>;
static constexpr const uint8_t I2C_BUFFER_SIZE = 32;
static MANAGER::I2CCOMMAND i2c_buffer[I2C_BUFFER_SIZE];
REGISTER_I2C_ISR(MANAGER)
#else
using MANAGER = i2c::I2CSyncManager<MODE>;
#endif

using streams::dec;
using streams::hex;
using streams::fixed;
using streams::endl;
using streams::flush;

using namespace devices::vl53l0x;
using TOF = VL53L0X<MANAGER>;

#define CHECK_OK(STATEMENT) if (! STATEMENT) out << F(#STATEMENT) << F(" ERROR!") << endl

class ToFController
{
public:
	ToFController(TOF& tof) : tof_{tof}
	{
		interrupt::register_handler(*this);
		signal_.enable();
	}

	void await_gpio()
	{
		while (!gpio_)
		{
			time::yield();
		}
		gpio_ = false;
	}

private:
	void gpio_raised()
	{
		gpio_ = true;
	}

	TOF& tof_;
	gpio::FAST_EXT_PIN<GPIO> gpio_pin_{gpio::PinMode::INPUT};
	interrupt::INTSignal<GPIO> signal_{interrupt::InterruptTrigger::FALLING_EDGE};
	volatile bool gpio_ = false;

	DECL_INT_ISR_HANDLERS_FRIEND
};

REGISTER_INT_ISR_METHOD(0, GPIO, ToFController, &ToFController::gpio_raised)

static void loop_gpio(
	streams::ostream& out, timer::RTT<board::Timer::TIMER0>& rtt, ToFController& controller, TOF& tof)
{
	out << F("await GPIO") << endl;
	// Limit loop to 1'
	for (uint8_t i = 0; i < 30; ++i)
	{
		uint16_t range = 0;
		rtt.millis(0);
		controller.await_gpio();
		time::RTTTime time1 = rtt.time();

{'fraction_non_alphanumeric': 0.11902500791389681, 'fraction_numerical': 0.028331750553972777, 'mean_word_length': 4.83471837488458, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2197251', 'n_tokens_mistral': 2432, 'n_tokens_neox': 2307, 'n_words': 606}