math.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #pragma once
  2. #include <stdbool.h>
  3. #include <stdint.h>
  4. #include <stddef.h>
  5. #define bit_read(value, bit) (((value) >> (bit)) & 0x01)
  6. #define bit_set(value, bit) ((value) |= (1UL << (bit)))
  7. #define bit_clear(value, bit) ((value) &= ~(1UL << (bit)))
  8. #define bit_write(value, bit, bitvalue) (bitvalue ? bit_set(value, bit) : bit_clear(value, bit))
  9. #define DURATION_DIFF(x, y) ((x < y) ? (y - x) : (x - y))
  10. #define abs(x) ((x) > 0 ? (x) : -(x))
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. /**
  15. * Flip the data bitwise.
  16. * @param key In data
  17. * @param count_bit number of data bits
  18. * @return Reverse data
  19. */
  20. uint64_t subghz_protocol_blocks_reverse_key(uint64_t key, uint8_t count_bit);
  21. /**
  22. * Get parity the data bitwise.
  23. * @param key In data
  24. * @param count_bit number of data bits
  25. * @return parity
  26. */
  27. uint8_t subghz_protocol_blocks_get_parity(uint64_t key, uint8_t count_bit);
  28. /**
  29. * CRC-4.
  30. * @param message array of bytes to check
  31. * @param nBytes number of bytes in message
  32. * @param polynomial CRC polynomial
  33. * @param init starting crc value
  34. * @return CRC value
  35. */
  36. uint8_t subghz_protocol_blocks_crc4(
  37. uint8_t const message[],
  38. unsigned nBytes,
  39. uint8_t polynomial,
  40. uint8_t init);
  41. /**
  42. * CRC-7.
  43. * @param message array of bytes to check
  44. * @param nBytes number of bytes in message
  45. * @param polynomial CRC polynomial
  46. * @param init starting crc value
  47. * @return CRC value
  48. */
  49. uint8_t subghz_protocol_blocks_crc7(
  50. uint8_t const message[],
  51. unsigned nBytes,
  52. uint8_t polynomial,
  53. uint8_t init);
  54. /**
  55. * Generic Cyclic Redundancy Check CRC-8.
  56. * Example polynomial: 0x31 = x8 + x5 + x4 + 1 (x8 is implicit)
  57. * Example polynomial: 0x80 = x8 + x7 (a normal bit-by-bit parity XOR)
  58. * @param message array of bytes to check
  59. * @param nBytes number of bytes in message
  60. * @param polynomial byte is from x^7 to x^0 (x^8 is implicitly one)
  61. * @param init starting crc value
  62. * @return CRC value
  63. */
  64. uint8_t subghz_protocol_blocks_crc8(
  65. uint8_t const message[],
  66. unsigned nBytes,
  67. uint8_t polynomial,
  68. uint8_t init);
  69. #ifdef __cplusplus
  70. }
  71. #endif