⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 蓝牙无线鼠标软件代码。采用CSR公司的BLUELAB27模式。有很好的参考价值。
💻 C
字号:
/****************************************************************************

  Copyright (C) Cambridge Silicon Radio 2002

FILE
        main.c - mouse example using HIDEngine

MODIFICATION HISTORY


*****************************************************************************/
#include <stdlib.h>
#include <string.h>
#include <debounce.h>
#include <event.h>
#include <message.h>
#include <sched.h>
#include <vm.h>
#include <ps.h>
#include <timer.h>
#include <pio.h>
#include <hid.h>
#include "mouse.h"
#include "power_table.h"
#include "hid_mouse_service_record.h"

/*
	Globals
*/
DEVICE_STATE_T	dev_state;

/*
	Prototypes for static functions
*/
static void initialise(void);

/*
	Entry point
*/
int main(void)
{
	/* initialise our HID */
	initialise();

	/* start scheduler as this is event driven */
	Sched();
	return 0;
}

/*
	This function initialises the mouse by initialising the HIDEngine
	and setting internal state.
*/
static void initialise(void)
{
	/* create initialisation message to HIDEngine */	
	MAKE_MSG(HID_INIT_REQ);

	/* set up hid service record */
	msg->hid_service_record_size = sizeof(hid_mouse_service_record);
	msg->hid_service_record = hid_mouse_service_record;

	/* we don't have a Plug and Play record, but could add one using the sample method as for hid record. */
	msg->pnp_service_record_size = 0;

	/* set class of device to mouse */
	msg->class_of_device = COD_LIMITED_DISCOVERY | HIDCOD_MAJOR | HIDCOD_MINOR_MOUSE;

	/* define our mouse interface */
	msg->interface = hidio;
	msg->number_of_buttons = 3;
	msg->wheel_type = full_pulse_low_enable; /* set up for optical wheel */
	msg->sensor_type = agilent2030;
	msg->xy_translation = no_translation; /* sensor is not rotated */

	/* we have a fixed PIN */
	msg->keyboard_pin_entry = FALSE;

	/* send our power management table */
	msg->power_state_table = PowerTable;

	/* post message to HIDEngine */
	MessagePut(0,msg);

	/* initialise our info */
	memset(&dev_state,0,sizeof(DEVICE_STATE_T));
	dev_state.state = init;

	/* default protocol is report */
	dev_state.protocol = HIDPROTOCOL_REPORT;

	/* see if we are virtually cabled - non zero BD addr */
	(void)PsRetrieve(0,&dev_state.host_addr,sizeof(BD_ADDR_T));
	if (dev_state.host_addr.lap || dev_state.host_addr.nap || dev_state.host_addr.uap)
		dev_state.virtual_cable = TRUE;
	else
		dev_state.virtual_cable = FALSE;
	
	/* set up event on connect button press */
    SchedPioEntryChanged();

    PioSetDir(STATUS_LED1 | STATUS_LED2, STATUS_LED1 | STATUS_LED2);
	PioSet(STATUS_LED1 | STATUS_LED2, STATUS_LED1 | STATUS_LED2);
}

/*
	This function starts a connection scenario by sending
	CONNECT_REQ to HIDEngine with correct parameters for
	type of scenario.
*/
void connect(connect_type_t con_type)
{
	MAKE_MSG(HID_CONNECT_REQ);

	/* specify our configuration for each connect scenario */
	switch (con_type)
	{
		case reconnect_to_host_on_data:
			/* mouse sensor sample rate when disconnected */
			msg->sample_rate = 500; /* 500ms */
			/*lint -fallthrough */
		case reconnect_to_host:
			dev_state.state = connecting;
			msg->host_addr = dev_state.host_addr;
			break;
		case host_to_reconnect:
			dev_state.state = connecting;
			msg->host_addr = dev_state.host_addr;
			msg->timeout = 0; /* never timeout */
			break;
		case host_to_discover_and_connect:
			/* turn on status LED for debugging to show inquiry or page*/
			PioSet(STATUS_LED1, 0);

			dev_state.state = discoverable;

			/* clear the virtual cable */
			memset(&msg->host_addr,0,sizeof(BD_ADDR_T));
			dev_state.virtual_cable = FALSE;
			(void)PsStore(0,&dev_state.host_addr,sizeof(BD_ADDR_T));

			/* discoverable for 3 minutes, although this seems a long
			   time, it can take the user a while to navigate the Host
			   through the discovery/pairing process. */
			msg->timeout = 180;
			break;
		case reserved1:
		case reserved2:
		case reconnect_on_data_or_from_host:
		default:
	}

	msg->connect_type = con_type;

	/* set the supervision timeout to the same as the SDP record (5 seconds) */
	msg->supervision_timeout = 0x1f40;

	MessagePut(0,msg);
}

/*
	Tell HIDEngine we want the current connection scenario
	to fail as soon as possible.
	The result will be a CONNECT_CFM as soon as possible reporting
	that the scenario had failed or timed out, as in normal operation.
*/
void cancelConnect(void)
{
	MAKE_MSG(HID_CANCEL_CONNECT_REQ);
	MessagePut(0,msg);
}

/*
	Disconnect from the Host.

	If the unplug flag is set, we will unplug our virtual cable as
	well as disconnecting.
*/
void disconnect(bool_t unplug)
{
	MAKE_MSG(HID_DISCONNECT_REQ);

	/* set state to disconnecting */
	dev_state.state = disconnecting;

	/* are we unplugging? */
	if (unplug)
	{
		/* yes remove virtual cable */
		removeVirtualCable();

		/* tell HIDEngine to unplug */
		msg->virtual_cable_unplug = TRUE;
	}
	else
		msg->virtual_cable_unplug = FALSE;

	/* post message to HIDEngine */
	MessagePut(0,msg);
}

/*
	Removes all details of virtual cable
*/
void removeVirtualCable(void)
{
	/* delete all knowledge of virtual cable */
	dev_state.virtual_cable = FALSE;
	memset(&dev_state.host_addr,0,sizeof(BD_ADDR_T));

	/* delete the PS info by write length zero to it */
	(void)PsStore(0,&dev_state.host_addr,sizeof(BD_ADDR_T));
}

/*
	Sends a battery level report to the Host.
*/
void sendBatteryReport(uint16_t battery_level)
{
	/*
		TODO:
		There is no standard method for HIDs to indicate their battery
		level.  All current wireless HID's use vendor specific reports
		sent from their mouse which the vendor's Windows software
		intercepts and processes.
		Below is example code of how a vendor specific report could be
		sent.  The report must have a corresponding HID descriptor in
		the device's SDP service record.

		Note that is method of sending reports is slow and may add latency
		to other reports in the pipeline.  Sending a report will also
		move the device back to the first powermode as if the mouse is moved,
		so it is recommended that these are only sent during active power mode.
	*/
#if 0
	MAKE_MSG(HID_SEND_INPUT_REPORT_REQ);

	/* create the report */
	msg->data = (uint8_t *)malloc(sizeof(uint8_t)*3);

	/* multiply battery level by two as there is divide by 2
	   potential divider on ADC input */
	battery_level *=2;

	msg->data[0] = 0x3; /* report ID 3 */
	msg->data[1] = (battery_level & 0xff);
	msg->data[2] = (battery_level >> 8);
	msg->length = 3;

	/* post message to HIDEngine */
	MessagePut(0,msg);
#else
	/* keep compiler happy */
	battery_level = battery_level;
#endif
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -