Esempio n. 1
0
void joystick_linux::open_joystick(const char *p_path) {

	int joy_num = get_free_joy_slot();
	int fd = open(p_path, O_RDONLY | O_NONBLOCK);
	if (fd != -1 && joy_num != -1) {

		int rc = libevdev_new_from_fd(fd, &joysticks[joy_num].dev);
		if (rc < 0) {

			fprintf(stderr, "Failed to init libevdev (%s)\n", strerror(-rc));
			return;
		}

		libevdev *dev = joysticks[joy_num].dev;

		//check if the device supports basic gamepad events, prevents certain keyboards from
		//being detected as joysticks
		if (libevdev_has_event_type(dev, EV_ABS) && libevdev_has_event_type(dev, EV_KEY) &&
				(libevdev_has_event_code(dev, EV_KEY, BTN_A) || libevdev_has_event_code(dev, EV_KEY, BTN_THUMBL) || libevdev_has_event_code(dev, EV_KEY, BTN_TOP))) {

			char uid[128];
			String name = libevdev_get_name(dev);
			uint16_t bus = __bswap_16(libevdev_get_id_bustype(dev));
			uint16_t vendor = __bswap_16(libevdev_get_id_vendor(dev));
			uint16_t product = __bswap_16(libevdev_get_id_product(dev));
			uint16_t version = __bswap_16(libevdev_get_id_version(dev));

			joysticks[joy_num].reset();

			Joystick &joy = joysticks[joy_num];
			joy.fd = fd;
			joy.devpath = String(p_path);
			setup_joystick_properties(joy_num);
			sprintf(uid, "%04x%04x", bus, 0);
			if (vendor && product && version) {

				sprintf(uid + String(uid).length(), "%04x%04x%04x%04x%04x%04x", vendor,0,product,0,version,0);
				input->joy_connection_changed(joy_num, true, name, uid);
			}
			else {
				String uidname = uid;
				int uidlen = MIN(name.length(), 11);
				for (int i=0; i<uidlen; i++) {

					uidname = uidname + _hex_str(name[i]);
				}
				uidname += "00";
				input->joy_connection_changed(joy_num, true, name, uidname);

			}
		}
		else {
			//device is not a gamepad, clean up
			libevdev_free(dev);
			close(fd);
		}
	}
}
Esempio n. 2
0
lf_hdr* read_lf(lf_hdr *lf, struct hive *h, int offset ) {
  memcpy(lf, h->base+offset+4, sizeof(lf_hdr));
  lf->hr = (h->base+offset+4+4);
#if BYTE_ORDER == LITTLE_ENDIAN
#elif BYTE_ORDER == BIG_ENDIAN
  lf->id = __bswap_16(lf->id);
  lf->key_num = __bswap_16(lf->key_num);
#endif
  return lf;
}
Esempio n. 3
0
vk_hdr* read_vk(vk_hdr *vk, struct hive *h, int offset ) {
  memcpy(vk, h->base+offset+4, sizeof(vk_hdr));
  vk->value_name = (h->base+offset+4+20);
#if BYTE_ORDER == LITTLE_ENDIAN
#elif BYTE_ORDER == BIG_ENDIAN
  vk->id = __bswap_16(vk->id);
  vk->name_len = __bswap_16(vk->name_len);
  vk->flag = __bswap_16(vk->flag);
  vk->data_len = __bswap_32(vk->data_len);
  vk->data_off = __bswap_32(vk->data_off);
  vk->data_type = __bswap_32(vk->data_type);
#endif
  return vk;
}
Esempio n. 4
0
int I2C::Read16( uint8_t reg, uint16_t* value, bool big_endian )
{
	int ret = Read( reg, value, 2 );
	if ( big_endian ) {
		*value = __bswap_16( *value );
	}
	return ret;
}
Esempio n. 5
0
nk_hdr* read_nk(nk_hdr *nk, struct hive *h, int offset ) {
  memcpy(nk, h->base+offset+4, sizeof(nk_hdr));
  nk->key_name = (h->base+offset+4+76);
#if BYTE_ORDER == LITTLE_ENDIAN
#elif BYTE_ORDER == BIG_ENDIAN
  nk->id = __bswap_16(nk->id);
  nk->type = __bswap_16(nk->type);
  nk->name_len = __bswap_16(nk->name_len);
  nk->classname_len = __bswap_16(nk->classname_len);
  nk->classname_off = __bswap_32(nk->classname_off);
  nk->unk2 = __bswap_32(nk->unk2);
  nk->lf_off = __bswap_32(nk->lf_off);
  nk->value_cnt = __bswap_32(nk->value_cnt);
  nk->value_off = __bswap_32(nk->value_off);
  nk->subkey_num = __bswap_32(nk->subkey_num);
#endif
  return nk;
}
Esempio n. 6
0
uint16_t little_endian_chg16(uint16_t n)
{
#if BYTE_ORDER == BIG_ENDIAN
	return __bswap_16(n);
#elif BYTE_ORDER == LITTLE_ENDIAN
	return n;
#else
# error "What kind of system is this?"
#endif
}
static uint16_t
le16toh(uint16_t arg)
{
#if BYTE_ORDER == LITTLE_ENDIAN
    return arg;
#elif BYTE_ORDER == BIG_ENDIAN
    return __bswap_16(arg);
#else
#  error Unknown byte order!
#endif
}
Esempio n. 8
0
static void myAnalogWrite (struct wiringPiNodeStruct *node, int pin, int data)
{
  int chan = pin - node->pinBase ;
  int reg ;
  int16_t ndata ;

  chan &= 3 ;

  reg = chan + 2 ;

  /**/ if (data < -32767)
    ndata = -32767 ;
  else if (data > 32767)
    ndata = 32767 ;
  else
    ndata = (int16_t)data ;

  ndata = __bswap_16 (ndata) ;
  wiringPiI2CWriteReg16 (node->fd, reg, data) ;
}
void joystick_linux::open_joystick(const char *p_path) {

	int joy_num = get_free_joy_slot();
	int fd = open(p_path, O_RDONLY | O_NONBLOCK);
	if (fd != -1 && joy_num != -1) {

		unsigned long evbit[NBITS(EV_MAX)] = { 0 };
		unsigned long keybit[NBITS(KEY_MAX)] = { 0 };
		unsigned long absbit[NBITS(ABS_MAX)] = { 0 };

		if ((ioctl(fd, EVIOCGBIT(0, sizeof(evbit)), evbit) < 0) ||
		    (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybit)), keybit) < 0) ||
		    (ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbit)), absbit) < 0)) {
			return;
		}

		//check if the device supports basic gamepad events, prevents certain keyboards from
		//being detected as joysticks
		if (!(test_bit(EV_KEY, evbit) && test_bit(EV_ABS, evbit) &&
		    ((test_bit(ABS_X, absbit) || test_bit(ABS_Y, absbit)) ||
		     (test_bit(BTN_A, keybit) || test_bit(BTN_THUMBL, keybit))))) {
			close(fd);
			return;
		}

		char uid[128];
		char namebuf[128];
		String name = "";
		input_id inpid;
		if (ioctl(fd, EVIOCGNAME(sizeof(namebuf)), namebuf) >= 0) {
			name = namebuf;
		}

		if (ioctl(fd, EVIOCGID, &inpid) < 0) {
			close(fd);
			return;
		}

		joysticks[joy_num].reset();

		Joystick &joy = joysticks[joy_num];
		joy.fd = fd;
		joy.devpath = String(p_path);
		setup_joystick_properties(joy_num);
		sprintf(uid, "%04x%04x", __bswap_16(inpid.bustype), 0);
		if (inpid.vendor && inpid.product && inpid.version) {

			uint16_t vendor = __bswap_16(inpid.vendor);
			uint16_t product = __bswap_16(inpid.product);
			uint16_t version = __bswap_16(inpid.version);

			sprintf(uid + String(uid).length(), "%04x%04x%04x%04x%04x%04x", vendor,0,product,0,version,0);
			input->joy_connection_changed(joy_num, true, name, uid);
		}
		else {
			String uidname = uid;
			int uidlen = MIN(name.length(), 11);
			for (int i=0; i<uidlen; i++) {

				uidname = uidname + _hex_str(name[i]);
			}
			uidname += "00";
			input->joy_connection_changed(joy_num, true, name, uidname);
		}
	}
}
Esempio n. 10
0
inline uint16_t bswap(uint16_t x)
{
	return __bswap_16(x);
}
Esempio n. 11
0
File: ns.c Progetto: rendau/rose
uint16_t
ns_csu16(uint16_t n) {
  if(ns_isne())
    return n;
  return __bswap_16(n);
}
uint16_t test_bswap16 ( uint16_t x ) {
	return __bswap_16 ( x );
}
Esempio n. 13
0
static inline uint16_t tofs16(uint16_t i)
{
	return __bswap_16(i);
	//return i;
	
}
Esempio n. 14
0
inline UInt16 hton16(UInt16 v) { return __bswap_16(v); }
Esempio n. 15
0
uint16_t htons (uint16_t x)
{
	return __bswap_16(x);
}
Esempio n. 16
0
uint16_t ntohs (uint16_t x)
{
	return __bswap_16(x);
}
Esempio n. 17
0
inline UInt16 ntoh16(UInt16 v) { return __bswap_16(v); }
Esempio n. 18
0
static int myAnalogRead (struct wiringPiNodeStruct *node, int pin)
{
  int chan = pin - node->pinBase ;
  int16_t  result ;
  uint16_t config = CONFIG_DEFAULT ;

  chan &= 7 ;

// Setup the configuration register

//	Set PGA/voltage range

  config &= ~CONFIG_PGA_MASK ;
  config |= node->data0 ;

//	Set sample speed

  config &= ~CONFIG_DR_MASK ;
  config |= node->data1 ;

//	Set single-ended channel or differential mode

  config &= ~CONFIG_MUX_MASK ;

  switch (chan)
  {
    case 0: config |= CONFIG_MUX_SINGLE_0 ; break ;
    case 1: config |= CONFIG_MUX_SINGLE_1 ; break ;
    case 2: config |= CONFIG_MUX_SINGLE_2 ; break ;
    case 3: config |= CONFIG_MUX_SINGLE_3 ; break ;

    case 4: config |= CONFIG_MUX_DIFF_0_1 ; break ;
    case 5: config |= CONFIG_MUX_DIFF_2_3 ; break ;
    case 6: config |= CONFIG_MUX_DIFF_0_3 ; break ;
    case 7: config |= CONFIG_MUX_DIFF_1_3 ; break ;
  }

//	Start a single conversion

  config |= CONFIG_OS_SINGLE ;
  config = __bswap_16 (config) ;
  wiringPiI2CWriteReg16 (node->fd, 1, config) ;

// Wait for the conversion to complete

  for (;;)
  {
    result =  wiringPiI2CReadReg16 (node->fd, 1) ;
    result = __bswap_16 (result) ;
    if ((result & CONFIG_OS_MASK) != 0)
      break ;
    delayMicroseconds (100) ;
  }

  result =  wiringPiI2CReadReg16 (node->fd, 0) ;
  result = __bswap_16 (result) ;

// Sometimes with a 0v input on a single-ended channel the internal 0v reference
//	can be higher than the input, so you get a negative result...

  if ( (chan < 4) && (result < 0) ) 
    return 0 ;
  else
    return (int)result ;
}