From 5e492ea925f705ff59a1b468a779e99bfef45cc0 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Tue, 13 Apr 2021 10:45:35 -0700 Subject: [PATCH] Modern cast cleanup (#462) * static_cast * cleanup and building --- .../sevensegment/NeoSegmentFade/NeoSegmentFade.ino | 2 +- src/NeoPixelBrightnessBus.h | 2 +- src/internal/HtmlColor.h | 2 +- src/internal/NeoArmMethod.h | 14 +++++++------- src/internal/NeoBitmapFile.h | 8 ++++---- src/internal/NeoBuffer.h | 4 ++-- src/internal/NeoEsp8266DmaMethod.h | 14 +++++++------- src/internal/NeoSpriteSheet.h | 4 ++-- src/internal/Rgb48Color.cpp | 14 +++++++------- src/internal/RgbColor.cpp | 14 +++++++------- src/internal/RgbwColor.cpp | 2 +- src/internal/SegmentDigit.cpp | 2 +- 12 files changed, 41 insertions(+), 41 deletions(-) diff --git a/examples/sevensegment/NeoSegmentFade/NeoSegmentFade.ino b/examples/sevensegment/NeoSegmentFade/NeoSegmentFade.ino index 588af8d..fd74cad 100644 --- a/examples/sevensegment/NeoSegmentFade/NeoSegmentFade.ino +++ b/examples/sevensegment/NeoSegmentFade/NeoSegmentFade.ino @@ -32,7 +32,7 @@ NeoPixelAnimator animations(Animation_COUNT); void CycleAnimation(const AnimationParam& param) { // calculate which segment should be on using the animation progress - uint8_t bitfield = 1 << ( (uint8_t)(param.progress * LedSegment_G) % LedSegment_G); + uint8_t bitfield = 1 << ( static_cast(param.progress * LedSegment_G) % LedSegment_G); // instant a digit with that segment on SevenSegDigit digit(bitfield, brightness); // apply it to the strip diff --git a/src/NeoPixelBrightnessBus.h b/src/NeoPixelBrightnessBus.h index 3bea54b..6b93a65 100644 --- a/src/NeoPixelBrightnessBus.h +++ b/src/NeoPixelBrightnessBus.h @@ -88,7 +88,7 @@ public: // Only update if there is a change if (brightness != _brightness) { - uint16_t scale = (((uint16_t)brightness + 1) << 8) / ((uint16_t)_brightness + 1); + uint16_t scale = ((static_cast(brightness) + 1) << 8) / (static_cast(_brightness) + 1); // scale existing pixels // diff --git a/src/internal/HtmlColor.h b/src/internal/HtmlColor.h index 238d4ac..25329eb 100644 --- a/src/internal/HtmlColor.h +++ b/src/internal/HtmlColor.h @@ -90,7 +90,7 @@ struct HtmlColor // ------------------------------------------------------------------------ HtmlColor(const RgbColor& color) { - Color = (uint32_t)color.R << 16 | (uint32_t)color.G << 8 | (uint32_t)color.B; + Color = static_cast(color.R) << 16 | static_cast(color.G) << 8 | static_cast(color.B); } // ------------------------------------------------------------------------ diff --git a/src/internal/NeoArmMethod.h b/src/internal/NeoArmMethod.h index d08d239..b0199ae 100644 --- a/src/internal/NeoArmMethod.h +++ b/src/internal/NeoArmMethod.h @@ -709,9 +709,9 @@ typedef NeoArm800KbpsMethod NeoArmApa106Method; class NeoArmOtherSpeedProps800KbpsBase { public: - static const uint32_t CyclesT0h = ((uint32_t)(0.40 * ARM_OTHER_SCALE + 0.5) - (5 * ARM_OTHER_INST)); - static const uint32_t CyclesT1h = ((uint32_t)(0.80 * ARM_OTHER_SCALE + 0.5) - (5 * ARM_OTHER_INST)); - static const uint32_t Cycles = ((uint32_t)(1.25 * ARM_OTHER_SCALE + 0.5) - (5 * ARM_OTHER_INST)); + static const uint32_t CyclesT0h = static_cast((0.40 * ARM_OTHER_SCALE + 0.5) - (5 * ARM_OTHER_INST)); + static const uint32_t CyclesT1h = static_cast((0.80 * ARM_OTHER_SCALE + 0.5) - (5 * ARM_OTHER_INST)); + static const uint32_t Cycles = static_cast((1.25 * ARM_OTHER_SCALE + 0.5) - (5 * ARM_OTHER_INST)); }; class NeoArmOtherSpeedPropsWs2812x : public NeoArmOtherSpeedProps800KbpsBase @@ -747,9 +747,9 @@ public: class NeoArmOtherSpeedProps400Kbps { public: - static const uint32_t CyclesT0h = ((uint32_t)(0.50 * ARM_OTHER_SCALE + 0.5) - (5 * ARM_OTHER_INST)); - static const uint32_t CyclesT1h = ((uint32_t)(1.20 * ARM_OTHER_SCALE + 0.5) - (5 * ARM_OTHER_INST)); - static const uint32_t Cycles = ((uint32_t)(2.50 * ARM_OTHER_SCALE + 0.5) - (5 * ARM_OTHER_INST)); + static const uint32_t CyclesT0h = static_cast((0.50 * ARM_OTHER_SCALE + 0.5) - (5 * ARM_OTHER_INST)); + static const uint32_t CyclesT1h = static_cast((1.20 * ARM_OTHER_SCALE + 0.5) - (5 * ARM_OTHER_INST)); + static const uint32_t Cycles = static_cast((2.50 * ARM_OTHER_SCALE + 0.5) - (5 * ARM_OTHER_INST)); static const uint32_t ResetTimeUs = 50; }; @@ -773,7 +773,7 @@ public: uint8_t mask; pmc_set_writeprotect(false); - pmc_enable_periph_clk((uint32_t)TC3_IRQn); + pmc_enable_periph_clk(static_cast(TC3_IRQn)); TC_Configure(TC1, 0, TC_CMR_WAVE | TC_CMR_WAVSEL_UP | TC_CMR_TCCLKS_TIMER_CLOCK1); diff --git a/src/internal/NeoBitmapFile.h b/src/internal/NeoBitmapFile.h index 2f0718e..467d55d 100644 --- a/src/internal/NeoBitmapFile.h +++ b/src/internal/NeoBitmapFile.h @@ -206,7 +206,7 @@ public: { for (int16_t x = 0; x < wSrc && indexPixel < destPixelCount; x++, indexPixel++) { - if ((uint16_t)xSrc < _width) + if (static_cast(xSrc) < _width) { if (readPixel(&color)) { @@ -255,7 +255,7 @@ public: { uint16_t indexDest = layoutMap(xDest + x, yDest + y); - if ((uint16_t)xFile < _width) + if (static_cast(xFile) < _width) { if (readPixel(&color)) { @@ -311,7 +311,7 @@ private: { x = 0; } - else if ((uint16_t)x >= _width) + else if (static_cast(x) >= _width) { x = _width - 1; } @@ -324,7 +324,7 @@ private: { y = 0; } - else if ((uint16_t)y >= _height) + else if (static_cast(y) >= _height) { y = _height - 1; } diff --git a/src/internal/NeoBuffer.h b/src/internal/NeoBuffer.h index 996a778..d904e87 100644 --- a/src/internal/NeoBuffer.h +++ b/src/internal/NeoBuffer.h @@ -162,9 +162,9 @@ private: uint16_t result = PixelIndex_OutOfBounds; if (x >= 0 && - (uint16_t)x < Width() && + static_cast(x) < Width() && y >= 0 && - (uint16_t)y < Height()) + static_cast(y) < Height()) { result = x + y * Width(); } diff --git a/src/internal/NeoEsp8266DmaMethod.h b/src/internal/NeoEsp8266DmaMethod.h index 747a5bd..70d44fc 100644 --- a/src/internal/NeoEsp8266DmaMethod.h +++ b/src/internal/NeoEsp8266DmaMethod.h @@ -314,9 +314,9 @@ public: _i2sBufDesc[indexDesc].sub_sof = 0; _i2sBufDesc[indexDesc].datalen = blockSize; _i2sBufDesc[indexDesc].blocksize = blockSize; - _i2sBufDesc[indexDesc].buf_ptr = (uint32_t)is2Buffer; + _i2sBufDesc[indexDesc].buf_ptr = reinterpret_cast(is2Buffer); _i2sBufDesc[indexDesc].unused = 0; - _i2sBufDesc[indexDesc].next_link_ptr = (uint32_t)&(_i2sBufDesc[indexDesc + 1]); + _i2sBufDesc[indexDesc].next_link_ptr = reinterpret_cast(&(_i2sBufDesc[indexDesc + 1])); is2Buffer += blockSize; is2BufferSize -= blockSize; @@ -330,15 +330,15 @@ public: _i2sBufDesc[indexDesc].sub_sof = 0; _i2sBufDesc[indexDesc].datalen = sizeof(_i2sZeroes); _i2sBufDesc[indexDesc].blocksize = sizeof(_i2sZeroes); - _i2sBufDesc[indexDesc].buf_ptr = (uint32_t)_i2sZeroes; + _i2sBufDesc[indexDesc].buf_ptr = reinterpret_cast(_i2sZeroes); _i2sBufDesc[indexDesc].unused = 0; - _i2sBufDesc[indexDesc].next_link_ptr = (uint32_t)&(_i2sBufDesc[indexDesc + 1]); + _i2sBufDesc[indexDesc].next_link_ptr = reinterpret_cast(&(_i2sBufDesc[indexDesc + 1])); } // the first state block will trigger the interrupt _i2sBufDesc[indexDesc - 2].eof = 1; // the last state block will loop to the first state block by defualt - _i2sBufDesc[indexDesc - 1].next_link_ptr = (uint32_t)&(_i2sBufDesc[indexDesc - 2]); + _i2sBufDesc[indexDesc - 1].next_link_ptr = reinterpret_cast(&(_i2sBufDesc[indexDesc - 2])); // setup the rest of i2s DMA // @@ -476,7 +476,7 @@ private: // data block has pending data waiting to send, prepare it // point last state block to top - (finished_item + 1)->next_link_ptr = (uint32_t)(s_this->_i2sBufDesc); + (finished_item + 1)->next_link_ptr = reinterpret_cast(s_this->_i2sBufDesc); s_this->_dmaState = NeoDmaState_Sending; } @@ -489,7 +489,7 @@ private: // the data block had actual data sent // point last state block to first state block thus // just looping and not sending the data blocks - (finished_item + 1)->next_link_ptr = (uint32_t)(finished_item); + (finished_item + 1)->next_link_ptr = reinterpret_cast(finished_item); s_this->_dmaState = NeoDmaState_Zeroing; } diff --git a/src/internal/NeoSpriteSheet.h b/src/internal/NeoSpriteSheet.h index 83fb978..73fdde3 100644 --- a/src/internal/NeoSpriteSheet.h +++ b/src/internal/NeoSpriteSheet.h @@ -152,9 +152,9 @@ private: if (indexSprite < _spriteCount && x >= 0 && - (uint16_t)x < SpriteWidth() && + static_cast(x) < SpriteWidth() && y >= 0 && - (uint16_t)y < SpriteHeight()) + static_cast(y) < SpriteHeight()) { result = x + y * SpriteWidth() + indexSprite * _spriteHeight * SpriteWidth(); } diff --git a/src/internal/Rgb48Color.cpp b/src/internal/Rgb48Color.cpp index 3743ac5..ea03f92 100644 --- a/src/internal/Rgb48Color.cpp +++ b/src/internal/Rgb48Color.cpp @@ -49,9 +49,9 @@ Rgb48Color::Rgb48Color(const HslColor& color) _HslToRgb(color, &r, &g, &b); - R = (uint16_t)(r * Max); - G = (uint16_t)(g * Max); - B = (uint16_t)(b * Max); + R = static_cast(r * Max); + G = static_cast(g * Max); + B = static_cast(b * Max); } Rgb48Color::Rgb48Color(const HsbColor& color) @@ -62,14 +62,14 @@ Rgb48Color::Rgb48Color(const HsbColor& color) _HsbToRgb(color, &r, &g, &b); - R = (uint16_t)(r * Max); - G = (uint16_t)(g * Max); - B = (uint16_t)(b * Max); + R = static_cast(r * Max); + G = static_cast(g * Max); + B = static_cast(b * Max); } uint16_t Rgb48Color::CalculateBrightness() const { - return (uint16_t)(((uint32_t)R + (uint32_t)G + (uint32_t)B) / 3); + return static_cast((static_cast(R) + static_cast(G) + static_cast(B)) / 3); } Rgb48Color Rgb48Color::Dim(uint16_t ratio) const diff --git a/src/internal/RgbColor.cpp b/src/internal/RgbColor.cpp index 99f4e94..406d78e 100644 --- a/src/internal/RgbColor.cpp +++ b/src/internal/RgbColor.cpp @@ -65,9 +65,9 @@ RgbColor::RgbColor(const HslColor& color) _HslToRgb(color, &r, &g, &b); - R = (uint8_t)(r * Max); - G = (uint8_t)(g * Max); - B = (uint8_t)(b * Max); + R = static_cast(r * Max); + G = static_cast(g * Max); + B = static_cast(b * Max); } RgbColor::RgbColor(const HsbColor& color) @@ -78,14 +78,14 @@ RgbColor::RgbColor(const HsbColor& color) _HsbToRgb(color, &r, &g, &b); - R = (uint8_t)(r * Max); - G = (uint8_t)(g * Max); - B = (uint8_t)(b * Max); + R = static_cast(r * Max); + G = static_cast(g * Max); + B = static_cast(b * Max); } uint8_t RgbColor::CalculateBrightness() const { - return (uint8_t)(((uint16_t)R + (uint16_t)G + (uint16_t)B) / 3); + return static_cast((static_cast(R) + static_cast(G) + static_cast(B)) / 3); } RgbColor RgbColor::Dim(uint8_t ratio) const diff --git a/src/internal/RgbwColor.cpp b/src/internal/RgbwColor.cpp index d73c7c9..10b3727 100644 --- a/src/internal/RgbwColor.cpp +++ b/src/internal/RgbwColor.cpp @@ -57,7 +57,7 @@ RgbwColor::RgbwColor(const HsbColor& color) uint8_t RgbwColor::CalculateBrightness() const { - uint8_t colorB = (uint8_t)(((uint16_t)R + (uint16_t)G + (uint16_t)B) / 3); + uint8_t colorB = static_cast((static_cast(R) + static_cast(G) + static_cast(B)) / 3); if (W > colorB) { return W; diff --git a/src/internal/SegmentDigit.cpp b/src/internal/SegmentDigit.cpp index 0348259..b310f59 100644 --- a/src/internal/SegmentDigit.cpp +++ b/src/internal/SegmentDigit.cpp @@ -116,7 +116,7 @@ uint8_t SevenSegDigit::CalculateBrightness() const sum += Segment[iSegment]; } - return (uint8_t)(sum / SegmentCount); + return static_cast(sum / SegmentCount); } void SevenSegDigit::Darken(uint8_t delta)