Пример #1
0
int main(void) {
    piHiPri(20);
    wiringPiSetup();

    struct encoder *encoder = setupencoder(
            ROTARY_PIN_A,
            ROTARY_PIN_B,
            ROTARY_BUTTON
            );

    long last_value = -1;
    double hold_time = 0;

    while (1) {
        // Divide by three since it counts 
        // about 3 value increments per "tick" which is too much.
        long curr = lround(encoder->value);
        if(curr != last_value) {
            printf("v %ld\n", curr);
            fflush(stdout);
            last_value = curr;
        }

        if(encoder->button_was_pressed && encoder->button_curr_pressed) {
            encoder->button_was_pressed = false;
            printf("p 1\n");
            fflush(stdout);
        }

        if(encoder->button_time.tv_sec > 0) {
            struct timeval now;
            gettimeofday(&now, NULL);

            hold_time = now.tv_sec - encoder->button_time.tv_sec;
            hold_time += (now.tv_usec - encoder->button_time.tv_usec) / (1000.0 * 1000.0);
            printf("t %f\n", hold_time);
            fflush(stdout);
        } else {
            hold_time = 0;
        }

        if(encoder->button_was_released && !encoder->button_curr_pressed) {
            encoder->button_was_released = false;
            printf("p 0\n");
            fflush(stdout);
        }

        // Sleep for a short amount to save cpu-time:
        struct timespec delay = {
            .tv_sec = 0,
            .tv_nsec = 1000 * 1000 * 25,
        };

        nanosleep(&delay, NULL);
    }

    return EXIT_SUCCESS;
}
int main()
{
CURL *curl;
CURLcode res;
wiringPiSetup () ;
/*using pins 23/24*/
struct encoder *encoder = setupencoder(4,5,6);
int buttonState;
long value;
char buffer[250];
while (1)
 {
 updateEncoders();
 buttonState = digitalRead(6);
 long l = encoder->value;
 if(l!=value)
 {
     printf("value: %d\n", (void *)l);
     curl=curl_easy_init();
     sprintf(buffer, "http://127.0.0.1/jsonrpc?request={\"jsonrpc\":\"2.0\",\"method\":\"Application.SetVolume\",\"params\":{\"volume\":%d},\"id\":1}", l);
     curl_easy_setopt(curl, CURLOPT_URL, buffer);
    /* Perform the request, res will get the return code */
     res = curl_easy_perform(curl);
     /* Check for errors */
     if(res != CURLE_OK)
     {
         fprintf(stderr, "curl_easy_perform() failed: %s\n",curl_easy_strerror(res));
     }
     /* always cleanup */
     curl_easy_cleanup(curl);
     value = l;
 }
 if(buttonState==1)
 {
     curl=curl_easy_init();
     sprintf(buffer, "http://127.0.0.1/jsonrpc?request={\"jsonrpc\":\"2.0\",\"method\":\"Player.PlayPause\",\"params\":{\"playerid\":0},\"id\":1}");
     curl_easy_setopt(curl, CURLOPT_URL, buffer);
    /* Perform the request, res will get the return code */
     res = curl_easy_perform(curl);
     printf("Button pushed!\n");
     delay(1);
 }
 }
return(0);
}
Пример #3
0
int main(int argc, char **argv)
{
	ros::init(argc, argv, "talker");

	ros::NodeHandle n;

	ros::Publisher pub = n.advertise<pi_rotary_encoder::ParamUpdate>("parameter_update", 1000);

	ros::Rate loop_rate(10);

	//int r = wiringPiSetup(); 
	
	int r = wiringPiSetup();
	revokeRoot();
	printf("setup: %d\n", r);
	// wiringPiSetup requires sudo, wiringPiSetupSys requires you add this to your /etc/rc.local:
	//
	// for example, to use pin 23 as an input:
	//
	// gpio export 23 in
	// 
	struct encoder *encoder = setupencoder(4,5);
	long value;

	while (ros::ok())
	{
		updateEncoders();
		long l = encoder->value;
		if (l!=value)
		{
			printf("value: %d\n", (void *)l);
			value = l;
			pi_rotary_encoder::ParamUpdate msg;
			msg.parameter = "shutter_speed";
			msg.value = value*10;
			pub.publish(msg);
		}
		ros::spinOnce();
		loop_rate.sleep();
	}
	
}
void KeyboardInput::setupRotaryEncoder()
{
	wiringPiSetup();
	encoder = setupencoder(4, 5, 6);
}