Zoltan Hudak / mbedPi
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers wait.cpp Source File

wait.cpp

00001 #include "wait.h"
00002 
00003 /**
00004  * @brief
00005  * @note
00006  * @param
00007  * @retval
00008  */
00009 void wait(float s)
00010 {
00011     unistd::usleep(s * 1000 * 1000);
00012 }
00013 
00014 /**
00015  * @brief
00016  * @note
00017  * @param
00018  * @retval
00019  */
00020 void wait_ms(int ms)
00021 {
00022     unistd::usleep(ms * 1000);
00023 }
00024 
00025 /**
00026  * @brief
00027  * @note
00028  * @param
00029  * @retval
00030  */
00031 void wait_us(int us)
00032 {
00033     if (us > 100) {
00034         struct timespec tim, tim2;
00035         tim.tv_sec = 0;
00036         tim.tv_nsec = us * 1000;
00037 
00038         if (nanosleep(&tim, &tim2) < 0) {
00039             fprintf(stderr, "Nano sleep system call failed \n");
00040             exit(1);
00041         }
00042     }
00043     else {
00044         struct timeval  tNow, tLong, tEnd;
00045 
00046         gettimeofday(&tNow, NULL);
00047         tLong.tv_sec = us / 1000000;
00048         tLong.tv_usec = us % 1000000;
00049         timeradd(&tNow, &tLong, &tEnd);
00050 
00051         while (timercmp(&tNow, &tEnd, < ))
00052             gettimeofday(&tNow, NULL);
00053     }
00054 }
00055 
00056 /**
00057  * @brief
00058  * @note
00059  * @param
00060  * @retval
00061  */
00062 uint64_t millis()
00063 {
00064     return bcm2835_systimer_read() / 1000;
00065 }
00066 
00067 /**
00068  * @brief
00069  * @note
00070  * @param
00071  * @retval
00072  */
00073 void bcm2835_delay(unsigned int millis)
00074 {
00075     struct timespec sleeper;
00076 
00077     sleeper.tv_sec = (time_t) (millis / 1000);
00078     sleeper.tv_nsec = (long)(millis % 1000) * 1000000;
00079     nanosleep(&sleeper, NULL);
00080 }