COSA
An Object-Oriented Platform for Arduino Programming
TCS230.cpp
Go to the documentation of this file.
1 
21 #include "TCS230.hh"
22 #include "Cosa/RTT.hh"
23 
28  Board::DigitalPin s3) :
29  m_out(out),
30  m_s0(s0, 1),
31  m_s1(s1, 0),
32  m_s2(s2, 1),
33  m_s3(s3, 1)
34 {
35 }
36 
37 void
39 {
40  m_s2.set(type & 0x2);
41  m_s3.set(type & 0x1);
42 }
43 
44 void
45 TCS230::frequency_scaling(uint8_t percent)
46 {
47  // Power-down on 0%
48  if (percent == 0) {
49  m_s0.low();
50  m_s1.low();
51  }
52 
53  // Scale 21..255 to 100%
54  else if (percent > 20) {
55  m_s0.high();
56  m_s1.high();
57  }
58 
59  // Scale 3..20 to 20%
60  else if (percent > 2) {
61  m_s0.high();
62  m_s1.low();
63  }
64 
65  // Scale 1..2 to 2%
66  else {
67  m_s0.low();
68  m_s1.high();
69  }
70 }
71 
72 uint16_t
73 TCS230::sample(uint8_t ms)
74 {
75  // Measure number of pulses of the given time period
76  m_out.m_count = 0;
77  uint32_t start = RTT::micros();
78  m_out.enable();
79  RTT::delay(ms);
80  m_out.disable();
81  uint32_t stop = RTT::micros();
82 
83  // Check for over-flow
84  if (m_out.m_count == m_out.MAX) return (UINT16_MAX);
85 
86  // Scale with run-time
87  uint32_t run = stop - start;
88  return ((m_out.m_count * run) / (ms * 1000L));
89 }
90 
91 TCS230::IRQPin::IRQPin(Board::ExternalInterruptPin pin) :
93  m_count(0)
94 {
95 }
96 
97 void
98 TCS230::IRQPin::on_interrupt(uint16_t arg)
99 {
100  UNUSED(arg);
101  m_count += 1;
102  if (m_count < MAX) return;
103  disable();
104 }
105 
106 
uint16_t sample(uint8_t ms=10)
Definition: TCS230.cpp:73
static void delay(uint32_t ms)
Definition: RTT.cpp:136
void set() const
Definition: OutputPin.hh:85
static uint32_t micros()
Definition: RTT.cpp:103
void low() const
Definition: OutputPin.hh:134
#define UINT16_MAX
Definition: Types.h:71
ExternalInterruptPin
Definition: ATmega1284P.hh:190
void photodiode(Filter type)
Definition: TCS230.cpp:38
void frequency_scaling(uint8_t percent)
Definition: TCS230.cpp:45
#define UNUSED(x)
Definition: ATmega328P.hh:31
Filter
Definition: TCS230.hh:52
TCS230(Board::ExternalInterruptPin out=Board::EXT1, Board::DigitalPin s0=Board::D4, Board::DigitalPin s1=Board::D5, Board::DigitalPin s2=Board::D6, Board::DigitalPin s3=Board::D7)
Definition: TCS230.cpp:24
void high() const
Definition: OutputPin.hh:95