summaryrefslogtreecommitdiff
path: root/peripheral/libupm/src/my9221/my9221.h
blob: 5eebb392ab9e631328e154cfc39c2faf935c491f (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
/*
 * Author: Jon Trulson <jtrulson@ics.com>
 * Copyright (c) 2016 Intel Corporation.
 *
 * These modules were rewritten, based on original work by:
 *
 * (original my9221/groveledbar)
 * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
 * Copyright (c) 2014 Intel Corporation.
 *
 * (grovecircularled)
 * 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.
 */
#pragma once

#include <string>
#include <mraa/common.hpp>
#include <mraa/gpio.hpp>

namespace upm {

  /**
   * @brief MY9221 LED Controller library
   * @defgroup my9221 libupm-my9221
   * @ingroup seeed display gpio eak
   */
  class MY9221 {
  public:

    // 12 LED channels per chip (instance)
    static const int LEDS_PER_INSTANCE = 12;

    /**
     * Instantiates an MY9221 object
     *
     * @param dataPin Data pin
     * @param clockPin Clock pin
     * @param instances Number of daisy-chained my9221s, default 1
     */
    MY9221(uint8_t dataPin, uint8_t clockPin, int instances=1);

    /**
     * MY9221 destructor
     */
    ~MY9221();

    /**
     * Enable or disable auto refresh.  When auto refresh is enabled,
     * update the LED display as soon as the internal state changes.
     * When false, the display(s) will not be updated until the
     * refresh() method is called.
     *
     * @param enable true to enable auto refresh, false otherwise
     */
    void setAutoRefresh(bool enable)
    {
      m_autoRefresh = enable;
    }

    /**
     * Set an LED to a specific on (high intensity) or off (low
     * intensity) value.
     *
     * @param led The LED whose state you wish to change
     * @param on true to turn on the LED, false to turn the LED off
     */
    void setLED(int led, bool on);

    /**
     * Set the greyscale intensity of an LED in the OFF state.  The
     * intensity is a value from 0 (fully off) to 255 (fully on).
     * This will take effect on any future LED set or clear
     * operations.
     *
     * @param intensity a value from 0 (fully off) to 255 (fully on)
     */
    void setLowIntensityValue(int intensity);

    /**
     * Set the greyscale intensity of an LED in the ON state.  The
     * intensity is a value from 0 (fully off) to 255 (fully on).
     * This will take effect on any future LED set or clear
     * operations.
     *
     * @param intensity a value from 0 (fully off) to 255 (fully on)
     */
    void setHighIntensityValue(int intensity);

    /**
     * Set all of the LEDS to the ON (high intensity value) state.
     */
    void setAll();

    /**
     * Set all of the LEDS to the OFF (low intensity value) state.
     */
    void clearAll();

    /**
     * Set the LED states to match the internal stored states.  This
     * is useful when auto refresh (setAutoRefresh()) is false to
     * update the display.
     */
    void refresh();

  protected:
    virtual void lockData();
    virtual void send16bitBlock(uint16_t data);

    bool m_autoRefresh;
    // we're only doing 8-bit greyscale, so the high order bits are
    // always 0
    uint16_t m_lowIntensity;
    uint16_t m_highIntensity;

    unsigned int m_instances;

    // an array of uint16_t's representing our bit states (on/off)
    // intensities.  Only the low 8 bits are used, but in the future
    // 16bit support can work here as well.
    uint16_t *m_bitStates;

    uint16_t m_commandWord;

    mraa::Gpio m_gpioClk;
    mraa::Gpio m_gpioData;

  private:
  };

}