|
| 1 | +/*********************************** |
| 2 | + * AntPlus Bicycle Speed Display example |
| 3 | + * |
| 4 | + * Finds a nearby Bike Speed Sensor, pairs |
| 5 | + * to it and then reads the information |
| 6 | + * out via the serial port. |
| 7 | + * |
| 8 | + * Author Curtis Malainey |
| 9 | + ************************************/ |
| 10 | +#include <Arduino.h> |
| 11 | +#include "ANT.h" |
| 12 | +#include "ANTPLUS.h" |
| 13 | + |
| 14 | +#define BAUD_RATE 9600 |
| 15 | +#define CHANNEL_0 0 |
| 16 | + |
| 17 | +const uint8_t NETWORK_KEY[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77}; // get this from thisisant.com |
| 18 | + |
| 19 | +AntWithCallbacks ant = AntWithCallbacks(); |
| 20 | +AntPlusRouter router = AntPlusRouter(); |
| 21 | +ProfileBicycleSpeedDisplay bikeSpeed = ProfileBicycleSpeedDisplay(); |
| 22 | + |
| 23 | +void bicycleSpeedBaseDataPageHandler(AntRxDataResponse& msg, uintptr_t data); |
| 24 | +void batteryStatusDataPageHandler(BicycleSpeedBatteryStatus& msg, uintptr_t data); |
| 25 | +void motionAndSpeedDataPageHandler(BicycleSpeedMotionAndSpeed& msg, uintptr_t data); |
| 26 | +void cumulativeOperatingTimeDataPageHandler(BicycleSpeedCumulativeOperatingTime& msg, uintptr_t data); |
| 27 | +void defaultDataPageHandler(BicycleSpeedDefault& msg, uintptr_t data); |
| 28 | +void manufacturerIDDataPageHandler(BicycleSpeedManufacturerID& msg, uintptr_t data); |
| 29 | +void productIDDataPageHandler(BicycleSpeedProductID& msg, uintptr_t data); |
| 30 | + |
| 31 | +void bicycleSpeedFlags(uint8_t flags); |
| 32 | +void bicycleSpeedBatteryStatus(uint8_t flags); |
| 33 | +void printStatus(uint8_t status); |
| 34 | + |
| 35 | +void setup() { |
| 36 | + Serial1.begin(BAUD_RATE); |
| 37 | + ant.setSerial(Serial1); |
| 38 | + delay(15000); |
| 39 | + |
| 40 | + router.setDriver(&ant); // never touch ant again |
| 41 | + router.setAntPlusNetworkKey(NETWORK_KEY); |
| 42 | + router.setProfile(CHANNEL_0, &bikeSpeed); |
| 43 | + // Delay after initial setup to wait for user to connect on serial |
| 44 | + |
| 45 | + Serial.begin(BAUD_RATE); |
| 46 | + Serial.println("Running"); |
| 47 | + bikeSpeed.onDataPage(bicycleSpeedBaseDataPageHandler); |
| 48 | + bikeSpeed.onBicycleSpeedBatteryStatus(batteryStatusDataPageHandler); |
| 49 | + bikeSpeed.onBicycleSpeedMotionAndSpeed(motionAndSpeedDataPageHandler); |
| 50 | + bikeSpeed.onBicycleSpeedCumulativeOperatingTime(cumulativeOperatingTimeDataPageHandler); |
| 51 | + bikeSpeed.onBicycleSpeedDefault(defaultDataPageHandler); |
| 52 | + bikeSpeed.onBicycleSpeedManufacturerID(manufacturerIDDataPageHandler); |
| 53 | + bikeSpeed.onBicycleSpeedProductID(productIDDataPageHandler); |
| 54 | + bikeSpeed.begin(); |
| 55 | + // wait for pair to complete |
| 56 | + uint8_t status = bikeSpeed.waitForPair(); |
| 57 | + // print channel status |
| 58 | + Serial.println("==========================="); |
| 59 | + printStatus(status); |
| 60 | + Serial.print("Device Number: "); |
| 61 | + Serial.println(bikeSpeed.getDeviceNumber()); |
| 62 | + Serial.print("Transmisison Type: "); |
| 63 | + Serial.println(bikeSpeed.getTransmissionType()); |
| 64 | +} |
| 65 | + |
| 66 | +void loop() { |
| 67 | + router.loop(); |
| 68 | +} |
| 69 | + |
| 70 | +void batteryStatusDataPageHandler(BicycleSpeedBatteryStatus& msg, uintptr_t data) { |
| 71 | + Serial.print("Fractional Battery Voltage: "); |
| 72 | + Serial.println(msg.getFractionalBatteryVoltage()); |
| 73 | + Serial.print("Coarse Battery Voltage: "); |
| 74 | + Serial.println(msg.getCoarseBatteryVoltage()); |
| 75 | + Serial.print("Battery Status: "); |
| 76 | + bicycleSpeedBatteryStatus(msg.getBatteryStatus()); |
| 77 | +} |
| 78 | + |
| 79 | +void motionAndSpeedDataPageHandler(BicycleSpeedMotionAndSpeed& msg, uintptr_t data) { |
| 80 | + Serial.print("Flags: "); |
| 81 | + bicycleSpeedFlags(msg.getFlags()); |
| 82 | +} |
| 83 | + |
| 84 | +void cumulativeOperatingTimeDataPageHandler(BicycleSpeedCumulativeOperatingTime& msg, uintptr_t data) { |
| 85 | + Serial.print("Cumulative Operating Time: "); |
| 86 | + Serial.println(msg.getCumulativeOperatingTime()); |
| 87 | +} |
| 88 | + |
| 89 | +void defaultDataPageHandler(BicycleSpeedDefault& msg, uintptr_t data) { |
| 90 | + // All fields are reserved |
| 91 | +} |
| 92 | + |
| 93 | +void manufacturerIDDataPageHandler(BicycleSpeedManufacturerID& msg, uintptr_t data) { |
| 94 | + Serial.print("Manufacturer ID LSB: "); |
| 95 | + Serial.println(msg.getManufacturerId()); |
| 96 | + Serial.print("Serial Number: "); |
| 97 | + Serial.println(msg.getSerialNumber()); |
| 98 | +} |
| 99 | + |
| 100 | +void productIDDataPageHandler(BicycleSpeedProductID& msg, uintptr_t data) { |
| 101 | + Serial.print("Hardware Version: "); |
| 102 | + Serial.println(msg.getHardwareVersion()); |
| 103 | + Serial.print("Software Version: "); |
| 104 | + Serial.println(msg.getSoftwareVersion()); |
| 105 | + Serial.print("Model Number: "); |
| 106 | + Serial.println(msg.getModelNumber()); |
| 107 | +} |
| 108 | + |
| 109 | +void bicycleSpeedBaseDataPageHandler(AntRxDataResponse& msg, uintptr_t data) { |
| 110 | + BicycleSpeedBaseMainDataPage dp = BicycleSpeedBaseMainDataPage(msg); |
| 111 | + Serial.println("==========================="); |
| 112 | + Serial.print("DataPage: "); |
| 113 | + Serial.println(dp.getDataPageNumber()); |
| 114 | + Serial.print("Bike Speed Event Time: "); |
| 115 | + Serial.println(dp.getBikeSpeedEventTime()); |
| 116 | + Serial.print("Cumulative Speed Revolution Count: "); |
| 117 | + Serial.println(dp.getCumulativeSpeedRevolutionCount()); |
| 118 | +} |
| 119 | + |
| 120 | +void printStatus(uint8_t status) { |
| 121 | + Serial.print("Channel Status: "); |
| 122 | + switch (status) { |
| 123 | + case CHANNEL_STATUS_UNASSIGNED: |
| 124 | + Serial.println("Unassigned"); |
| 125 | + break; |
| 126 | + case CHANNEL_STATUS_ASSIGNED: |
| 127 | + Serial.println("Assigned"); |
| 128 | + break; |
| 129 | + case CHANNEL_STATUS_SEARCHING: |
| 130 | + Serial.println("Searching"); |
| 131 | + break; |
| 132 | + case CHANNEL_STATUS_TRACKING: |
| 133 | + Serial.println("Tracking"); |
| 134 | + break; |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +void bicycleSpeedFlags(uint8_t flags) { |
| 139 | + if (flags & ANTPLUS_BICYCLESPEED_DATAPAGE_MOTIONANDSPEED_FLAGS_STOPINDICATOR) { |
| 140 | + Serial.print("Motion Stopped"); |
| 141 | + } |
| 142 | + Serial.println(); |
| 143 | +} |
| 144 | + |
| 145 | +void bicycleSpeedBatteryStatus(uint8_t flags) { |
| 146 | + switch (flags) { |
| 147 | + case ANTPLUS_BICYCLESPEED_DATAPAGE_BATTERSTATUS_BATTERYSTATUS_NEW: |
| 148 | + Serial.println("New"); |
| 149 | + break; |
| 150 | + case ANTPLUS_BICYCLESPEED_DATAPAGE_BATTERSTATUS_BATTERYSTATUS_GOOD: |
| 151 | + Serial.println("Good"); |
| 152 | + break; |
| 153 | + case ANTPLUS_BICYCLESPEED_DATAPAGE_BATTERSTATUS_BATTERYSTATUS_OK: |
| 154 | + Serial.println("Ok"); |
| 155 | + break; |
| 156 | + case ANTPLUS_BICYCLESPEED_DATAPAGE_BATTERSTATUS_BATTERYSTATUS_LOW: |
| 157 | + Serial.println("Low"); |
| 158 | + break; |
| 159 | + case ANTPLUS_BICYCLESPEED_DATAPAGE_BATTERSTATUS_BATTERYSTATUS_CRITICAL: |
| 160 | + Serial.println("Critical"); |
| 161 | + break; |
| 162 | + case ANTPLUS_BICYCLESPEED_DATAPAGE_BATTERSTATUS_BATTERYSTATUS_INVALID: |
| 163 | + Serial.println("Invalid"); |
| 164 | + break; |
| 165 | + default: |
| 166 | + Serial.println("Reserved/Unknown"); |
| 167 | + break; |
| 168 | + } |
| 169 | +} |
0 commit comments