From: Antonio Ospite Date: Wed, 7 May 2014 15:46:50 +0000 (+0200) Subject: am7xxx: add a msleep() implementation in tools.[ch] X-Git-Tag: v0.1.5~10^2~4 X-Git-Url: https://git.ao2.it/libam7xxx.git/commitdiff_plain/2aaeac65529d1bd4a030c3c15728143c66b141dd?ds=sidebyside;hp=2d1f8c13ccce07e38c0dd08e28323f2dcbd14bc8 am7xxx: add a msleep() implementation in tools.[ch] --- diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 0817374..f743b1a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,10 +1,11 @@ add_definitions("-D_BSD_SOURCE") # for htole32() +add_definitions("-D_POSIX_C_SOURCE=199309L") # for nanosleep() # Find packages needed to build library find_package(libusb-1.0 REQUIRED) include_directories(${LIBUSB_1_INCLUDE_DIRS}) -set(SRC am7xxx.c serialize.c) +set(SRC am7xxx.c serialize.c tools.c) # Build the library add_library(am7xxx SHARED ${SRC}) diff --git a/src/tools.c b/src/tools.c new file mode 100644 index 0000000..be63ac6 --- /dev/null +++ b/src/tools.c @@ -0,0 +1,48 @@ +/* am7xxx - communication with AM7xxx based USB Pico Projectors and DPFs + * + * Copyright (C) 2014 Antonio Ospite + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include + +#include "tools.h" + +/** + * Sleep for a period expressed in milliseconds + * + * @param[in] msecs Time to sleep in milliseconds + * + * @return 0 on success, -1 on error + */ +int msleep(unsigned long msecs) +{ + struct timespec delay; + int ret; + + delay.tv_sec = msecs / 1000; + delay.tv_nsec = (msecs % 1000) * 1000000; + while (1) { + ret = nanosleep(&delay, &delay); + if (ret == -1 && errno == EINTR) + continue; + break; + } + if (ret == -1) + return ret; + + return 0; +} diff --git a/src/tools.h b/src/tools.h new file mode 100644 index 0000000..d3140c8 --- /dev/null +++ b/src/tools.h @@ -0,0 +1,24 @@ +/* am7xxx - communication with AM7xxx based USB Pico Projectors and DPFs + * + * Copyright (C) 2014 Antonio Ospite + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef __TOOLS_H +#define __TOOLS_H + +int msleep(unsigned long msecs); + +#endif /* __TOOLS_H */