|  |  | @ -3,6 +3,8 @@ | 
			
		
	
		
		
			
				
					
					|  |  |  | #include <math.h> |  |  |  | #include <math.h> | 
			
		
	
		
		
			
				
					
					|  |  |  | #include <sys/resource.h> |  |  |  | #include <sys/resource.h> | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | #include <algorithm> | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | #include "common/util.h" |  |  |  | #include "common/util.h" | 
			
		
	
		
		
			
				
					
					|  |  |  | #include "common/utilpp.h" |  |  |  | #include "common/utilpp.h" | 
			
		
	
		
		
			
				
					
					|  |  |  | #include "common/params.h" |  |  |  | #include "common/params.h" | 
			
		
	
	
		
		
			
				
					|  |  | @ -13,65 +15,11 @@ | 
			
		
	
		
		
			
				
					
					|  |  |  | #include "paint.hpp" |  |  |  | #include "paint.hpp" | 
			
		
	
		
		
			
				
					
					|  |  |  | #include "android/sl_sound.hpp" |  |  |  | #include "android/sl_sound.hpp" | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | // Includes for light sensor
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | #include <cutils/properties.h> |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | #include <hardware/sensors.h> |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | #include <utils/Timers.h> |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | volatile sig_atomic_t do_exit = 0; |  |  |  | volatile sig_atomic_t do_exit = 0; | 
			
		
	
		
		
			
				
					
					|  |  |  | static void set_do_exit(int sig) { |  |  |  | static void set_do_exit(int sig) { | 
			
		
	
		
		
			
				
					
					|  |  |  |   do_exit = 1; |  |  |  |   do_exit = 1; | 
			
		
	
		
		
			
				
					
					|  |  |  | } |  |  |  | } | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | static void* light_sensor_thread(void *args) { |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |   set_thread_name("light_sensor"); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |   int err; |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |   UIState *s = (UIState*)args; |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |   s->light_sensor = 0.0; |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |   struct sensors_poll_device_t* device; |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |   struct sensors_module_t* module; |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |   hw_get_module(SENSORS_HARDWARE_MODULE_ID, (hw_module_t const**)&module); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |   sensors_open(&module->common, &device); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |   // need to do this
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |   struct sensor_t const* list; |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |   module->get_sensors_list(module, &list); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |   int SENSOR_LIGHT = 7; |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |   err = device->activate(device, SENSOR_LIGHT, 0); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |   if (err != 0) goto fail; |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |   err = device->activate(device, SENSOR_LIGHT, 1); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |   if (err != 0) goto fail; |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |   device->setDelay(device, SENSOR_LIGHT, ms2ns(100)); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |   while (!do_exit) { |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |     static const size_t numEvents = 1; |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |     sensors_event_t buffer[numEvents]; |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |     int n = device->poll(device, buffer, numEvents); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |     if (n < 0) { |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |       LOG_100("light_sensor_poll failed: %d", n); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |     } |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |     if (n > 0) { |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |       s->light_sensor = buffer[0].light; |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |     } |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |   } |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |   sensors_close(device); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |   return NULL; |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | fail: |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |   LOGE("LIGHT SENSOR IS MISSING"); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |   s->light_sensor = 255; |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |   return NULL; |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | } |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | static void ui_set_brightness(UIState *s, int brightness) { |  |  |  | static void ui_set_brightness(UIState *s, int brightness) { | 
			
		
	
		
		
			
				
					
					|  |  |  |   static int last_brightness = -1; |  |  |  |   static int last_brightness = -1; | 
			
		
	
		
		
			
				
					
					|  |  |  |   if (last_brightness != brightness && (s->awake || brightness == 0)) { |  |  |  |   if (last_brightness != brightness && (s->awake || brightness == 0)) { | 
			
		
	
	
		
		
			
				
					|  |  | @ -92,24 +40,35 @@ static void enable_event_processing(bool yes) { | 
			
		
	
		
		
			
				
					
					|  |  |  |   } |  |  |  |   } | 
			
		
	
		
		
			
				
					
					|  |  |  | } |  |  |  | } | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | static void set_awake(UIState *s, bool awake) { |  |  |  | // TODO: implement double tap to wake and actually turn display off
 | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |   if (awake) { |  |  |  | static void handle_display_state(UIState *s, bool user_input) { | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |     // 30 second timeout
 |  |  |  | 
 | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |     s->awake_timeout = 30*UI_FREQ; |  |  |  |   static int display_mode = HWC_POWER_MODE_OFF; | 
			
				
				
			
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |   static int display_timeout = 0; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |   // determine desired state
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |   int desired_mode = display_mode; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |   if (user_input || s->ignition || s->started) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     desired_mode = HWC_POWER_MODE_NORMAL; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     display_timeout = 30*UI_FREQ; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |   } else { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     display_timeout = std::max(display_timeout-1, 0); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     if (display_timeout == 0) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |       desired_mode = HWC_POWER_MODE_OFF; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     } | 
			
		
	
		
		
			
				
					
					|  |  |  |   } |  |  |  |   } | 
			
		
	
		
		
			
				
					
					|  |  |  |   if (s->awake != awake) { |  |  |  | 
 | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |     s->awake = awake; |  |  |  |   // handle state transition
 | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |   if (display_mode != desired_mode) { | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |     // TODO: replace command_awake and command_sleep with direct calls to android
 |  |  |  |     LOGW("setting display mode %d", desired_mode); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |     if (awake) { |  |  |  | 
 | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |       LOGW("awake normal"); |  |  |  |     display_mode = desired_mode; | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |       framebuffer_set_power(s->fb, HWC_POWER_MODE_NORMAL); |  |  |  |     s->awake = display_mode == HWC_POWER_MODE_NORMAL; | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |       enable_event_processing(true); |  |  |  |     framebuffer_set_power(s->fb, display_mode); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |     } else { |  |  |  |     enable_event_processing(s->awake); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |       LOGW("awake off"); |  |  |  | 
 | 
			
				
				
			
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     if (!s->awake) { | 
			
		
	
		
		
			
				
					
					|  |  |  |       ui_set_brightness(s, 0); |  |  |  |       ui_set_brightness(s, 0); | 
			
		
	
		
		
			
				
					
					|  |  |  |       framebuffer_set_power(s->fb, HWC_POWER_MODE_OFF); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |       enable_event_processing(false); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |     } |  |  |  |     } | 
			
		
	
		
		
			
				
					
					|  |  |  |   } |  |  |  |   } | 
			
		
	
		
		
			
				
					
					|  |  |  | } |  |  |  | } | 
			
		
	
	
		
		
			
				
					|  |  | @ -161,9 +120,7 @@ static void update_offroad_layout_state(UIState *s, PubMaster *pm) { | 
			
		
	
		
		
			
				
					
					|  |  |  | } |  |  |  | } | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | int main(int argc, char* argv[]) { |  |  |  | int main(int argc, char* argv[]) { | 
			
		
	
		
		
			
				
					
					|  |  |  |   int err; |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |   setpriority(PRIO_PROCESS, 0, -14); |  |  |  |   setpriority(PRIO_PROCESS, 0, -14); | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |   signal(SIGINT, (sighandler_t)set_do_exit); |  |  |  |   signal(SIGINT, (sighandler_t)set_do_exit); | 
			
		
	
		
		
			
				
					
					|  |  |  |   SLSound sound; |  |  |  |   SLSound sound; | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
	
		
		
			
				
					|  |  | @ -172,14 +129,10 @@ int main(int argc, char* argv[]) { | 
			
		
	
		
		
			
				
					
					|  |  |  |   ui_init(s); |  |  |  |   ui_init(s); | 
			
		
	
		
		
			
				
					
					|  |  |  |   s->sound = &sound; |  |  |  |   s->sound = &sound; | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |   set_awake(s, true); |  |  |  |   handle_display_state(s, true); | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |   enable_event_processing(true); |  |  |  |   enable_event_processing(true); | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |   PubMaster *pm = new PubMaster({"offroadLayout"}); |  |  |  |   PubMaster *pm = new PubMaster({"offroadLayout"}); | 
			
		
	
		
		
			
				
					
					|  |  |  |   pthread_t light_sensor_thread_handle; |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |   err = pthread_create(&light_sensor_thread_handle, NULL, |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |                        light_sensor_thread, s); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |   assert(err == 0); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |   TouchState touch = {0}; |  |  |  |   TouchState touch = {0}; | 
			
		
	
		
		
			
				
					
					|  |  |  |   touch_init(&touch); |  |  |  |   touch_init(&touch); | 
			
		
	
	
		
		
			
				
					|  |  | @ -215,23 +168,12 @@ int main(int argc, char* argv[]) { | 
			
		
	
		
		
			
				
					
					|  |  |  |     int touch_x = -1, touch_y = -1; |  |  |  |     int touch_x = -1, touch_y = -1; | 
			
		
	
		
		
			
				
					
					|  |  |  |     int touched = touch_poll(&touch, &touch_x, &touch_y, 0); |  |  |  |     int touched = touch_poll(&touch, &touch_x, &touch_y, 0); | 
			
		
	
		
		
			
				
					
					|  |  |  |     if (touched == 1) { |  |  |  |     if (touched == 1) { | 
			
		
	
		
		
			
				
					
					|  |  |  |       set_awake(s, true); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |       handle_sidebar_touch(s, touch_x, touch_y); |  |  |  |       handle_sidebar_touch(s, touch_x, touch_y); | 
			
		
	
		
		
			
				
					
					|  |  |  |       handle_vision_touch(s, touch_x, touch_y); |  |  |  |       handle_vision_touch(s, touch_x, touch_y); | 
			
		
	
		
		
			
				
					
					|  |  |  |     } |  |  |  |     } | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |     // manage wakefulness
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |     if (s->started || s->ignition) { |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |       set_awake(s, true); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |     } |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |     if (s->awake_timeout > 0) { |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |       s->awake_timeout--; |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |     } else { |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |       set_awake(s, false); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |     } |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |     // Don't waste resources on drawing in case screen is off
 |  |  |  |     // Don't waste resources on drawing in case screen is off
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     handle_display_state(s, touched == 1); | 
			
		
	
		
		
			
				
					
					|  |  |  |     if (!s->awake) { |  |  |  |     if (!s->awake) { | 
			
		
	
		
		
			
				
					
					|  |  |  |       continue; |  |  |  |       continue; | 
			
		
	
		
		
			
				
					
					|  |  |  |     } |  |  |  |     } | 
			
		
	
	
		
		
			
				
					|  |  | @ -255,10 +197,7 @@ int main(int argc, char* argv[]) { | 
			
		
	
		
		
			
				
					
					|  |  |  |     framebuffer_swap(s->fb); |  |  |  |     framebuffer_swap(s->fb); | 
			
		
	
		
		
			
				
					
					|  |  |  |   } |  |  |  |   } | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |   set_awake(s, true); |  |  |  |   handle_display_state(s, true); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |   err = pthread_join(light_sensor_thread_handle, NULL); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |   assert(err == 0); |  |  |  |  | 
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |   delete s->sm; |  |  |  |   delete s->sm; | 
			
		
	
		
		
			
				
					
					|  |  |  |   delete pm; |  |  |  |   delete pm; | 
			
		
	
		
		
			
				
					
					|  |  |  |   return 0; |  |  |  |   return 0; | 
			
		
	
	
		
		
			
				
					|  |  | 
 |