コード例 #1
0
ファイル: hueSample.cpp プロジェクト: alanswx/afLib
void onAttributeSet_callback(const uint8_t requestId, const uint16_t attributeId, const uint16_t valueLen, const uint8_t *value)
{
  HttpRequest *request = NULL;
  char hue_request[1024];
  fprintf(stdout,"onAttributeSet_callback: %d len: %d %x %s\n",attributeId,valueLen,*value,value);
  switch (attributeId)
  {
fprintf(stderr,"BUTTON PRESSED!\n");
break;
        case AF_LIGHT1:
          fprintf(stdout,"AF_LIGHT1 == %d\n",*value);
          sprintf(hue_request,"%slights/1/state",hue_prefix);
          if (*value)
  request = new HttpRequest(hue_request,"{ \"on\": true, \"hue\": 25500, \"sat\": 100, \"bri\": 100 }");
          else
  request = new HttpRequest(hue_request,"{ \"on\": false, \"hue\": 25500, \"sat\": 100, \"bri\": 100 }");

 http.AddRequest(request);

        break;
        case AF_LIGHT2:
          fprintf(stdout,"AF_LIGHT2 == %d\n",*value);
          sprintf(hue_request,"%slights/2/state",hue_prefix);
          if (*value)
  request = new HttpRequest(hue_request,"{ \"on\": true, \"hue\": 25500, \"sat\": 100, \"bri\": 100 }");
          else
  request = new HttpRequest(hue_request,"{ \"on\": false, \"hue\": 25500, \"sat\": 100, \"bri\": 100 }");

 http.AddRequest(request);

        break;
        case AF_LIGHT3:
          fprintf(stdout,"AF_LIGHT3 == %d\n",*value);
          sprintf(hue_request,"%slights/3/state",hue_prefix);
fprintf(stderr,"LIGHT 3 CHANGE STATE: %s\n",hue_request);
          if (*value)
  request = new HttpRequest(hue_request,"{ \"on\": true, \"hue\": 25500, \"sat\": 100, \"bri\": 100 }");
          else
  request = new HttpRequest(hue_request,"{ \"on\": false, \"hue\": 25500, \"sat\": 100, \"bri\": 100 }");

 http.AddRequest(request);

        break;
#if 0
        case AF_LIGHT1BRI:
        {
          uint16_t *light3Value= (uint16_t *) value;
          uint8_t *light3Value2= (uint8_t *) value;
          sprintf(hue_request,"%slights/1/state",hue_prefix);
          fprintf(stdout,"AF_LIGHT3 == %d %d %d\n",*value,*light3Value,*light3Value2);
          char command[512];
          
            sprintf(command, "{ \"on\": true, \"hue\": 25500, \"sat\": 100, \"bri\": %d }",*light3Value2);
fprintf(stderr,"!!!! [%s]\n",command);
  		request = new HttpRequest(hue_request,(const char *)command);
               http.AddRequest(request);

        }
        case AF_LIGHT2BRI:
        {
          uint16_t *light3Value= (uint16_t *) value;
          uint8_t *light3Value2= (uint8_t *) value;
          sprintf(hue_request,"%slights/2/state",hue_prefix);
          fprintf(stdout,"AF_LIGHT3 == %d %d %d\n",*value,*light3Value,*light3Value2);
          char command[512];
          
            sprintf(command, "{ \"on\": true, \"hue\": 25500, \"sat\": 100, \"bri\": %d }",*light3Value2);
fprintf(stderr,"!!!! [%s]\n",command);
  		request = new HttpRequest(hue_request,(const char *)command);
               http.AddRequest(request);

        }
#endif
        case AF_LIGHT3BRI:
        {
          uint16_t *light3Value= (uint16_t *) value;
          uint8_t *light3Value2= (uint8_t *) value;
          sprintf(hue_request,"%slights/3/state",hue_prefix);
          fprintf(stdout,"AF_LIGHT3 == %d %d %d\n",*value,*light3Value,*light3Value2);
          char command[512];
          
            sprintf(command, "{ \"on\": true, \"hue\": 25500, \"sat\": 100, \"bri\": %d }",*light3Value2);
fprintf(stderr,"!!!! [%s]\n",command);
  		request = new HttpRequest(hue_request,(const char *)command);
               http.AddRequest(request);

        }
        break;
	case AF_LIGHT1LABEL:
          {
          char *light1Label= (char *) value;
          fprintf(stdout,"bAF_LIGHT1LABEL == %s %d\n",light1Label);
          }

	case AF_LIGHT2LABEL:
          {
          char *light2Label= (char *) value;
          fprintf(stdout,"bAF_LIGHT2LABEL == %s %d\n",light2Label);
          }

	case AF_LIGHT3LABEL:
          {
          char *light3Label= (char *) value;
          fprintf(stdout,"bAF_LIGHT3LABEL == %s %d\n",light3Label);
          }
        
  }
    if (theLib->setAttributeComplete(requestId, attributeId, valueLen, value) != afSUCCESS) {
        fprintf(stderr,"setAttributeComplete failed!");
    }

  
}
コード例 #2
0
ファイル: hueSample.cpp プロジェクト: alanswx/afLib
int main(int argc, char *argv[])
{

  HueLightResponse *hueLightResponder= NULL;
  HueIPResponse *IPAddr= new HueIPResponse("https://www.meethue.com/api/nupnp");
  http.AddRequest(IPAddr);

  int state = HUE_STATE_NEED_IP;

        struct pollfd fdset[2];
        int nfds = 2;
        int gpio_fd, timeout, rc;
        char *buf[MAX_BUF];
        unsigned int interrupt_line= 17;/*17;*/
        unsigned int reset_line= 4;
        int len;
        int counter=0;

	Stream *theLog = new linuxLog();
        afSPI *theSPI = new linuxSPI();

        gpio_export(interrupt_line);
        gpio_set_dir(interrupt_line, 0);
        gpio_set_edge(interrupt_line, "falling");
        gpio_fd = gpio_fd_open(interrupt_line);

        timeout = POLL_TIMEOUT;

        fprintf(stdout,"Test\n");

        theLib = iafLib::create(0,isr_callback,onAttributeSet_callback,onAttributeSetComplete_callback,theLog,theSPI);
        theLib->mcuISR();

/* we need to hook up and use the reset line */
        gpio_export(reset_line);
        gpio_set_dir(reset_line, 1);
	gpio_set_value(reset_line,0);
    
        timespec sleep_time;
        timespec remaining;
        sleep_time.tv_sec=0;
        sleep_time.tv_nsec=250000;
        nanosleep(&sleep_time,&remaining);
         /* check for E_INTR? and call again? */
	gpio_set_value(reset_line,1);

        while (1) {
                counter++;
                memset((void*)fdset, 0, sizeof(fdset));

                fdset[0].fd = STDIN_FILENO;
                fdset[0].events = POLLIN;

                fdset[1].fd = gpio_fd;
                fdset[1].events = POLLPRI;

                lseek(gpio_fd, 0, SEEK_SET);    /* consume any prior interrupt */
                read(gpio_fd, buf, sizeof (buf));


                rc = poll(fdset, nfds, timeout);

                if (rc < 0) {
                        printf("\npoll() failed!\n");
                        return -1;
                }

                if (rc == 0) {
                        printf(".");
                }

                if (fdset[1].revents & POLLPRI) {
                        len = read(fdset[1].fd, buf, MAX_BUF);
                        printf("\npoll() GPIO %d interrupt occurred\n", interrupt_line);
                lseek(gpio_fd, 0, SEEK_SET);    /* consume interrupt */
                read(gpio_fd, buf, sizeof (buf));

                        theLib->mcuISR();
                }

                if (fdset[0].revents & POLLIN) {
                        (void)read(fdset[0].fd, buf, 1);
                        //printf("\npoll() stdin read 0x%2.2X\n", (unsigned int) buf[0]);
                }



		switch (state)
		{
		   case HUE_STATE_NEED_IP:
			// we need to check and see if we have an IP, then we can switch
  			if (IPAddr->completed())
{
                          state = HUE_STATE_HAS_IP;
                          IPAddr->get_ip(hue_ip);
                        fprintf(stderr,"IP: %s\n",hue_ip);
			sprintf(hue_prefix,"http://%s/api/huelibrary/",hue_ip);
                        state = HUE_STATE_HAS_IP;
}
			break;
		   case HUE_STATE_HAS_IP:

                // every so many loops check and start the lights query to update the lights..
		if (counter % 100 == 0)
                {
                   //check to see how many HTTP requests are outstanding
                   if (hueLightResponder == NULL)
                   { 
printf("****initializing new request to get the lights\n");
                      char hue_request[1024];
                      sprintf(hue_request,"%slights",hue_prefix);
                      hueLightResponder =  new HueLightResponse(hue_request,theLib);
                      http.AddRequest(hueLightResponder);
                   }
                   else if (hueLightResponder->completed())
                   {
                      delete hueLightResponder;
                      char hue_request[1024];
                      sprintf(hue_request,"%slights",hue_prefix);
                      hueLightResponder =  new HueLightResponse(hue_request,theLib);
                      http.AddRequest(hueLightResponder);
                   }
                  

                }

                }

  	http.Update();
  	http.Status();
        theLib->loop();
        fflush(stdout);
        }

    gpio_fd_close(gpio_fd);
   return 0;
}