PropWare  3.0.0.229
C++ objects and CMake build system for Parallax Propeller
max72xx.h
Go to the documentation of this file.
1 
26 #pragma once
27 
29 
30 namespace PropWare {
31 
59 class MAX72xx {
60  public:
64  // Addresses start at 0x10 to avoid conflicts with MAX695x devices
65  enum class Register { NO_OP, DIGIT_ZERO = 0x11, DIGIT_ONE = 0x12, DIGIT_TWO = 0x13, DIGIT_THREE = 0x14, DIGIT_FOUR = 0x15, DIGIT_FIVE = 0x16, DIGIT_SIX = 0x17, DIGIT_SEVEN = 0x18, DECODE_MODE = 0x19, INTENSITY = 0x1A, SCAN_LIMIT = 0x1B, SHUTDOWN = 0x1C, DISPLAY_TEST = 0x1D
80  };
81 
88  enum class BcdCharacter {
89  ZERO,
90  ONE,
91  TWO,
92  THREE,
93  FOUR,
94  FIVE,
95  SIX,
96  SEVEN,
97  EIGHT,
98  NINE,
99  DASH,
100  E,
101  H,
102  L,
103  P,
104  BLANK
105  };
106 
111  static const uint8_t DEFAULT_INTENSITY = 15;
112  static const uint8_t DEFAULT_SCAN_LIMIT = 7;
113  static const uint8_t DEFAULT_BCD_DECODE_BITS = 0xFF;
114  static const uint8_t DECIMAL_POINT = BIT_7;
115 
116  public:
124  static Register to_register (unsigned int zeroIndexedDigit) {
125  return static_cast<Register>(static_cast<unsigned int>(Register::DIGIT_ZERO) + zeroIndexedDigit);
126  }
127 
128  public:
136  MAX72xx (SPI &bus, const Pin::Mask csMask, const bool alwaysSetSPIMode = false)
137  : m_bus(&bus),
138  m_cs(csMask, Pin::Dir::OUT),
139  m_alwaysSetMode(alwaysSetSPIMode) {
140  if (!alwaysSetSPIMode)
141  this->m_bus->set_mode(SPI_MODE);
142  this->m_cs.set();
143  }
144 
151  MAX72xx (const Pin::Mask csMask, const bool alwaysSetSPIMode = true)
152  : m_bus(&SPI::get_instance()),
153  m_cs(csMask, Pin::Dir::OUT),
154  m_alwaysSetMode(alwaysSetSPIMode) {
155  if (!alwaysSetSPIMode)
156  this->m_bus->set_mode(SPI::Mode::MODE_1);
157  this->m_cs.set();
158  }
159 
166  void always_set_spi_mode (const bool alwaysSetMode) {
167  this->m_alwaysSetMode = alwaysSetMode;
168  }
169 
180  void start (const uint8_t intensity = DEFAULT_INTENSITY, const uint8_t scanLimit = DEFAULT_SCAN_LIMIT,
181  const uint8_t bcdDecodeBits = DEFAULT_BCD_DECODE_BITS) const {
182  this->set_intensity(intensity);
183  this->set_scan_limit(scanLimit);
184  this->set_decode_mode(bcdDecodeBits);
185  this->set_test_mode(false);
186  this->clear();
187  this->write(Register::SHUTDOWN, 1);
188  }
189 
193  void shutdown () const {
194  this->write(Register::SHUTDOWN, 0);
195  }
196 
202  void set_intensity (const uint8_t intensity) const {
203  if (16 > intensity)
204  this->write(Register::INTENSITY, intensity);
205  }
206 
213  void set_scan_limit (const uint8_t scanLimit) const {
214  if (8 > scanLimit)
215  this->write(Register::SCAN_LIMIT, scanLimit);
216  }
217 
226  void set_decode_mode (const uint8_t bcdDecodeBits) const {
227  this->write(Register::DECODE_MODE, bcdDecodeBits);
228  }
229 
236  void set_test_mode (const bool enableTestMode) const {
237  this->write(Register::DISPLAY_TEST, static_cast<const uint8_t>(enableTestMode));
238  }
239 
251  void put_int (int32_t x, const BcdCharacter fillChar = BcdCharacter::BLANK, unsigned int width = 0) const {
252  if (width > 8)
253  width = 8;
254 
255  const bool isNegative = 0 > x;
256  if (isNegative) {
257  x = abs(x);
258  }
259 
260  uint_fast8_t i = 0;
261  do {
262  const uint8_t digit = static_cast<const uint8_t>(x % 10);
263  if (x)
264  this->write(to_register(i), digit);
265  x /= 10;
266  ++i;
267  } while (x && 8 > i);
268 
269  if (isNegative) {
270  if (width)
271  --width;
272  while (width > i) {
273  this->write(to_register(i++), static_cast<const uint8_t>(fillChar));
274  }
275  if (8 > i)
276  this->write(to_register(i), static_cast<const uint8_t>(BcdCharacter::DASH));
277  } else {
278  while (width > i) {
279  this->write(to_register(i), static_cast<const uint8_t>(fillChar));
280  ++i;
281  }
282  }
283  }
284 
288  void clear () const {
289  for (uint8_t i = 0; i < 8; ++i)
290  this->write(to_register(i), static_cast<const uint8_t>(BcdCharacter::BLANK));
291  }
292 
301  void write (const Register address, const uint8_t value, const bool decimal = false) const {
302  if (this->m_alwaysSetMode)
303  this->m_bus->set_mode(SPI_MODE);
304 
305  this->m_cs.clear();
306  this->m_bus->shift_out(8, static_cast<uint32_t>(address));
307  if (decimal)
308  this->m_bus->shift_out(8, value | DECIMAL_POINT);
309  else
310  this->m_bus->shift_out(8, value);
311  this->m_cs.set();
312  }
313 
314  private:
315  SPI *m_bus;
316  const Pin m_cs;
317  bool m_alwaysSetMode;
318 };
319 
320 }
PropWare::MAX72xx
Serially interfaced, 8-Digit LED display driver.
Definition: max72xx.h:59
PropWare::MAX72xx::to_register
static Register to_register(unsigned int zeroIndexedDigit)
Get the Register address of the requested 0-indexed digit on the device.
Definition: max72xx.h:124
PropWare::MAX72xx::set_scan_limit
void set_scan_limit(const uint8_t scanLimit) const
Enables or disables specific digits on the device.
Definition: max72xx.h:213
PropWare::MAX72xx::clear
void clear() const
Write BcdCharacter::BLANK to all eight digits.
Definition: max72xx.h:288
PropWare::MAX72xx::SPI_MODE
static const SPI::Mode SPI_MODE
Definition: max72xx.h:110
PropWare::SPI
SPI serial communications library; Core functionality comes from a dedicated assembly cog.
Definition: spi.h:43
PropWare::MAX72xx::Register
Register
Register addresses.
Definition: max72xx.h:65
PropWare::MAX72xx::Register::DIGIT_THREE
@ DIGIT_THREE
PropWare::MAX72xx::Register::DISPLAY_TEST
@ DISPLAY_TEST
PropWare::Port::Mask
Mask
Definition: port.h:43
PropWare::MAX72xx::Register::DIGIT_SEVEN
@ DIGIT_SEVEN
spi.h
PropWare::MAX72xx::Register::DIGIT_FIVE
@ DIGIT_FIVE
PropWare::MAX72xx::Register::DIGIT_TWO
@ DIGIT_TWO
PropWare::MAX72xx::MAX72xx
MAX72xx(const Pin::Mask csMask, const bool alwaysSetSPIMode=true)
Create an object which communicates over the shared SPI bus.
Definition: max72xx.h:151
PropWare::Pin
Utility class to handle general purpose I/O pins.
Definition: pin.h:36
PropWare::MAX72xx::put_int
void put_int(int32_t x, const BcdCharacter fillChar=BcdCharacter::BLANK, unsigned int width=0) const
Write an integer to the device.
Definition: max72xx.h:251
PropWare::MAX72xx::Register::DIGIT_ZERO
@ DIGIT_ZERO
PropWare::MAX72xx::Register::DIGIT_SIX
@ DIGIT_SIX
PropWare::MAX72xx::BcdCharacter
BcdCharacter
Characters that will utilize the device's built-in font.
Definition: max72xx.h:88
PropWare::MAX72xx::Register::DECODE_MODE
@ DECODE_MODE
PropWare::MAX72xx::Register::DIGIT_ONE
@ DIGIT_ONE
PropWare::MAX72xx::set_test_mode
void set_test_mode(const bool enableTestMode) const
Enable or disable the test mode of the device. When enable, all LED segments will be lit....
Definition: max72xx.h:236
PropWare::SPI::shift_out
void shift_out(uint8_t bits, uint32_t value) const
Send a value out to a peripheral device.
Definition: spi.h:224
PropWare::MAX72xx::set_decode_mode
void set_decode_mode(const uint8_t bcdDecodeBits) const
Determines which digits will be decoded and which ones will be converted to the build-in font prior t...
Definition: max72xx.h:226
PropWare::Port::set
void set() const
Set selected output port high (set all pins to 1)
Definition: port.h:226
PropWare::MAX72xx::set_intensity
void set_intensity(const uint8_t intensity) const
Set the device's brightness.
Definition: max72xx.h:202
PropWare::SPI::set_mode
void set_mode(const Mode mode)
Set the mode of SPI communication.
Definition: spi.h:166
x
int x
Definition: 07 Box and Lines.c:13
PropWare::MAX72xx::Register::INTENSITY
@ INTENSITY
PropWare::MAX72xx::Register::SCAN_LIMIT
@ SCAN_LIMIT
PropWare::MAX72xx::Register::SHUTDOWN
@ SHUTDOWN
PropWare::MAX72xx::Register::DIGIT_FOUR
@ DIGIT_FOUR
L
#define L
For selecting the left of the two RGB LEDs. Example: rgb(L, RED) would set the left RGB LED to the co...
Definition: badgetools.h:88
PropWare::MAX72xx::always_set_spi_mode
void always_set_spi_mode(const bool alwaysSetMode)
Choose whether to always set the SPI mode before writing to the device; Useful when multiple devices ...
Definition: max72xx.h:166
PropWare::MAX72xx::Register::NO_OP
@ NO_OP
PropWare::MAX72xx::shutdown
void shutdown() const
Disable the device by writing to the SHUTDOWN register. Values in other registers will not be lost.
Definition: max72xx.h:193
PropWare::SPI::Mode::MODE_1
@ MODE_1
PropWare
Generic definitions and functions for the Parallax Propeller.
Definition: runnable.h:33
PropWare::Port::clear
void clear() const
Clear selected output port (set it to 0)
Definition: port.h:249
PropWare::MAX72xx::MAX72xx
MAX72xx(SPI &bus, const Pin::Mask csMask, const bool alwaysSetSPIMode=false)
Create an object which communicates over the given SPI bus.
Definition: max72xx.h:136
PropWare::MAX72xx::start
void start(const uint8_t intensity=DEFAULT_INTENSITY, const uint8_t scanLimit=DEFAULT_SCAN_LIMIT, const uint8_t bcdDecodeBits=DEFAULT_BCD_DECODE_BITS) const
Initialize the device after power-on.
Definition: max72xx.h:180
PropWare::MAX72xx::write
void write(const Register address, const uint8_t value, const bool decimal=false) const
Perform a manual write to the device.
Definition: max72xx.h:301
PropWare::SPI::Mode
Mode
Descriptor for SPI signal as defined by Motorola modes.
Definition: spi.h:66