RecordOutput.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. This software is subject to the license described in the License.txt file
  3. included with this software distribution. You may not use this file except in compliance
  4. with this license.
  5. Copyright (c) Dynastream Innovations Inc. 2014
  6. All rights reserved.
  7. */
  8. #include "string.h"
  9. #include "stdlib.h"
  10. #include "math.h"
  11. #include "PowerDecoder.h"
  12. #include "RecordOutput.h"
  13. static double dRecordInterval;
  14. void ResamplerOutput_Init(BPSAMPLER *pstDecoder_, unsigned short usRecordInterval_, double dRecordInterval_, unsigned short usTimeBase_)
  15. {
  16. pstDecoder_->ucCadence = 0;
  17. pstDecoder_->dTotalEnergy = 0;
  18. pstDecoder_->fAccumEnergy = 0;
  19. pstDecoder_->fPendingEnergy = 0;
  20. pstDecoder_->fGapEnergy = 0;
  21. pstDecoder_->dTotalRotation = 0;
  22. pstDecoder_->fAccumRotation = 0;
  23. pstDecoder_->fPendingRotation = 0;
  24. pstDecoder_->fGapRotation = 0;
  25. pstDecoder_->ulEventTime = 0;
  26. pstDecoder_->ucRecordGapCount = 0;
  27. pstDecoder_->dLastRecordTime = 0;
  28. pstDecoder_->dLastMessageTime = 0;
  29. pstDecoder_->usRecordInterval = usRecordInterval_;
  30. pstDecoder_->usTimeBase = usTimeBase_;
  31. dRecordInterval = dRecordInterval_;
  32. }
  33. ///////////////////////////////////////////////////////////////////////
  34. // void RecordOutput(FILE *pfOut_, BPRESAMPLER *pstDecoder_)
  35. ///////////////////////////////////////////////////////////////////////
  36. //
  37. // This function pushes output records to catch up to the latest event.
  38. // It also updates the state as required.
  39. //
  40. ///////////////////////////////////////////////////////////////////////
  41. void RecordOutput(PowerRecordReceiver powerRecordReceiverPtr_, BPSAMPLER *pstDecoder_)
  42. {
  43. // Calculate average power and cadence over the recording interval.
  44. float fAveragePower = (float)(pstDecoder_->fPendingEnergy / dRecordInterval);
  45. float fAverageCadence = (float)(pstDecoder_->fPendingRotation * 60.0 / dRecordInterval);
  46. pstDecoder_->dTotalEnergy += pstDecoder_->fPendingEnergy;
  47. pstDecoder_->dTotalRotation += pstDecoder_->fPendingRotation;
  48. pstDecoder_->dLastRecordTime += dRecordInterval;
  49. pstDecoder_->ulLastRecordTime = (pstDecoder_->ulEventTime / pstDecoder_->usRecordInterval)*pstDecoder_->usRecordInterval;
  50. (*powerRecordReceiverPtr_)(pstDecoder_->dLastRecordTime, pstDecoder_->dTotalRotation, pstDecoder_->dTotalEnergy, fAverageCadence, fAveragePower);
  51. // If there was any recovered message outage, fill in here.
  52. RecordOutput_FillGap(powerRecordReceiverPtr_, pstDecoder_);
  53. }
  54. ///////////////////////////////////////////////////////////////////////
  55. // double RecordOutput_FillGap(FILE *pfOut_, BPRESAMPLER *pstDecoder_)
  56. ///////////////////////////////////////////////////////////////////////
  57. //
  58. // This function is called to fill the data record with energy/
  59. // rotation that's evidently occurred during a message gap.
  60. // If the gap is _too_ long then we shouldn't do this because
  61. // otherwise it could cause some pretty huge files to be generated.
  62. //
  63. ///////////////////////////////////////////////////////////////////////
  64. void RecordOutput_FillGap(PowerRecordReceiver powerRecordReceiverPtr_, BPSAMPLER *pstDecoder_)
  65. {
  66. int i;
  67. float fIncEnergy;
  68. float fIncRotation;
  69. float fAveragePower;
  70. float fAverageCadence;
  71. if (pstDecoder_->ucRecordGapCount > 0)
  72. {
  73. fIncEnergy = (float)(pstDecoder_->fGapEnergy / pstDecoder_->ucRecordGapCount);
  74. fIncRotation = (float)(pstDecoder_->fGapRotation / pstDecoder_->ucRecordGapCount);
  75. // These two things are broken out here for clarity.
  76. // With respect to the output generation they can simply be combined.
  77. fAveragePower = fIncEnergy / dRecordInterval;
  78. fAverageCadence = fIncRotation * 60.0f / dRecordInterval;
  79. for (i = 0; i < pstDecoder_->ucRecordGapCount; i++)
  80. {
  81. pstDecoder_->dTotalEnergy += fIncEnergy;
  82. pstDecoder_->dTotalRotation += fIncRotation;
  83. pstDecoder_->dLastRecordTime += dRecordInterval;
  84. (*powerRecordReceiverPtr_)(pstDecoder_->dLastRecordTime, pstDecoder_->dTotalRotation, pstDecoder_->dTotalEnergy, fAverageCadence, fAveragePower);
  85. }
  86. pstDecoder_->ucRecordGapCount = 0;
  87. }
  88. }