graphlcd-base/glcddrivers/common.c
changeset 4 df6a40031aa5
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/graphlcd-base/glcddrivers/common.c	Wed Feb 06 17:32:55 2008 +0000
     1.3 @@ -0,0 +1,231 @@
     1.4 +/*
     1.5 + * GraphLCD driver library
     1.6 + *
     1.7 + * common.c  -  various functions
     1.8 + *
     1.9 + * parts were taken from graphlcd plugin for the Video Disc Recorder
    1.10 + * (c) 2001-2004 Carsten Siebholz <c.siebholz AT t-online.de>
    1.11 + *
    1.12 + * This file is released under the GNU General Public License. Refer
    1.13 + * to the COPYING file distributed with this package.
    1.14 + *
    1.15 + * (c) 2004 Andreas Regel <andreas.regel AT powarman.de>
    1.16 + */
    1.17 +
    1.18 +#include <ctype.h>
    1.19 +#include <sched.h>
    1.20 +#include <time.h>
    1.21 +#include <unistd.h>
    1.22 +#include <sys/time.h>
    1.23 +#include <sys/resource.h>
    1.24 +
    1.25 +#include "common.h"
    1.26 +#include "config.h"
    1.27 +
    1.28 +
    1.29 +namespace GLCD
    1.30 +{
    1.31 +
    1.32 +static const int BitReverseTable[256] =
    1.33 +{
    1.34 +    0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
    1.35 +    0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,
    1.36 +    0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8,
    1.37 +    0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8,
    1.38 +    0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4,
    1.39 +    0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4,
    1.40 +    0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec,
    1.41 +    0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc,
    1.42 +    0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2,
    1.43 +    0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2,
    1.44 +    0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea,
    1.45 +    0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa,
    1.46 +    0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6,
    1.47 +    0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6,
    1.48 +    0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee,
    1.49 +    0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe,
    1.50 +    0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1,
    1.51 +    0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1,
    1.52 +    0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9,
    1.53 +    0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9,
    1.54 +    0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5,
    1.55 +    0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5,
    1.56 +    0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed,
    1.57 +    0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd,
    1.58 +    0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3,
    1.59 +    0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3,
    1.60 +    0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb,
    1.61 +    0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb,
    1.62 +    0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7,
    1.63 +    0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7,
    1.64 +    0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef,
    1.65 +    0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff
    1.66 +};
    1.67 +
    1.68 +int nSleepInit()
    1.69 +{
    1.70 +    int ret = 0;
    1.71 +
    1.72 +    if (Config.waitPriority != 0)
    1.73 +    {
    1.74 +        ret = setpriority(PRIO_PROCESS, 0, Config.waitPriority);
    1.75 +    }
    1.76 +
    1.77 +    if (!ret)
    1.78 +    {
    1.79 +        switch (Config.waitMethod)
    1.80 +        {
    1.81 +            case kWaitUsleep: // usleep
    1.82 +                break;
    1.83 +            case kWaitNanosleep: // nanosleep
    1.84 +                break;
    1.85 +            case kWaitNanosleepRR: // nanosleep (sched_rr)
    1.86 +                // set sched algorithm
    1.87 +                struct sched_param param;
    1.88 +
    1.89 +                param.sched_priority=1;
    1.90 +                ret = sched_setscheduler(0, SCHED_RR, &param);
    1.91 +                break;
    1.92 +            case kWaitGettimeofday: // gettimeofday
    1.93 +                break;
    1.94 +        }
    1.95 +    }
    1.96 +    return ret;
    1.97 +}
    1.98 +
    1.99 +int nSleepDeInit()
   1.100 +{
   1.101 +    int ret = 0;
   1.102 +
   1.103 +    if (Config.waitPriority != 0)
   1.104 +    {
   1.105 +        ret = setpriority(PRIO_PROCESS, 0, 0);
   1.106 +    }
   1.107 +
   1.108 +    if (!ret)
   1.109 +    {
   1.110 +        switch (Config.waitMethod)
   1.111 +        {
   1.112 +            case kWaitUsleep: // usleep
   1.113 +                break;
   1.114 +            case kWaitNanosleep: // nanosleep
   1.115 +                break;
   1.116 +            case kWaitNanosleepRR: // nanosleep (sched_rr)
   1.117 +                // set sched algorithm
   1.118 +                struct sched_param param;
   1.119 +
   1.120 +                param.sched_priority=0;
   1.121 +                ret = sched_setscheduler(0, SCHED_OTHER, &param);
   1.122 +                break;
   1.123 +            case kWaitGettimeofday: // gettimeofday
   1.124 +                break;
   1.125 +        }
   1.126 +    }
   1.127 +    return ret;
   1.128 +}
   1.129 +
   1.130 +void nSleep(long ns)
   1.131 +{
   1.132 +    switch (Config.waitMethod)
   1.133 +    {
   1.134 +        case kWaitUsleep: // usleep
   1.135 +            if (ns > 0)
   1.136 +            {
   1.137 +                usleep(std::max(1L, ns/1000));
   1.138 +            }
   1.139 +            break;
   1.140 +        case kWaitNanosleep: // nanosleep
   1.141 +        case kWaitNanosleepRR: // nanosleep(sched_rr)
   1.142 +            struct timespec delay, remaining;
   1.143 +            if (ns > 0)
   1.144 +            {
   1.145 +                delay.tv_sec = ns/1000000000;
   1.146 +                delay.tv_nsec = ns%1000000000;
   1.147 +                while (nanosleep(&delay, &remaining) == -1)
   1.148 +                {
   1.149 +                    delay.tv_sec  = remaining.tv_sec;
   1.150 +                    delay.tv_nsec = remaining.tv_nsec;
   1.151 +                }
   1.152 +            }
   1.153 +            break;
   1.154 +        case kWaitGettimeofday: //getTimeofday for Kernel 2.6
   1.155 +            long us;
   1.156 +            struct timeval tv1, tv2;
   1.157 +            suseconds_t us2;
   1.158 +            if (ns > 0)
   1.159 +            {
   1.160 +                us = ns / 1000;
   1.161 +                if (us == 0)
   1.162 +                    us = 1;
   1.163 +                gettimeofday(&tv1, 0);
   1.164 +                do
   1.165 +                {
   1.166 +                    gettimeofday(&tv2, 0);
   1.167 +                    us2 = tv2.tv_usec + ((tv2.tv_sec - tv1.tv_sec) * 1000000);
   1.168 +                } while (us2 - tv1.tv_usec < us);
   1.169 +            }
   1.170 +    }
   1.171 +}
   1.172 +
   1.173 +void uSleep(long us)
   1.174 +{
   1.175 +    if (Config.waitMethod == kWaitUsleep)
   1.176 +    {
   1.177 +        // usleep
   1.178 +        if (us > 0)
   1.179 +            usleep(std::max(1L, us));
   1.180 +    }
   1.181 +    else
   1.182 +    {
   1.183 +        nSleep(us * 1000);
   1.184 +    }
   1.185 +}
   1.186 +
   1.187 +
   1.188 +
   1.189 +unsigned char ReverseBits(unsigned char value)
   1.190 +{
   1.191 +    return BitReverseTable[value];
   1.192 +}
   1.193 +
   1.194 +void clip(int & value, int min, int max)
   1.195 +{
   1.196 +    if (value < min)
   1.197 +        value = min;
   1.198 +    if (value > max)
   1.199 +        value = max;
   1.200 +}
   1.201 +
   1.202 +void sort(int & value1, int & value2)
   1.203 +{
   1.204 +    if (value2 < value1)
   1.205 +    {
   1.206 +        int tmp;
   1.207 +        tmp = value2;
   1.208 +        value2 = value1;
   1.209 +        value1 = tmp;
   1.210 +    }
   1.211 +}
   1.212 +
   1.213 +std::string trim(const std::string & s)
   1.214 +{
   1.215 +    std::string::size_type start, end;
   1.216 +
   1.217 +    start = 0;
   1.218 +    while (start < s.length())
   1.219 +    {
   1.220 +        if (!isspace(s[start]))
   1.221 +            break;
   1.222 +        start++;
   1.223 +    }
   1.224 +    end = s.length() - 1;
   1.225 +    while (end >= 0)
   1.226 +    {
   1.227 +        if (!isspace(s[end]))
   1.228 +            break;
   1.229 +        end--;
   1.230 +    }
   1.231 +    return s.substr(start, end - start + 1);
   1.232 +}
   1.233 +
   1.234 +} // end of namespace