clicpix2_pixels.hpp 6.59 KB
Newer Older
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
205
// Defines CLICpix2 pixels types

#ifndef CLICPIX2_PIXELS_HPP
#define CLICPIX2_PIXELS_HPP

#include <ostream>
#include "datatypes.hpp"

namespace caribou {

    // Basic pixel class
    // The information is internally stored in the same way, the chip stores it, as
    // a 14bit register.
    //
    // The individual values are set via the member functions of a specialized classes
    class clicpix2_pixel : public virtual pixel {
    public:
        virtual ~clicpix2_pixel(){};

        // direct latch access
        void setLatches(uint16_t latches) { m_latches = latches; }

        // set a dedicated bit of the latch
        void setLatches(bool bit, uint8_t idx) {
            if(bit) {
                m_latches |= static_cast<uint16_t>(1 << idx);
            } else {
                m_latches &= static_cast<uint16_t>(~(1 << idx));
            }
        }

        /* Member function to return single bit of the latches.
         */
        bool GetBit(uint8_t bit) { return ((m_latches >> bit) & 0x1); }

        uint16_t GetLatches() const { return m_latches; }

        /** Overloaded comparison operators
         */
        bool operator==(const clicpix2_pixel& rhs) const {
            if(this->GetLatches() == rhs.GetLatches()) {
                return true;
            }
            return false;
        }

        bool operator!=(const clicpix2_pixel& rhs) const { return !(*this == rhs); }

    protected:
        clicpix2_pixel(){};
        clicpix2_pixel(uint16_t latches) : m_latches(latches){};
        uint16_t m_latches;
    };

    /* CLICpix2 pixel configuration class
     *
     * Class to hold all information required to fully configure one pixel.
     * The information is internally stored in the same way, the chip stores it, as
     * a 14bit register. The individual values are set via the member functions
     * and can be retrieved bitwise for convenience.
     */
    class pixelConfig : public virtual clicpix2_pixel {
    public:
        /* Default constructor
         *
         * Initializes the pixel in a masked state
         */
        pixelConfig() : clicpix2_pixel(0x2000){};
        pixelConfig(bool mask, uint8_t threshold, bool cntmode, bool tpenable, bool longcnt) : pixelConfig() {
            SetMask(mask);
            SetThreshold(threshold);
            SetCountingMode(cntmode);
            EnableTestpulse(tpenable);
            LongCounter(longcnt);
        };

        /* Mask setting of the pixel
         */
        void SetMask(bool mask) {
            if(mask) {
                m_latches |= (1 << 13);
            } else {
                m_latches &= static_cast<uint16_t>(~(1 << 13));
            }
        }
        bool GetMask() const { return (m_latches >> 13) & 0x1; }

        /* Individual threshold adjustment (4bit)
         */
        void SetThreshold(uint8_t thr_adjust) {
            m_latches = (m_latches & 0xf0ff) | static_cast<uint16_t>((thr_adjust & 0x0f) << 8);
        }
        uint8_t GetThreshold() const { return (m_latches >> 8) & 0x0f; }

        /* Enable/disable counting mode
         */
        void SetCountingMode(bool cntmo) {
            if(cntmo) {
                m_latches |= (1 << 3);
            } else {
                m_latches &= static_cast<uint16_t>(~(1 << 3));
            }
        }
        bool GetCountingMode() const { return (m_latches >> 3) & 0x1; }

        /* Enable/disable testpulse circuit for this pixel
         */
        void EnableTestpulse(bool tpen) {
            if(tpen) {
                m_latches |= (1 << 2);
            } else {
                m_latches &= static_cast<uint16_t>(~(1 << 2));
            }
        }
        bool GetEnableTestpulse() const { return (m_latches >> 2) & 0x1; }

        /* Enable/disable "long counter" mode (13bit ToA only)
         */
        void LongCounter(bool lgcnt) {
            if(lgcnt) {
                m_latches |= (1 << 1);
            } else {
                m_latches &= static_cast<uint16_t>(~(1 << 1));
            }
        }
        bool GetLongCounter() const { return (m_latches >> 1) & 0x1; }

        /** Overloaded print function for ostream operator
         */
        void print(std::ostream& out) const {
            out << "px [" << this->GetMask() << "|" << static_cast<int>(this->GetThreshold()) << "|"
                << this->GetCountingMode() << "|" << this->GetEnableTestpulse() << "|" << this->GetLongCounter() << "]";
        }
    };

    // CLICpix2 pixel readout class
    // The individual values are set via the member functions
    class pixelReadout : public virtual clicpix2_pixel {
    public:
        // Default constructor
        // Disables the pixel
        pixelReadout() : clicpix2_pixel(0x0), longflag(false){};
        pixelReadout(bool flag, uint8_t tot, uint8_t toa) : pixelReadout() {
            SetFlag(flag);
            SetTOT(tot);
            SetTOA(toa);
        }

        // Flag setting of the pixel
        void SetFlag(bool flag) {
            if(flag) {
                m_latches |= (1 << 13);
            } else {
                m_latches &= static_cast<uint16_t>(~(1 << 13));
            }
        }
        bool GetFlag() const { return (m_latches >> 13) & 0x1; }

        // TOT setting of the pixel (5bit)
        void SetTOT(uint8_t tot) { m_latches = (m_latches & 0xe0ff) | static_cast<uint16_t>((tot & 0x1f) << 8); }
        uint8_t GetTOT() const {
            if(longflag)
                throw DataException("LongCnt set, no TOT available.");
            else
                return (m_latches >> 8) & 0x1f;
        };

        // TOA setting of the pixel (13bit)
        void SetTOA(uint16_t toa) {
            m_latches = (m_latches & 0xe000) | (toa & 0x1fff);
            longflag = true;
        }
        // TOA setting of the pixel (8bit)
        void SetTOA(uint8_t toa) {
            m_latches = (m_latches & 0xff00) | static_cast<uint16_t>(toa & 0x00ff);
            longflag = false;
        }
        uint16_t GetTOA() const {
            if(longflag)
                return m_latches & 0x1fff;
            else
                return m_latches & 0x00ff;
        };

        // Counter
        void SetCounter(uint16_t cnt) { return SetTOA(cnt); }
        void SetCounter(uint8_t cnt) { return SetTOA(cnt); }
        uint16_t GetCounter() const { return GetTOA(); }

        /** Overloaded print function for ostream operator
         */
        void print(std::ostream& out) const {
            out << this->GetFlag();
            if(!longflag) {
                out << "," << static_cast<int>(this->GetTOT());
            }
            out << "," << static_cast<int>(this->GetTOA());
        }

    private:
        bool longflag;
    };
} // namespace caribou

#endif