/****************************************************************************
 *
 *   Copyright (c) 2017 Fan.zhang. All rights reserved. 421395590@qq.com
 *   Copyright (c) 2017 PX4 Development Team. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 3. Neither the name PX4 nor the names of its contributors may be
 *    used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 ****************************************************************************/

#pragma once

/*! @file rpi_rc_in.h
 * Raspberry Pi driver to publish RC input from shared memory.
 * It requires the ppmdecode program (https://github.com/crossa/raspberry-pi-ppm-rc-in)
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdint.h>

#include <px4_config.h>
#include <px4_workqueue.h>
#include <px4_defines.h>

#include <drivers/drv_hrt.h>

#include <uORB/uORB.h>
#include <uORB/topics/input_rc.h>

#define RCINPUT_MEASURE_INTERVAL_US 20000

namespace rpi_rc_in
{
class RcInput
{
public:
	RcInput() = default;

	~RcInput();

	/** @return 0 on success, -errno on failure */
	int start();

	/** @return 0 on success, -errno on failure */
	void stop();

	/** Trampoline for the work queue. */
	static void cycle_trampoline(void *arg);

	bool is_running()
	{
		return _is_running;
	}

private:
	void _cycle();
	void _measure();

	int rpi_rc_init();

	bool _should_exit = false;
	bool _is_running = false;
	struct work_s _work = {};
	orb_advert_t _rcinput_pub = nullptr;
	int _channels = 8; //D8R-II plus
	struct input_rc_s _data = {};

	int *_mem = nullptr;
	key_t _key = 4096; ///< shared memory key (matches the ppmdecode program's key)
	int _shmid = 0;
};

static void usage(const char *reason);
static RcInput *rc_input = nullptr;
}
extern "C" __EXPORT int rpi_rc_in_main(int argc, char **argv);
