summaryrefslogtreecommitdiff
path: root/peripheral/libupm/src/my9221/my9221.cxx
blob: 4cb10c6c0c571d1f7ad0ff790dea5ce9626347b4 (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
/*
 * Author: Jon Trulson <jtrulson@ics.com>
 * Copyright (c) 2016 Intel Corporation.
 *
 * These modules were rewritten, based on original work by:
 *
 * (original my9221/groveledbar driver)
 * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
 * Copyright (c) 2014 Intel Corporation.
 *
 * (grovecircularled driver)
 * Author: Jun Kato and Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
 * Contributions: Jon Trulson <jtrulson@ics.com>
 * Copyright (c) 2014 Intel Corporation.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include <iostream>
#include <string>
#include <stdexcept>
#include <unistd.h>
#include <stdlib.h>

#include "my9221.hpp"

using namespace upm;
using namespace std;

MY9221::MY9221 (uint8_t dataPin, uint8_t clockPin, int instances)
  : m_gpioData(dataPin), m_gpioClk(clockPin), m_bitStates(0)
{
  if (instances < 1)
    {
      throw std::out_of_range(std::string(__FUNCTION__) +
                              ": instances must be at least 1");
    }

  // set directions
  m_gpioClk.dir(mraa::DIR_OUT);
  m_gpioData.dir(mraa::DIR_OUT);
  
  // we warn if these fail, since it may not be possible to handle
  // more than one instance

  if (m_gpioClk.useMmap(true) != mraa::SUCCESS)
    cerr << __FUNCTION__
         << ": Warning: mmap of Clk pin failed, correct operation "
         << "may be affected."
         << endl;

  if (m_gpioData.useMmap(true) != mraa::SUCCESS)
    cerr << __FUNCTION__
         << ": Warning: mmap of Data pin failed, correct operation "
         << "may be affected."
         << endl;

  setLowIntensityValue(0x00);   // full off
  setHighIntensityValue(0xff);  // full brightness

  m_commandWord = 0x0000;       // all defaults
  m_instances = instances;

  m_bitStates = new uint16_t[instances * LEDS_PER_INSTANCE];

  setAutoRefresh(true);
  clearAll();
}

MY9221::~MY9221()
{
  clearAll();

  if (!m_autoRefresh)
    refresh();

  delete m_bitStates;
}

void MY9221::setLED(int led, bool on)
{
  int maxLed = (LEDS_PER_INSTANCE * m_instances) - 1;

  if (led > maxLed)
    led = maxLed;
  if (led < 0)
    led = 0;

  m_bitStates[led] = (on) ? m_highIntensity : m_lowIntensity;

  if (m_autoRefresh)
    refresh();
}

void MY9221::setLowIntensityValue(int intensity)
{
  m_lowIntensity = (intensity & 0xff);
}

void MY9221::setHighIntensityValue(int intensity)
{
  m_highIntensity = (intensity & 0xff);
}

void MY9221::setAll()
{
  for (int i=0; i<(m_instances * LEDS_PER_INSTANCE); i++)
    m_bitStates[i] = m_highIntensity;

  if (m_autoRefresh)
    refresh();
}

void MY9221::clearAll()
{
  for (int i=0; i<(m_instances * LEDS_PER_INSTANCE); i++)
    m_bitStates[i] = m_lowIntensity;

  if (m_autoRefresh)
    refresh();
}

void MY9221::refresh()
{
  for (int i=0; i<(m_instances * LEDS_PER_INSTANCE); i++)
    {
      if (i % 12 == 0)
        {
          send16bitBlock(m_commandWord);
        }
      send16bitBlock(m_bitStates[i]);
    }

  lockData();
}

void MY9221::lockData()
{
  m_gpioData.write(0);
  usleep(220);

  for(int idx = 0; idx < 4; idx++)
    {
      m_gpioData.write(1);
      m_gpioData.write(0);
    }

  // in reality, we only need > 200ns + (m_instances * 10ns), so the
  // following should be good for up to m_instances < 80), if the
  // datasheet is to be believed :)
  usleep(1);

  return;
}

void MY9221::send16bitBlock(uint16_t data)
{
  for (uint8_t bit_idx = 0; bit_idx < 16; bit_idx++)
    {
      uint32_t state = (data & 0x8000) ? 1 : 0;
      m_gpioData.write(state);
      state = m_gpioClk.read();

      if (state)
        state = 0;
      else
        state = 1;
      
      m_gpioClk.write(state);
      
      data <<= 1;
    }
  return;
}