Add exitThread() polling the exit button (#888)
* Add exitThread() polling the exit button * Add missing includes * Remove redundant code
This commit is contained in:
		
				
					committed by
					
						
						Peli de Halleux
					
				
			
			
				
	
			
			
			
						parent
						
							e511630c2e
						
					
				
				
					commit
					8b3461bebd
				
			@@ -159,11 +159,6 @@ namespace brick {
 | 
				
			|||||||
            if (sl[i])
 | 
					            if (sl[i])
 | 
				
			||||||
                ret |= 1 << i
 | 
					                ret |= 1 << i
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        // this needs to be done in query(), which is run without the main JS execution mutex
 | 
					 | 
				
			||||||
        // otherwise, while(true){} will lock the device
 | 
					 | 
				
			||||||
        if (ret & DAL.BUTTON_ID_ESCAPE) {
 | 
					 | 
				
			||||||
            control.reset()
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        return ret
 | 
					        return ret
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -14,6 +14,8 @@
 | 
				
			|||||||
#include <errno.h>
 | 
					#include <errno.h>
 | 
				
			||||||
#include <fcntl.h>
 | 
					#include <fcntl.h>
 | 
				
			||||||
#include <malloc.h>
 | 
					#include <malloc.h>
 | 
				
			||||||
 | 
					#include <sys/mman.h>
 | 
				
			||||||
 | 
					#include <sys/ioctl.h>
 | 
				
			||||||
#include "ev3const.h"
 | 
					#include "ev3const.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#define THREAD_DBG(...)
 | 
					#define THREAD_DBG(...)
 | 
				
			||||||
@@ -145,6 +147,29 @@ static void startUsb() {
 | 
				
			|||||||
    pthread_detach(pid);
 | 
					    pthread_detach(pid);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static void *exitThread(void *) {
 | 
				
			||||||
 | 
					    int fd = open("/dev/lms_ui", O_RDWR, 0666);
 | 
				
			||||||
 | 
					    if (fd < 0)
 | 
				
			||||||
 | 
					        return 0;
 | 
				
			||||||
 | 
					    uint8_t *data =
 | 
				
			||||||
 | 
					        (uint8_t *)mmap(NULL, NUM_BUTTONS, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
 | 
				
			||||||
 | 
					    if (data == MAP_FAILED) {
 | 
				
			||||||
 | 
					        close(fd);
 | 
				
			||||||
 | 
					        return 0;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    for (;;) {
 | 
				
			||||||
 | 
					        if (data[5])
 | 
				
			||||||
 | 
					            target_reset();
 | 
				
			||||||
 | 
					        sleep_core_us(50000);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static void startExitThread() {
 | 
				
			||||||
 | 
					    pthread_t pid;
 | 
				
			||||||
 | 
					    pthread_create(&pid, NULL, exitThread, NULL);
 | 
				
			||||||
 | 
					    pthread_detach(pid);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void sendUsb(uint16_t code, const char *data, int len) {
 | 
					void sendUsb(uint16_t code, const char *data, int len) {
 | 
				
			||||||
    while (len > 0) {
 | 
					    while (len > 0) {
 | 
				
			||||||
        int sz = len;
 | 
					        int sz = len;
 | 
				
			||||||
@@ -490,14 +515,14 @@ void runLMS() {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void stopMotors() {
 | 
					void stopMotors() {
 | 
				
			||||||
    uint8_t cmd[3] = { opOutputStop, 0x0F, 0 };
 | 
					    uint8_t cmd[3] = {opOutputStop, 0x0F, 0};
 | 
				
			||||||
    int fd = open("/dev/lms_pwm", O_RDWR);
 | 
					    int fd = open("/dev/lms_pwm", O_RDWR);
 | 
				
			||||||
    write(fd, cmd, 3);
 | 
					    write(fd, cmd, 3);
 | 
				
			||||||
    close(fd);
 | 
					    close(fd);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void stopProgram() {
 | 
					void stopProgram() {
 | 
				
			||||||
    uint8_t cmd[1] = { opOutputProgramStop };
 | 
					    uint8_t cmd[1] = {opOutputProgramStop};
 | 
				
			||||||
    int fd = open("/dev/lms_pwm", O_RDWR);
 | 
					    int fd = open("/dev/lms_pwm", O_RDWR);
 | 
				
			||||||
    write(fd, cmd, 1);
 | 
					    write(fd, cmd, 1);
 | 
				
			||||||
    close(fd);
 | 
					    close(fd);
 | 
				
			||||||
@@ -519,6 +544,7 @@ void initRuntime() {
 | 
				
			|||||||
    DMESG("runtime starting...");
 | 
					    DMESG("runtime starting...");
 | 
				
			||||||
    stopLMS();
 | 
					    stopLMS();
 | 
				
			||||||
    startUsb();
 | 
					    startUsb();
 | 
				
			||||||
 | 
					    startExitThread();
 | 
				
			||||||
    pthread_t disp;
 | 
					    pthread_t disp;
 | 
				
			||||||
    pthread_create(&disp, NULL, evtDispatcher, NULL);
 | 
					    pthread_create(&disp, NULL, evtDispatcher, NULL);
 | 
				
			||||||
    pthread_detach(disp);
 | 
					    pthread_detach(disp);
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user