Exemplo n.º 1
0
uint8_t SkaarhojUtils::touch_state() {
	
	bool isTouchedNow = touch_isTouched();
	if (_touch_exitByTapAndHold)	{
		//Serial.print("_touch_exitByTapAndHold is set: ");
		if (isTouchedNow)	{
			isTouchedNow = false;
			//Serial.println("keeps isTouchedNow low");
		} else {
			_touch_exitByTapAndHold = false;
			//Serial.println("resets _touch_exitByu....");
		}
	}
		
		// If screen is touched currently:
	if (isTouchedNow)	{
			// New touch registered:
		if (!_touch_isTouchedState)	{
			_touch_isTouchedState = true;
			_touch_checkingForTapAndHold = false;

			// Init variables:
			_touch_touchStartedRawXVal = 0;
			_touch_touchStartedRawYVal = 0;
			_touch_touchCoordRoundRobinIdx = 0;
			_touch_touchStartedTime = millis();	// long unsigned
			
			_touch_touchRawXValMax = 0;
			_touch_touchRawXValMin = 1024;
			_touch_touchRawYValMax = 0;
			_touch_touchRawYValMin = 1024;
			
			_touch_touchEndedRawXVal = 0;
			_touch_touchEndedRawYVal = 0;
		}
		
			// Round robin accumulation of the coordinates:
		_touch_touchRawXValRoundRobin[_touch_touchCoordRoundRobinIdx % 8] = _touch_rawXVal;
		_touch_touchRawYValRoundRobin[_touch_touchCoordRoundRobinIdx % 8] = _touch_rawYVal;
		_touch_touchCoordRoundRobinIdx++;
		
			// Set the start touch coordinate:
			// First, there must be 8 recent values in the round-robin base, second the start value must not already be set and thirdly, we  register it only AFTER the touchTimeThreshold has been reached. This is all about "stabilizing" the touch value since it fluctuates most when people put their finger to the screen first time.
		if (_touch_touchCoordRoundRobinIdx>=8 && _touch_touchStartedRawXVal==0 && (unsigned long)millis()-_touch_touchStartedTime>_touch_touchTimeThreshold)	{
			for(uint8_t i=0;i<8;i++)	{
				_touch_touchStartedRawXVal+=_touch_touchRawXValRoundRobin[i];
				_touch_touchStartedRawYVal+=_touch_touchRawYValRoundRobin[i];
			}
			_touch_touchStartedRawXVal/=8;
			_touch_touchStartedRawYVal/=8;
		}
		
			// Min / max:
		if (_touch_touchStartedRawXVal>0 && _touch_touchCoordRoundRobinIdx>=12)	{
			_touch_endedValueCalculation();
			
			if (_touch_touchEndedRawXVal > 	_touch_touchRawXValMax)	_touch_touchRawXValMax = _touch_touchEndedRawXVal;
			if (_touch_touchEndedRawXVal < 	_touch_touchRawXValMin)	_touch_touchRawXValMin = _touch_touchEndedRawXVal;
			if (_touch_touchEndedRawYVal > 	_touch_touchRawYValMax)	_touch_touchRawYValMax = _touch_touchEndedRawYVal;
			if (_touch_touchEndedRawYVal < 	_touch_touchRawYValMin)	_touch_touchRawYValMin = _touch_touchEndedRawYVal;

		}		

			// Checking if a single touch is the case and if so exits the active touch in case the tapAndHold threshold has been exceeded.
		if (!_touch_checkingForTapAndHold && (unsigned long)millis()-_touch_touchStartedTime > _touch_touchTimeTapAndHold)	{
			//Serial.println("Check for tapAndHold...");
			_touch_checkingForTapAndHold = true;
			_touch_endedValueCalculation();
			if (touch_type()==1)	{
				//Serial.println("Exceeded time, exit");
				_touch_exitByTapAndHold = true;
			} else {
				//Serial.println("Nope...!");
			}
		}
	} else {	// Touch ended:
		if (_touch_isTouchedState)	{
			_touch_isTouchedState = false;
			unsigned long touchTime = (unsigned long)millis()-_touch_touchStartedTime;
			
			if (touchTime > _touch_touchTimeThreshold && _touch_touchStartedRawXVal>0)	{
				_touch_endedValueCalculation();
				
				uint8_t touchType = touch_type();


/*
								Serial.print("Start: ");
								Serial.print(_touch_touchStartedRawXVal);
								Serial.print(",");
								Serial.println(_touch_touchStartedRawYVal);
								Serial.print("End: ");
								Serial.print(_touch_touchEndedRawXVal);
								Serial.print(",");
								Serial.println(_touch_touchEndedRawYVal);

								unsigned int dX = abs(_touch_touchStartedRawXVal-_touch_touchEndedRawXVal);
								unsigned int dY = abs(_touch_touchStartedRawYVal-_touch_touchEndedRawYVal);

								Serial.print("dX,dY: ");
								Serial.print(dX);
								Serial.print(",");
								Serial.println(dY);

								Serial.print("dX/dY, dY/dX: ");
								Serial.print((float)dX/dY);
								Serial.print(",");
								Serial.println((float)dY/dX);

								Serial.print("xMax,yMax: ");
								Serial.print(_touch_touchRawXValMax);
								Serial.print(",");
								Serial.println(_touch_touchRawYValMax);

								Serial.print("xMin,yMin: ");
								Serial.print(_touch_touchRawXValMin);
								Serial.print(",");
								Serial.println(_touch_touchRawYValMin);

								Serial.print("Bounding Box: ");
								Serial.print(_touch_touchRawXValMax-_touch_touchRawXValMin);
								Serial.print(",");
								Serial.println(_touch_touchRawYValMax-_touch_touchRawYValMin);

								Serial.print("Touch Type:");
								Serial.println(touch_type());

*/
				
					// Single:
				if (touchType==1)	{
					uint8_t retVal =0;
					
					if ((unsigned long)_touch_touchStartedTime-_touch_touchStartedTime2<500) {
						if ((unsigned long)_touch_touchStartedTime2-_touch_touchStartedTime3<500)	{
							_touch_touchStartedTime3=0;
							_touch_touchStartedTime2=0;
							_touch_touchStartedTime=0;
							retVal= 3;	// Triple
						} else {
							retVal= 2;	// Double
						}
					} else {
						if (touchTime > _touch_touchTimeTapAndHold)	{
							retVal= 9;	// Single + hold
						} else {
							retVal= 1;	// Single
						}
					}

					_touch_touchStartedTime3=_touch_touchStartedTime2;
					_touch_touchStartedTime2=_touch_touchStartedTime;
					
					return retVal;
				}
				
					// Gesture:
				if (touchType>=10 && touchType<=13)	{
					return touchType;
				}
				
				return 255;	// Something else touched...
			}
		}
	}
	
	return 0;
}
Exemplo n.º 2
0
int main(int argc, char **argv)
{
	sysarg_t baud = 38400;
	service_id_t svc_id;
	char *serial_port_name = NULL;

	int arg = 1;
	int rc;

	isdv4_event_fn event_fn = emit_event;

	if (argc > arg && str_test_prefix(argv[arg], "--baud=")) {
		size_t arg_offset = str_lsize(argv[arg], 7);
		char* arg_str = argv[arg] + arg_offset;
		if (str_length(arg_str) == 0) {
			fprintf(stderr, "--baud requires an argument\n");
			syntax_print();
			return 1;
		}
		char *endptr;
		baud = strtol(arg_str, &endptr, 10);
		if (*endptr != '\0') {
			fprintf(stderr, "Invalid value for baud\n");
			syntax_print();
			return 1;
		}
		arg++;
	}

	if (argc > arg && str_cmp(argv[arg], "--print-events") == 0) {
		event_fn = print_and_emit_event;
		arg++;
	}

	if (argc > arg) {
		serial_port_name = argv[arg];
		rc = loc_service_get_id(serial_port_name, &svc_id, 0);
		if (rc != EOK) {
			fprintf(stderr, "Cannot find device service %s\n",
			    argv[arg]);
			return 1;
		}
		arg++;
	}
	else {
		category_id_t serial_cat_id;

		rc = loc_category_get_id("serial", &serial_cat_id, 0);
		if (rc != EOK) {
			fprintf(stderr, "Failed getting id of category "
			    "'serial'\n");
			return 1;
		}

		service_id_t *svc_ids;
		size_t svc_count;

		rc = loc_category_get_svcs(serial_cat_id, &svc_ids, &svc_count);		if (rc != EOK) {
			fprintf(stderr, "Failed getting list of services\n");
			return 1;
		}

		if (svc_count == 0) {
			fprintf(stderr, "No service in category 'serial'\n");
			free(svc_ids);
			return 1;
		}

		svc_id = svc_ids[0];

		rc = loc_service_get_name(svc_id, &serial_port_name);
		if (rc != EOK) {
			fprintf(stderr, "Failed getting name of serial service\n");
			return 1;
		}

		free(svc_ids);
	}

	if (argc > arg) {
		fprintf(stderr, "Too many arguments\n");
		syntax_print();
		return 1;
	}

	fibril_mutex_initialize(&client_mutex);

	printf(NAME ": Using serial port %s\n", serial_port_name);

	async_sess_t *sess = loc_service_connect(svc_id, INTERFACE_DDF,
	    IPC_FLAG_BLOCKING);
	if (!sess) {
		fprintf(stderr, "Failed connecting to service\n");
	}

	async_exch_t *exch = async_exchange_begin(sess);
	rc = async_req_4_0(exch, SERIAL_SET_COM_PROPS, baud,
	    SERIAL_NO_PARITY, 8, 1);
	async_exchange_end(exch);

	if (rc != EOK) {
		fprintf(stderr, "Failed setting serial properties\n");
		return 2;
	}

	rc = isdv4_init(&state, sess, event_fn);
	if (rc != EOK) {
		fprintf(stderr, "Failed initializing isdv4 state");
		return 2;
	}

	rc = isdv4_init_tablet(&state);
	if (rc != EOK) {
		fprintf(stderr, "Failed initializing tablet");
		return 2;
	}

	printf("Tablet information:\n");
	printf(" Stylus: %ux%u pressure: %u tilt: ", state.stylus_max_x,
	    state.stylus_max_y, state.stylus_max_pressure);
	if (state.stylus_tilt_supported) {
		printf("%ux%u\n", state.stylus_max_xtilt, state.stylus_max_ytilt);
	}
	else {
		printf("not supported\n");
	}
	printf(" Touch: %ux%u type: %s\n", state.touch_max_x, state.touch_max_y,
		touch_type(state.touch_type));
	
	fid_t fibril = fibril_create(read_fibril, NULL);
	/* From this on, state is to be used only by read_fibril */
	fibril_add_ready(fibril);

	async_set_fallback_port_handler(mouse_connection, NULL);
	rc = loc_server_register(NAME);
	if (rc != EOK) {
		printf("%s: Unable to register driver.\n", NAME);
		return rc;
	}

	service_id_t service_id;
	char *service_name;
	rc = asprintf(&service_name, "mouse/isdv4-%" PRIun, svc_id);
	if (rc < 0) {
		printf(NAME ": Unable to create service name\n");
		return rc;
	}

	rc = loc_service_register(service_name, &service_id);
	if (rc != EOK) {
		printf(NAME ": Unable to register service %s.\n", service_name);
		return rc;
	}

	category_id_t mouse_category;
	rc = loc_category_get_id("mouse", &mouse_category, IPC_FLAG_BLOCKING);
	if (rc != EOK) {
		printf(NAME ": Unable to get mouse category id.\n");
	}
	else {
		rc = loc_service_add_to_cat(service_id, mouse_category);
		if (rc != EOK) {
			printf(NAME ": Unable to add device to mouse category.\n");
		}
	}

	printf("%s: Accepting connections\n", NAME);
	task_retval(0);
	async_manager();

	/* Not reached */
	return 0;
}