23 #include "Charlieplex.h"
24 #if defined(ARDUINO) && ARDUINO >= 100
122 : _count(((int)numPins) * (numPins - 1))
123 , _lastTime(micros())
130 _holdTime = 20000 / _count / 4;
134 _pins1 = (uint8_t *)malloc(_count);
135 _pins2 = (uint8_t *)malloc(_count);
137 for (uint8_t pass = 1; pass < numPins; ++pass) {
138 for (uint8_t pin = 0; pin < (numPins - pass); ++pin) {
139 _pins1[n] = _pins2[n + 1] = pins[pin];
140 _pins2[n] = _pins1[n + 1] = pins[pin + pass];
146 _values = (uint8_t *)malloc(_count);
147 memset(_values, 0, _count);
150 for (uint8_t pin = 0; pin < numPins; ++pin) {
151 digitalWrite(pins[pin], LOW);
152 pinMode(pins[pin], INPUT);
279 unsigned long us = micros();
280 if ((us - _lastTime) >= _holdTime) {
299 int prevIndex = _currentIndex;
302 _currentIndex = (_currentIndex + 1) % _count;
303 if (_values[_currentIndex] != 0)
309 if (prevIndex != -1) {
310 digitalWrite(_pins1[prevIndex], LOW);
311 digitalWrite(_pins2[prevIndex], LOW);
312 pinMode(_pins1[prevIndex], INPUT);
313 pinMode(_pins2[prevIndex], INPUT);
320 uint8_t value = _values[_currentIndex];
321 uint8_t pin1 = _pins1[_currentIndex];
322 uint8_t pin2 = _pins2[_currentIndex];
324 if (prevIndex != _currentIndex) {
326 if (prevIndex != -1) {
327 digitalWrite(_pins1[prevIndex], LOW);
328 digitalWrite(_pins2[prevIndex], LOW);
329 pinMode(_pins1[prevIndex], INPUT);
330 pinMode(_pins2[prevIndex], INPUT);
336 pinMode(pin1, OUTPUT);
337 pinMode(pin2, OUTPUT);
338 if (value > _pwmPhase)
339 digitalWrite(pin1, HIGH);
341 digitalWrite(pin1, LOW);
346 digitalWrite(pin1, HIGH);
348 analogWrite(pin1, value);