summaryrefslogtreecommitdiff
path: root/seduthread.h
blob: 38ca6cc89c4f4fc4a058c73907b01f5e57577f94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
 * seduthread.h: A plugin for the Video Disk Recorder
 *
 * See the README file for copyright information and how to reach the author.
 *
 * $Id: seduthread.h,v 1.64 2012/11/28 06:29:24 wendel Exp $
 */

#include <termios.h>
#include <queue>

#include <vdr/thread.h>

#include "common.h"
#include "config.h"

//***************************************************************************
// Pixel - format as provided by softhddevice 
//***************************************************************************

struct Pixel
{
   unsigned char b;
   unsigned char g;
   unsigned char r;
   unsigned char a;
};

class PixQueue
{
   public:

      PixQueue()            { clear(); }
      virtual ~PixQueue()   { clear(); }
      void clear()          { while (!pQueue.empty()) pQueue.pop(); r=g=b=0; }
      int getCount()        { return pQueue.size(); }

      void push(Pixel* p) 
      { 
         pQueue.push(*p);
 
         r += p->r;
         g += p->g;
         b += p->b;        

         // remove old entrys from queue and sum

         while (pQueue.size() > cfg.average)
         {
            r -= pQueue.front().r;
            g -= pQueue.front().g;
            b -= pQueue.front().b;
            
            pQueue.pop();
         }
      }

      void getPixel(Pixel* p)
      {
         p->r = r / pQueue.size();
         p->g = g / pQueue.size();
         p->b = b / pQueue.size();
      }
      
   protected:

      std::queue <Pixel> pQueue;
      unsigned int r;
      unsigned int g;
      unsigned int b;
};

class PixSum
{
   public:

      PixSum()       { clear(); }

      void clear()   { r=g=b=count=0;  }
      int getCount() { return count; }

      void add(Pixel* p)
      {
         r += p->r;
         g += p->g;
         b += p->b;
         count++;
      }

      void getAvg(Pixel* p)
      {
         p->r = r / count;
         p->g = g / count;
         p->b = b / count;
      }

   protected:
      
      unsigned int r;
      unsigned int g;
      unsigned int b;
      int count;
};

//***************************************************************************
// cSeduLine - Serial Communication
//***************************************************************************

class cSeduLine : public cSeduService
{
   public:

      cSeduLine();
      ~cSeduLine()  { close(); }

      int open();
      int close();
      int isOpen()  { return fd != na; }

      int checkLine();
      int read();

      int write(unsigned char b);

      int writeStartSeq();
      int writeEndSeq();
      int writePix(Pixel* p);
      int writeColor(Pixel* p, int index);

      // set ..

      void setMode(SeduMode aMode, int channels = 0);

   private:

      int detect();
   
      // data

      int dataBytesSend;

      int mode;
      int dataBytes;
      int byteMode;
      int byteStart;
      int byteEnd;

      char* deviceName;
      int fd;
      struct termios oldtio;
};

//***************************************************************************
// SEDU Thread
//***************************************************************************

class cSeduThread : public cThread, public cSeduService
{
   public:

      cSeduThread();
      ~cSeduThread();

      int isRunning() { return Running(); }

      // interface

      void Stop();
     
   private:

      void Action(void);

      int grabImage();
      int detectCineBars();
      int putData();

      void threshold(Pixel* p);
      void whiteAdj(Pixel* p);
      void gammaAdj(Pixel* p);
      void merge(Pixel* p1, Pixel* p2, int level = 100);

      int getPixel(int ledIdx, Pixel* pixel);
      int isBlack(Pixel* p) { return p->r < cfg.threshold && p->g < cfg.threshold &&  p->b < cfg.threshold; }

      // data

      cSeduLine sedu;

      cMutex mutex;
      cCondVar waitCondition;
      int loopActive;

      Pixel* image;
      PixQueue* pixAverage;
      int cineBarsHor;
      int cineBarsVer;
      int imageSize;
      int imageWidth;
      int imageHeight;

      Pixel getRainbowColor();
      Pixel hsv2Rgb(int h, double s, double v);
};