aboutsummaryrefslogtreecommitdiff
path: root/examples/smartresponse_public/PublicMessageRing.h
blob: af0b7e14fdbcfa2137b9e6f57f5b21bb6f890488 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#pragma once

#include <Arduino.h>
#include <MeshCore.h>
#include <string.h>

struct PublicMessage {
  static const uint8_t kWrapCols = 54;
  static const uint8_t kTextMax = 172;  // 171 visible bytes plus NUL.

  uint32_t sequence;
  uint32_t timestamp;
  bool outbound;
  bool rebroadcasted;
  uint8_t hash[MAX_HASH_SIZE];
  char text[kTextMax];
};

static inline uint8_t wrappedSegmentLen(const char* text, uint8_t cols = PublicMessage::kWrapCols) {
  uint8_t len = 0;
  uint8_t last_space = 0;
  if (!text || !*text) {
    return 0;
  }
  while (text[len] && len < cols) {
    if (text[len] == ' ') {
      last_space = len;
    }
    ++len;
  }
  if (!text[len]) {
    return len;
  }
  return (last_space > 0) ? last_space : cols;
}

static inline uint8_t wrappedLineCount(const char* text, uint8_t cols = PublicMessage::kWrapCols) {
  if (!text || !*text) {
    return 1;
  }

  uint8_t count = 0;
  const char* p = text;
  while (*p) {
    while (*p == ' ') {
      ++p;
    }
    if (!*p) {
      break;
    }
    uint8_t len = wrappedSegmentLen(p, cols);
    if (len == 0) {
      break;
    }
    ++count;
    p += len;
    while (*p == ' ') {
      ++p;
    }
  }
  return count ? count : 1;
}