Example #1
0
int not_main(int argc, char *argv[])
{
  Http http;

//  HttpRequest *request1 = new HttpRequest("http://www.google.com");
  //http.AddRequest(request1);

//  HueLightResponse *request2 = new HueLightResponse("http://10.0.2.227/api/huelibrary/lights");
 // http.AddRequest(request2);

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

//  HttpRequest *request3 = new HueLightResponse("http://10.0.2.227/api/huelibrary/lights/1/state","{ \"on\": false, \"hue\": 25500, \"sat\": 100, \"bri\": 100 }");
 // http.AddRequest(request3);

  //HttpRequest *request = new HttpRequest("http://10.0.2.227/api/huelibrary/lights/3/state","{ \"on\": true, \"hue\": 25500, \"sat\": 100, \"bri\": 100 }");

//  http.AddRequest("http://www.google.com");
//  http.AddRequest("http://10.0.2.227/api/huelibrary/lights/3/state","{ \"on\": true, \"hue\": 25500, \"sat\": 100, \"bri\": 100 }");
  //http.AddRequest("http://10.0.2.227/");
 // http.AddRequest("http://10.0.2.227/api/huelibrary/lights");

  while (1)
  {
fprintf(stderr,"inside loop\n");
  // In some update loop called each frame
  http.Update();
  http.Status();
   sleep(1);
  }

 return 0;
}
Example #2
0
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;
}