Skip to content

Commit bacb403

Browse files
Update armdroid.hpp
1 parent 0dd9255 commit bacb403

1 file changed

Lines changed: 18 additions & 17 deletions

File tree

code/armdroid-class/armdroid.hpp

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include <Arduino.h>
22
#include "armbase.hpp"
33

4+
typedef uint8_t (*bitpacker_t)(uint8_t address, uint8_t data);
5+
46
class ParallelArmdroid : public AbstractArmdroid {
57
private:
68
uint8_t port[8];
@@ -35,10 +37,9 @@ class PartialSerialArmdroid : public AbstractArmdroid {
3537
uint8_t dataPin;
3638
uint8_t clockPin;
3739
uint8_t latchPin;
38-
uint32_t dataIndexes;
39-
uint32_t addressIndexes;
40+
bitpacker_t packBits;
4041
public:
41-
PartialSerialArmdroid(uint8_t dataPin, uint8_t clockPin, uint8_t latchPin, uint32_t dataIndexes, uint32_t addressIndexes) {
42+
PartialSerialArmdroid(uint8_t dataPin, uint8_t clockPin, uint8_t latchPin, bitpacker_t packBits) {
4243
this->dataPin = dataPin;
4344
this->clockPin = clockPin;
4445
this->latchPin = latchPin;
@@ -47,22 +48,22 @@ class PartialSerialArmdroid : public AbstractArmdroid {
4748
pinMode(this->latchPin, OUTPUT);
4849
digitalWrite(this->dataPin, LOW);
4950
digitalWrite(this->clockPin, LOW);
50-
digitalWrite(this->latchPin, HIGH);
51-
this->dataIndexes = dataIndexes;
52-
this->addressIndexes = addressIndexes;
51+
digitalWrite(this->latchPin, LOW);
52+
this->packBits = packBits;
5353
}
5454
void writeToPort(uint8_t address, uint8_t data) {
55-
uint8_t b = (
56-
(((address >> ((this->addressIndexes >> 16) & 15)) & 1) ) |
57-
(((address >> ((this->addressIndexes >> 8) & 15)) & 1) << 1) |
58-
(((address >> ((this->addressIndexes ) & 15)) & 1) << 2) |
59-
(((data >> ((this->dataIndexes >> 24) & 15)) & 1) << 3) |
60-
(((data >> ((this->dataIndexes >> 16) & 15)) & 1) << 4) |
61-
(((data >> ((this->dataIndexes >> 8) & 15)) & 1) << 5) |
62-
(((data >> ((this->dataIndexes ) & 15)) & 1) << 6)
63-
);
64-
shiftOut(this->dataPin, this->clockPin, LSBFIRST, b);
65-
digitalWrite(this->latchPin, LOW);
55+
uint8_t b = this->packBits(address, data);
56+
for (int i = 0; i < 7; i++) {
57+
digitalWrite(this->dataPin, b & 1);
58+
delayMicroseconds(30);
59+
digitalWrite(this->clockPin, HIGH);
60+
delayMicroseconds(30);
61+
digitalWrite(this->clockPin, LOW);
62+
delayMicroseconds(30);
63+
b >>= 1;
64+
}
6665
digitalWrite(this->latchPin, HIGH);
66+
delayMicroseconds(30);
67+
digitalWrite(this->latchPin, LOW);
6768
}
6869
};

0 commit comments

Comments
 (0)