|
| 1 | +/*********************************** |
| 2 | + * AntPlus HRMonitor example |
| 3 | + * |
| 4 | + * Implements a HR Monitor sensor with |
| 5 | + * mocked data to it and then reports |
| 6 | + * all events through the serial port |
| 7 | + * |
| 8 | + * This is a minimum viable example |
| 9 | + * all additional datapages are optional |
| 10 | + * and are enabled through flags passed |
| 11 | + * into the profile |
| 12 | + * |
| 13 | + * It is highly recommended you read the documenation at |
| 14 | + * https://github.com/cujomalainey/antplus-arduino/wiki/HeartRate-Monitor-Profile |
| 15 | + * |
| 16 | + * Author Curtis Malainey |
| 17 | + ************************************/ |
| 18 | +#include <Arduino.h> |
| 19 | +#include "ANT.h" |
| 20 | +#include "ANTPLUS.h" |
| 21 | + |
| 22 | +#define BAUD_RATE 9600 |
| 23 | +#define CHANNEL_0 0 |
| 24 | + |
| 25 | +const uint8_t NETWORK_KEY[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77}; // get this from thisisant.com |
| 26 | + |
| 27 | +uint32_t flags = 0; |
| 28 | +AntWithCallbacks ant; |
| 29 | +AntPlusRouter router; |
| 30 | +ProfileHeartRateMonitor hr(123, 0, flags); |
| 31 | + |
| 32 | +// void batteryStatusDataPageHandler(HeartRateBatteryStatusMsg& msg, uintptr_t data); |
| 33 | +// void capabilitiesDataPageHandler(HeartRateCapabilitiesMsg& msg, uintptr_t data); |
| 34 | +// void cumulativeOperatingTimeDataPageHandler(HeartRateCumulativeOperatingTimeMsg& msg, uintptr_t data); |
| 35 | +void defaultDataPageHandler(HeartRateDefaultMsg& msg, uintptr_t data); |
| 36 | +void manufacturerInformationDataPageHandler(HeartRateManufacturerInformationMsg& msg, uintptr_t data); |
| 37 | +// void previousHeartBeatDataPageHandler(HeartRatePreviousHeartBeatMsg& msg, uintptr_t data); |
| 38 | +void productInformationDataPageHandler(HeartRateProductInformationMsg& msg, uintptr_t data); |
| 39 | +// void swimIntervalSummary(HeartRateSwimIntervalSummaryMsg& msg, uintptr_t data); |
| 40 | + |
| 41 | +void populateBaseHeartRate(HeartRateBaseMainDataPageMsg& msg); |
| 42 | + |
| 43 | +void setup() { |
| 44 | + Serial1.begin(BAUD_RATE); |
| 45 | + ant.setSerial(Serial1); |
| 46 | + delay(15000); |
| 47 | + Serial.begin(BAUD_RATE); |
| 48 | + Serial.println("Running"); |
| 49 | + |
| 50 | + router.setDriver(&ant); // never touch ant again |
| 51 | + router.setAntPlusNetworkKey(NETWORK_KEY); |
| 52 | + router.setProfile(CHANNEL_0, &hr); |
| 53 | + // Delay after initial setup to wait for user to connect on serial |
| 54 | + |
| 55 | + // hr.createHeartRateBatteryStatusMsg(batteryStatusDataPageHandler); |
| 56 | + // hr.createHeartRateCapabilitiesMsg(capabilitiesDataPageHandler); |
| 57 | + // hr.createHeartRateCumulativeOperatingTimeMsg(cumulativeOperatingTimeDataPageHandler); |
| 58 | + hr.createHeartRateDefaultMsg(defaultDataPageHandler); |
| 59 | + hr.createHeartRateManufacturerInformationMsg(manufacturerInformationDataPageHandler); |
| 60 | + // hr.createHeartRatePreviousHeartBeatMsg(previousHeartBeatDataPageHandler); |
| 61 | + hr.createHeartRateProductInformationMsg(productInformationDataPageHandler); |
| 62 | + // hr.createHeartRateSwimIntervalSummaryMsg(swimIntervalSummary); |
| 63 | + Serial.println("==========================="); |
| 64 | + hr.begin(); |
| 65 | +} |
| 66 | + |
| 67 | +void loop() { |
| 68 | + // Call this very frequently |
| 69 | + router.loop(); |
| 70 | +} |
| 71 | + |
| 72 | +void printDpMsg(int dp, const char* s) { |
| 73 | + Serial.print("Sending DataPage: "); |
| 74 | + Serial.print(dp); |
| 75 | + Serial.print(" - "); |
| 76 | + Serial.println(s); |
| 77 | +} |
| 78 | + |
| 79 | +/* Optional */ |
| 80 | +// void batteryStatusDataPageHandler(HeartRateBatteryStatusMsg& msg, uintptr_t data) { |
| 81 | +// printDpMsg(7, "Battery Status"); |
| 82 | +// populateBaseHeartRate(msg); |
| 83 | +// } |
| 84 | + |
| 85 | +/* Optional */ |
| 86 | +// void capabilitiesDataPageHandler(HeartRateCapabilitiesMsg& msg, uintptr_t data) { |
| 87 | +// printDpMsg(6, "Capabilities"); |
| 88 | +// populateBaseHeartRate(msg); |
| 89 | +// } |
| 90 | + |
| 91 | +/* Optional */ |
| 92 | +// void cumulativeOperatingTimeDataPageHandler(HeartRateCumulativeOperatingTimeMsg& msg, uintptr_t data) { |
| 93 | +// printDpMsg(1, "Cumulative Operating Time"); |
| 94 | +// populateBaseHeartRate(msg); |
| 95 | +// } |
| 96 | + |
| 97 | +void defaultDataPageHandler(HeartRateDefaultMsg& msg, uintptr_t data) { |
| 98 | + // All fields are reserved |
| 99 | + printDpMsg(0, "Default"); |
| 100 | + populateBaseHeartRate(msg); |
| 101 | +} |
| 102 | + |
| 103 | +void manufacturerInformationDataPageHandler(HeartRateManufacturerInformationMsg& msg, uintptr_t data) { |
| 104 | + printDpMsg(2, "Manufacturer Information"); |
| 105 | + populateBaseHeartRate(msg); |
| 106 | + msg.setManufacturerIdLsb(78); |
| 107 | + msg.setSerialNumber(0xabcd); |
| 108 | +} |
| 109 | + |
| 110 | +/* Optional */ |
| 111 | +// void previousHeartBeatDataPageHandler(HeartRatePreviousHeartBeatMsg& msg, uintptr_t data) { |
| 112 | +// printDpMsg(4, "Previous Heart Beat"); |
| 113 | +// populateBaseHeartRate(msg); |
| 114 | +// } |
| 115 | + |
| 116 | +void productInformationDataPageHandler(HeartRateProductInformationMsg& msg, uintptr_t data) { |
| 117 | + printDpMsg(3, "Product Information"); |
| 118 | + populateBaseHeartRate(msg); |
| 119 | + msg.setHardwareVersion(1); |
| 120 | + msg.setSoftwareVersion(2); |
| 121 | + msg.setModelNumber(3); |
| 122 | +} |
| 123 | + |
| 124 | +/* Optional */ |
| 125 | +// void swimIntervalSummary(HeartRateSwimIntervalSummaryMsg& msg, uintptr_t data) { |
| 126 | +// printDpMsg(5, "Swim Interval"); |
| 127 | +// populateBaseHeartRate(msg); |
| 128 | +// } |
| 129 | + |
| 130 | +void populateBaseHeartRate(HeartRateBaseMainDataPageMsg& msg) { |
| 131 | + static uint8_t toggle = 0; |
| 132 | + static uint8_t hr = 0; |
| 133 | + static uint16_t eventTime = 0; |
| 134 | + static uint8_t count = 0; |
| 135 | + msg.setPageChangeToggle(toggle++ < 4); |
| 136 | + msg.setComputedHeartRate(20*sin(hr++) + 120); |
| 137 | + msg.setHeartBeatEventTime(eventTime); |
| 138 | + msg.setHeartBeatCount(count++); |
| 139 | + |
| 140 | + if (toggle >= 8) { |
| 141 | + toggle = 0; |
| 142 | + } |
| 143 | + eventTime += 120; |
| 144 | +} |
0 commit comments