diff --git a/Pipfile b/Pipfile new file mode 100644 index 0000000..98bdc68 --- /dev/null +++ b/Pipfile @@ -0,0 +1,13 @@ +[[source]] +url = "https://pypi.python.org/simple" +verify_ssl = true +name = "pypi" + +[packages] +pigpio = "*" + +[dev-packages] +pylint = "*" + +[requires] +python_version = "3.7" diff --git a/Pipfile.lock b/Pipfile.lock new file mode 100644 index 0000000..688857e --- /dev/null +++ b/Pipfile.lock @@ -0,0 +1,116 @@ +{ + "_meta": { + "hash": { + "sha256": "34f89c847ad420c24df2cc885dc53c690713001eed8b62e3d1f2fc6c59511423" + }, + "pipfile-spec": 6, + "requires": { + "python_version": "3.7" + }, + "sources": [ + { + "name": "pypi", + "url": "https://pypi.python.org/simple", + "verify_ssl": true + } + ] + }, + "default": { + "pigpio": { + "hashes": [ + "sha256:c53c155fac05f855657c794f533cb128a516d041d6560e0fb0ead5247a7ac2fe", + "sha256:df5e430e5c026c95f575cb50ccd18eb37d41be4e0bbbde070ab332dae27fdf58" + ], + "index": "pypi", + "version": "==1.44" + } + }, + "develop": { + "astroid": { + "hashes": [ + "sha256:6560e1e1749f68c64a4b5dee4e091fce798d2f0d84ebe638cf0e0585a343acf4", + "sha256:b65db1bbaac9f9f4d190199bb8680af6f6f84fd3769a5ea883df8a91fe68b4c4" + ], + "version": "==2.2.5" + }, + "isort": { + "hashes": [ + "sha256:54da7e92468955c4fceacd0c86bd0ec997b0e1ee80d97f67c35a78b719dccab1", + "sha256:6e811fcb295968434526407adb8796944f1988c5b65e8139058f2014cbe100fd" + ], + "version": "==4.3.21" + }, + "lazy-object-proxy": { + "hashes": [ + "sha256:02b260c8deb80db09325b99edf62ae344ce9bc64d68b7a634410b8e9a568edbf", + "sha256:18f9c401083a4ba6e162355873f906315332ea7035803d0fd8166051e3d402e3", + "sha256:1f2c6209a8917c525c1e2b55a716135ca4658a3042b5122d4e3413a4030c26ce", + "sha256:2f06d97f0ca0f414f6b707c974aaf8829c2292c1c497642f63824119d770226f", + "sha256:616c94f8176808f4018b39f9638080ed86f96b55370b5a9463b2ee5c926f6c5f", + "sha256:63b91e30ef47ef68a30f0c3c278fbfe9822319c15f34b7538a829515b84ca2a0", + "sha256:77b454f03860b844f758c5d5c6e5f18d27de899a3db367f4af06bec2e6013a8e", + "sha256:83fe27ba321e4cfac466178606147d3c0aa18e8087507caec78ed5a966a64905", + "sha256:84742532d39f72df959d237912344d8a1764c2d03fe58beba96a87bfa11a76d8", + "sha256:874ebf3caaf55a020aeb08acead813baf5a305927a71ce88c9377970fe7ad3c2", + "sha256:9f5caf2c7436d44f3cec97c2fa7791f8a675170badbfa86e1992ca1b84c37009", + "sha256:a0c8758d01fcdfe7ae8e4b4017b13552efa7f1197dd7358dc9da0576f9d0328a", + "sha256:a4def978d9d28cda2d960c279318d46b327632686d82b4917516c36d4c274512", + "sha256:ad4f4be843dace866af5fc142509e9b9817ca0c59342fdb176ab6ad552c927f5", + "sha256:ae33dd198f772f714420c5ab698ff05ff900150486c648d29951e9c70694338e", + "sha256:b4a2b782b8a8c5522ad35c93e04d60e2ba7f7dcb9271ec8e8c3e08239be6c7b4", + "sha256:c462eb33f6abca3b34cdedbe84d761f31a60b814e173b98ede3c81bb48967c4f", + "sha256:fd135b8d35dfdcdb984828c84d695937e58cc5f49e1c854eb311c4d6aa03f4f1" + ], + "version": "==1.4.2" + }, + "mccabe": { + "hashes": [ + "sha256:ab8a6258860da4b6677da4bd2fe5dc2c659cff31b3ee4f7f5d64e79735b80d42", + "sha256:dd8d182285a0fe56bace7f45b5e7d1a6ebcbf524e8f3bd87eb0f125271b8831f" + ], + "version": "==0.6.1" + }, + "pylint": { + "hashes": [ + "sha256:5d77031694a5fb97ea95e828c8d10fc770a1df6eb3906067aaed42201a8a6a09", + "sha256:723e3db49555abaf9bf79dc474c6b9e2935ad82230b10c1138a71ea41ac0fff1" + ], + "index": "pypi", + "version": "==2.3.1" + }, + "six": { + "hashes": [ + "sha256:3350809f0555b11f552448330d0b52d5f24c91a322ea4a15ef22629740f3761c", + "sha256:d16a0141ec1a18405cd4ce8b4613101da75da0e9a7aec5bdd4fa804d0e0eba73" + ], + "version": "==1.12.0" + }, + "typed-ast": { + "hashes": [ + "sha256:18511a0b3e7922276346bcb47e2ef9f38fb90fd31cb9223eed42c85d1312344e", + "sha256:262c247a82d005e43b5b7f69aff746370538e176131c32dda9cb0f324d27141e", + "sha256:2b907eb046d049bcd9892e3076c7a6456c93a25bebfe554e931620c90e6a25b0", + "sha256:354c16e5babd09f5cb0ee000d54cfa38401d8b8891eefa878ac772f827181a3c", + "sha256:4e0b70c6fc4d010f8107726af5fd37921b666f5b31d9331f0bd24ad9a088e631", + "sha256:630968c5cdee51a11c05a30453f8cd65e0cc1d2ad0d9192819df9978984529f4", + "sha256:66480f95b8167c9c5c5c87f32cf437d585937970f3fc24386f313a4c97b44e34", + "sha256:71211d26ffd12d63a83e079ff258ac9d56a1376a25bc80b1cdcdf601b855b90b", + "sha256:95bd11af7eafc16e829af2d3df510cecfd4387f6453355188342c3e79a2ec87a", + "sha256:bc6c7d3fa1325a0c6613512a093bc2a2a15aeec350451cbdf9e1d4bffe3e3233", + "sha256:cc34a6f5b426748a507dd5d1de4c1978f2eb5626d51326e43280941206c209e1", + "sha256:d755f03c1e4a51e9b24d899561fec4ccaf51f210d52abdf8c07ee2849b212a36", + "sha256:d7c45933b1bdfaf9f36c579671fec15d25b06c8398f113dab64c18ed1adda01d", + "sha256:d896919306dd0aa22d0132f62a1b78d11aaf4c9fc5b3410d3c666b818191630a", + "sha256:ffde2fbfad571af120fcbfbbc61c72469e72f550d676c3342492a9dfdefb8f12" + ], + "markers": "implementation_name == 'cpython'", + "version": "==1.4.0" + }, + "wrapt": { + "hashes": [ + "sha256:565a021fd19419476b9362b05eeaa094178de64f8361e44468f9e9d7843901e1" + ], + "version": "==1.11.2" + } + } +} diff --git a/share/bb_UART/bb_uart_test.py b/share/bb_UART/bb_uart_test.py new file mode 100644 index 0000000..da71d47 --- /dev/null +++ b/share/bb_UART/bb_uart_test.py @@ -0,0 +1,13 @@ +import pigpio + +pi = pigpio.pi() +try: + pi.bb_serial_read_close(17) + status = pi.bb_serial_read_open(17, 9600) + while True: + count, data = pi.bb_serial_read(17) + while count: + print(data) + +finally: + pi.bb_serial_read_close(17) diff --git a/share/bb_UART/include/UART.hpp b/share/bb_UART/include/UART.hpp new file mode 100644 index 0000000..478f940 --- /dev/null +++ b/share/bb_UART/include/UART.hpp @@ -0,0 +1,71 @@ +#pragma once + +#include +#include +#include +#include +#include + +namespace uart +{ + enum uart_err_t + { + UART_SUCCEEDED = 0, + UART_ERROR_UNKNOWN = -1, + UART_INIT_FAILED = -2, + UART_TRANSMIT_FAILED = -3, + UART_RECEIVE_FAILED = -4, + UART_HASH_NOT_MATCH = -5, + UART_BUFFER_OVER = -6 + }; + + struct pdu + { + struct header + { + uint8_t seq_num; + uint8_t payload_size; + uint8_t checksum; + }; + struct data + { + uint8_t type; + uint8_t* data; + uint8_t checksum; + }; + }; + + struct uart_conf + { + int TX = -1; // TX pin + int RX = -1; // RX pin + int BAUD = 9600; + int DATA_BIT_LEN = 8; // bits + int STOP_BIT = 2; + int OFFSET = 0; + bool ENABLE_RESEND = true; // 再送 + bool ENABLE_ECC = false; + }; + + class UART + { + private: + const uart_conf conf; + uint8_t sequence_num; + std::queue transmit_queue; + std::mutex lock; + + public: + UART(uart_conf&); + virtual uart_err_t initialize(); + virtual int thrower(const char*); + // Generate Protocol Data Unit + template uart_err_t create_pdu(const char&, const T&); + template void calc_parity(char*, const T&, size_t&); + virtual int receive(); + uart_err_t get_data(); + + }; + +} + diff --git a/share/bb_UART/src/UART.cpp b/share/bb_UART/src/UART.cpp new file mode 100644 index 0000000..c122b9c --- /dev/null +++ b/share/bb_UART/src/UART.cpp @@ -0,0 +1,50 @@ +#include "UART.hpp" + +namespace uart +{ + UART::UART(uart_conf& conf_) : conf(conf_), sequence_num(0) {} + + template + uart_err_t UART::create_pdu(const char &type, const T &data_) + { + size_t size = sizeof(T) + std::unique_ptr buf(new pdu); + + (*buf).data.type = type; + + if (conf.ENABLE_ECC == false){ + (*buf).data.data = (char*)&data_; + } else { + calc_parity((*buf).data.data, data_, size); + } + + (*buf).data.checksum = std::accumulate((*buf).data.data, + (*buf).data.data+(uint8_t)size, 0) / (uint8_t)size; + + (*buf).header.payload_size = (char)size; + + lock.lock(); + (*buf).header.seq_num = sequence_num; + if (sequence_num == 255) { + sequence_num = 0; + } else { + sequence_num++; + } + lock.unlock(); + + (*buf).header.checksum = ((*buf).header.seq_num + (*buf).header.payload_size) / 2; + + } + + template + void UART::calc_parity(char* buf, const T &data_, size_t &size) + { + + } + + uart_err_t UART::get_data() + { + + } + +}