|
| 1 | + |
| 2 | +// STL includes |
| 3 | +#include <cstdint> |
| 4 | +#include <vector> |
| 5 | +#include <iostream> |
| 6 | + |
| 7 | +#include <unistd.h> //Used for UART |
| 8 | +#include <fcntl.h> //Used for UART |
| 9 | +#include <termios.h> //Used for UART |
| 10 | +#include <sys/ioctl.h> |
| 11 | + |
| 12 | +std::vector<uint8_t> encode(const std::vector<uint8_t> & data); |
| 13 | +uint8_t encode(const bool bit1, const bool bit2, const bool bit3); |
| 14 | + |
| 15 | +void printClockSignal(const std::vector<uint8_t> & signal) |
| 16 | +{ |
| 17 | + bool prevBit = true; |
| 18 | + bool nextBit = true; |
| 19 | + |
| 20 | + for (uint8_t byte : signal) |
| 21 | + { |
| 22 | + |
| 23 | + for (int i=-1; i<9; ++i) |
| 24 | + { |
| 25 | + if (i == -1) // Start bit |
| 26 | + nextBit = true; |
| 27 | + else if (i == 8) // Stop bit |
| 28 | + nextBit = false; |
| 29 | + else |
| 30 | + nextBit = ~byte & (1 << i); |
| 31 | + |
| 32 | + if (!prevBit && nextBit) |
| 33 | + { |
| 34 | + std::cout << ' '; |
| 35 | + } |
| 36 | + |
| 37 | + if (nextBit) |
| 38 | + std::cout << '1'; |
| 39 | + else |
| 40 | + std::cout << '0'; |
| 41 | + |
| 42 | + prevBit = nextBit; |
| 43 | + } |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +int main() |
| 48 | +{ |
| 49 | + const std::vector<uint8_t> white{0xff, 0xff, 0xff}; |
| 50 | + const std::vector<uint8_t> green{0xff, 0x00, 0x00}; |
| 51 | + const std::vector<uint8_t> red {0x00, 0xff, 0x00}; |
| 52 | + const std::vector<uint8_t> blue {0x00, 0x00, 0xff}; |
| 53 | + const std::vector<uint8_t> cyan {0xff, 0x00, 0xff}; |
| 54 | + const std::vector<uint8_t> mix {0x55, 0x55, 0x55}; |
| 55 | + const std::vector<uint8_t> black{0x00, 0x00, 0x00}; |
| 56 | + const std::vector<uint8_t> gray{0x01, 0x01, 0x01}; |
| 57 | + |
| 58 | +// printClockSignal(encode(mix));std::cout << std::endl; |
| 59 | + |
| 60 | + //OPEN THE UART |
| 61 | +// int uart0_filestream = open("/dev/ttyAMA0", O_WRONLY | O_NOCTTY | O_NDELAY); |
| 62 | + int uart0_filestream = open("/dev/ttyUSB0", O_WRONLY | O_NOCTTY | O_NDELAY); |
| 63 | + if (uart0_filestream == -1) |
| 64 | + { |
| 65 | + //ERROR - CAN'T OPEN SERIAL PORT |
| 66 | + printf("Error - Unable to open UART. Ensure it is not in use by another application\n"); |
| 67 | + return -1; |
| 68 | + } |
| 69 | + |
| 70 | + // Configure the port |
| 71 | + struct termios options; |
| 72 | + tcgetattr(uart0_filestream, &options); |
| 73 | + options.c_cflag = B2500000 | CS8 | CLOCAL; |
| 74 | + options.c_iflag = IGNPAR; |
| 75 | + options.c_oflag = 0; |
| 76 | + options.c_lflag = 0; |
| 77 | + |
| 78 | + tcflush(uart0_filestream, TCIFLUSH); |
| 79 | + tcsetattr(uart0_filestream, TCSANOW, &options); |
| 80 | + |
| 81 | + { |
| 82 | + getchar(); |
| 83 | + const std::vector<uint8_t> encGreenData = encode(green); |
| 84 | + const std::vector<uint8_t> encBlueData = encode(blue); |
| 85 | + const std::vector<uint8_t> encRedData = encode(red); |
| 86 | + const std::vector<uint8_t> encGrayData = encode(gray); |
| 87 | + const std::vector<uint8_t> encBlackData = encode(black); |
| 88 | + |
| 89 | + //std::cout << "Writing GREEN ("; printClockSignal(encode(green)); std::cout << ")" << std::endl; |
| 90 | + const std::vector<uint8_t> garbage {0x0f}; |
| 91 | + write(uart0_filestream, garbage.data(), garbage.size()); |
| 92 | + write(uart0_filestream, encGreenData.data(), encGreenData.size()); |
| 93 | + write(uart0_filestream, encRedData.data(), encRedData.size()); |
| 94 | + write(uart0_filestream, encBlueData.data(), encBlueData.size()); |
| 95 | + write(uart0_filestream, encGrayData.data(), encGrayData.size()); |
| 96 | + write(uart0_filestream, encBlackData.data(), encBlackData.size()); |
| 97 | + } |
| 98 | + { |
| 99 | + getchar(); |
| 100 | + const std::vector<uint8_t> encData = encode(white); |
| 101 | + std::cout << "Writing WHITE ("; printClockSignal(encode(white)); std::cout << ")" << std::endl; |
| 102 | + const std::vector<uint8_t> garbage {0x0f}; |
| 103 | + write(uart0_filestream, garbage.data(), garbage.size()); |
| 104 | + write(uart0_filestream, encData.data(), encData.size()); |
| 105 | + } |
| 106 | + { |
| 107 | + getchar(); |
| 108 | + const std::vector<uint8_t> encData = encode(green); |
| 109 | + std::cout << "Writing GREEN ("; printClockSignal(encode(green)); std::cout << ")" << std::endl; |
| 110 | + write(uart0_filestream, encData.data(), encData.size()); |
| 111 | + } |
| 112 | + { |
| 113 | + getchar(); |
| 114 | + const std::vector<uint8_t> encData = encode(red); |
| 115 | + std::cout << "Writing RED ("; printClockSignal(encode(red)); std::cout << ")" << std::endl; |
| 116 | + write(uart0_filestream, encData.data(), encData.size()); |
| 117 | + } |
| 118 | + { |
| 119 | + getchar(); |
| 120 | + const std::vector<uint8_t> encData = encode(blue); |
| 121 | + std::cout << "Writing BLUE ("; printClockSignal(encode(blue)); std::cout << ")" << std::endl; |
| 122 | + write(uart0_filestream, encData.data(), encData.size()); |
| 123 | + } |
| 124 | + { |
| 125 | + getchar(); |
| 126 | + const std::vector<uint8_t> encData = encode(cyan); |
| 127 | + std::cout << "Writing CYAN? ("; printClockSignal(encode(cyan)); std::cout << ")" << std::endl; |
| 128 | + write(uart0_filestream, encData.data(), encData.size()); |
| 129 | + } |
| 130 | + { |
| 131 | + getchar(); |
| 132 | + const std::vector<uint8_t> encData = encode(mix); |
| 133 | + std::cout << "Writing MIX ("; printClockSignal(encode(mix)); std::cout << ")" << std::endl; |
| 134 | + write(uart0_filestream, encData.data(), encData.size()); |
| 135 | + } |
| 136 | + { |
| 137 | + getchar(); |
| 138 | + const std::vector<uint8_t> encData = encode(black); |
| 139 | + std::cout << "Writing BLACK ("; printClockSignal(encode(black)); std::cout << ")" << std::endl; |
| 140 | + write(uart0_filestream, encData.data(), encData.size()); |
| 141 | + write(uart0_filestream, encData.data(), encData.size()); |
| 142 | + write(uart0_filestream, encData.data(), encData.size()); |
| 143 | + write(uart0_filestream, encData.data(), encData.size()); |
| 144 | + } |
| 145 | + |
| 146 | + close(uart0_filestream); |
| 147 | + |
| 148 | + return 0; |
| 149 | +} |
| 150 | + |
| 151 | +std::vector<uint8_t> encode(const std::vector<uint8_t> & data) |
| 152 | +{ |
| 153 | + std::vector<uint8_t> result; |
| 154 | + for (size_t iByte=0; iByte<data.size(); iByte+=3) |
| 155 | + { |
| 156 | + const uint8_t byte1 = data[iByte]; |
| 157 | + const uint8_t byte2 = data[iByte+1]; |
| 158 | + const uint8_t byte3 = data[iByte+2]; |
| 159 | + |
| 160 | + result.push_back(encode(byte1 & 0x80, byte1 & 0x40, byte1 & 0x20)); |
| 161 | + result.push_back(encode(byte1 & 0x10, byte1 & 0x08, byte1 & 0x04)); |
| 162 | + result.push_back(encode(byte1 & 0x02, byte1 & 0x01, byte2 & 0x80)); |
| 163 | + result.push_back(encode(byte2 & 0x40, byte2 & 0x20, byte2 & 0x10)); |
| 164 | + result.push_back(encode(byte2 & 0x08, byte2 & 0x04, byte2 & 0x02)); |
| 165 | + result.push_back(encode(byte2 & 0x01, byte3 & 0x80, byte3 & 0x40)); |
| 166 | + result.push_back(encode(byte3 & 0x20, byte3 & 0x10, byte3 & 0x08)); |
| 167 | + result.push_back(encode(byte3 & 0x04, byte3 & 0x02, byte3 & 0x01)); |
| 168 | + } |
| 169 | + return result; |
| 170 | +} |
| 171 | + |
| 172 | +uint8_t encode(const bool bit1, const bool bit2, const bool bit3) |
| 173 | +{ |
| 174 | + uint8_t result = 0x44; // 0100 0100 |
| 175 | + |
| 176 | + if (bit1) |
| 177 | + result |= 0x01; // 0000 0001 |
| 178 | + |
| 179 | + if (bit2) |
| 180 | + result |= 0x18; // 0001 1000 |
| 181 | + |
| 182 | + if (bit3) |
| 183 | + result |= 0x80; // 1000 0000 |
| 184 | + |
| 185 | + return ~result; |
| 186 | +} |
0 commit comments