diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..bf3583d --- /dev/null +++ b/.clang-format @@ -0,0 +1,225 @@ +--- +Language: Cpp +# BasedOnStyle: LLVM +AccessModifierOffset: -2 +AlignAfterOpenBracket: Align +AlignArrayOfStructures: None +AlignConsecutiveAssignments: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + PadOperators: true +AlignConsecutiveBitFields: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + PadOperators: false +AlignConsecutiveDeclarations: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + PadOperators: false +AlignConsecutiveMacros: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + PadOperators: false +AlignEscapedNewlines: Right +AlignOperands: Align +AlignTrailingComments: + Kind: Always + OverEmptyLines: 0 +AllowAllArgumentsOnNextLine: true +AllowAllParametersOfDeclarationOnNextLine: true +AllowShortBlocksOnASingleLine: Never +AllowShortCaseLabelsOnASingleLine: false +AllowShortEnumsOnASingleLine: true +AllowShortFunctionsOnASingleLine: All +AllowShortIfStatementsOnASingleLine: Never +AllowShortLambdasOnASingleLine: All +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: false +AlwaysBreakTemplateDeclarations: MultiLine +AttributeMacros: + - __capability +BinPackArguments: true +BinPackParameters: true +BitFieldColonSpacing: Both +BraceWrapping: + AfterCaseLabel: false + AfterClass: false + AfterControlStatement: Never + AfterEnum: false + AfterExternBlock: false + AfterFunction: false + AfterNamespace: false + AfterObjCDeclaration: false + AfterStruct: false + AfterUnion: false + BeforeCatch: false + BeforeElse: false + BeforeLambdaBody: false + BeforeWhile: false + IndentBraces: false + SplitEmptyFunction: true + SplitEmptyRecord: true + SplitEmptyNamespace: true +BreakAfterAttributes: Never +BreakAfterJavaFieldAnnotations: false +BreakArrays: true +BreakBeforeBinaryOperators: None +BreakBeforeConceptDeclarations: Always +BreakBeforeBraces: Attach +BreakBeforeInlineASMColon: OnlyMultiline +BreakBeforeTernaryOperators: true +BreakConstructorInitializers: BeforeColon +BreakInheritanceList: BeforeColon +BreakStringLiterals: true +ColumnLimit: 80 +CommentPragmas: '^ IWYU pragma:' +CompactNamespaces: false +ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: true +DerivePointerAlignment: false +DisableFormat: false +EmptyLineAfterAccessModifier: Never +EmptyLineBeforeAccessModifier: LogicalBlock +ExperimentalAutoDetectBinPacking: false +FixNamespaceComments: true +ForEachMacros: + - foreach + - Q_FOREACH + - BOOST_FOREACH +IfMacros: + - KJ_IF_MAYBE +IncludeBlocks: Preserve +IncludeCategories: + - Regex: '^"(llvm|llvm-c|clang|clang-c)/' + Priority: 2 + SortPriority: 0 + CaseSensitive: false + - Regex: '^(<|"(gtest|gmock|isl|json)/)' + Priority: 3 + SortPriority: 0 + CaseSensitive: false + - Regex: '.*' + Priority: 1 + SortPriority: 0 + CaseSensitive: false +IncludeIsMainRegex: '(Test)?$' +IncludeIsMainSourceRegex: '' +IndentAccessModifiers: false +IndentCaseBlocks: false +IndentCaseLabels: false +IndentExternBlock: AfterExternBlock +IndentGotoLabels: true +IndentPPDirectives: None +IndentRequiresClause: true +IndentWidth: 4 +IndentWrappedFunctionNames: false +InsertBraces: false +InsertNewlineAtEOF: false +InsertTrailingCommas: None +IntegerLiteralSeparator: + Binary: 0 + BinaryMinDigits: 0 + Decimal: 0 + DecimalMinDigits: 0 + Hex: 0 + HexMinDigits: 0 +JavaScriptQuotes: Leave +JavaScriptWrapImports: true +KeepEmptyLinesAtTheStartOfBlocks: true +LambdaBodyIndentation: Signature +LineEnding: DeriveLF +MacroBlockBegin: '' +MacroBlockEnd: '' +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +ObjCBinPackProtocolList: Auto +ObjCBlockIndentWidth: 2 +ObjCBreakBeforeNestedBlockParam: true +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: true +PackConstructorInitializers: BinPack +PenaltyBreakAssignment: 2 +PenaltyBreakBeforeFirstCallParameter: 19 +PenaltyBreakComment: 300 +PenaltyBreakFirstLessLess: 120 +PenaltyBreakOpenParenthesis: 0 +PenaltyBreakString: 1000 +PenaltyBreakTemplateDeclaration: 10 +PenaltyExcessCharacter: 1000000 +PenaltyIndentedWhitespace: 0 +PenaltyReturnTypeOnItsOwnLine: 60 +PointerAlignment: Right +PPIndentWidth: -1 +QualifierAlignment: Leave +ReferenceAlignment: Pointer +ReflowComments: true +RemoveBracesLLVM: false +RemoveSemicolon: false +RequiresClausePosition: OwnLine +RequiresExpressionIndentation: OuterScope +SeparateDefinitionBlocks: Leave +ShortNamespaceLines: 1 +SortIncludes: CaseSensitive +SortJavaStaticImport: Before +SortUsingDeclarations: LexicographicNumeric +SpaceAfterCStyleCast: false +SpaceAfterLogicalNot: false +SpaceAfterTemplateKeyword: true +SpaceAroundPointerQualifiers: Default +SpaceBeforeAssignmentOperators: true +SpaceBeforeCaseColon: false +SpaceBeforeCpp11BracedList: false +SpaceBeforeCtorInitializerColon: true +SpaceBeforeInheritanceColon: true +SpaceBeforeParens: ControlStatements +SpaceBeforeParensOptions: + AfterControlStatements: true + AfterForeachMacros: true + AfterFunctionDefinitionName: false + AfterFunctionDeclarationName: false + AfterIfMacros: true + AfterOverloadedOperator: false + AfterRequiresInClause: false + AfterRequiresInExpression: false + BeforeNonEmptyParentheses: false +SpaceBeforeRangeBasedForLoopColon: true +SpaceBeforeSquareBrackets: false +SpaceInEmptyBlock: false +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 1 +SpacesInAngles: Never +SpacesInConditionalStatement: false +SpacesInContainerLiterals: true +SpacesInCStyleCastParentheses: false +SpacesInLineCommentPrefix: + Minimum: 1 + Maximum: -1 +SpacesInParentheses: false +SpacesInSquareBrackets: false +Standard: Latest +StatementAttributeLikeMacros: + - Q_EMIT +StatementMacros: + - Q_UNUSED + - QT_REQUIRE_VERSION +TabWidth: 8 +UseTab: Never +WhitespaceSensitiveMacros: + - BOOST_PP_STRINGIZE + - CF_SWIFT_NAME + - NS_SWIFT_NAME + - PP_STRINGIZE + - STRINGIZE +... + diff --git a/esp_ws28xx.c b/esp_ws28xx.c index d64fa17..27dd71a 100644 --- a/esp_ws28xx.c +++ b/esp_ws28xx.c @@ -1,35 +1,43 @@ #include "esp_ws28xx.h" -uint16_t* dma_buffer; -CRGB* ws28xx_pixels; +uint16_t *dma_buffer; +CRGB *ws28xx_pixels; static int n_of_leds, reset_delay, dma_buf_size; led_strip_model_t led_model; -static spi_settings_t spi_settings = {.host = SPI2_HOST, - .dma_chan = SPI_DMA_CH_AUTO, - .buscfg = - { - .miso_io_num = -1, - .sclk_io_num = -1, - .quadwp_io_num = -1, - .quadhd_io_num = -1, - }, - .devcfg = { - .clock_speed_hz = 3.2 * 1000 * 1000, // Clock out at 3.2 MHz - .mode = 0, // SPI mode 0 - .spics_io_num = -1, // CS pin - .queue_size = 1, - .command_bits = 0, - .address_bits = 0, - .flags = SPI_DEVICE_TXBIT_LSBFIRST, - }}; +static spi_settings_t spi_settings = { + .host = SPI2_HOST, + .dma_chan = SPI_DMA_CH_AUTO, + .buscfg = + { + .miso_io_num = -1, + .sclk_io_num = -1, + .quadwp_io_num = -1, + .quadhd_io_num = -1, + }, + .devcfg = + { + .clock_speed_hz = 3.2 * 1000 * 1000, // Clock out at 3.2 MHz + .mode = 0, // SPI mode 0 + .spics_io_num = -1, // CS pin + .queue_size = 1, + .command_bits = 0, + .address_bits = 0, + .flags = SPI_DEVICE_TXBIT_LSBFIRST, + }, +}; -esp_err_t ws28xx_init(int pin, led_strip_model_t model, int num_of_leds, CRGB** led_buffer_ptr) { +static const uint16_t timing_bits[16] = { + 0x1111, 0x7111, 0x1711, 0x7711, 0x1171, 0x7171, 0x1771, 0x7771, + 0x1117, 0x7117, 0x1717, 0x7717, 0x1177, 0x7177, 0x1777, 0x7777}; + +esp_err_t ws28xx_init(int pin, led_strip_model_t model, int num_of_leds, + CRGB **led_buffer_ptr) { esp_err_t err = ESP_OK; n_of_leds = num_of_leds; led_model = model; - dma_buf_size = - n_of_leds * 12 + (reset_delay + 1) * 2; // 12 bytes for each led + bytes for initial zero and reset state + // 12 bytes for each led + bytes for initial zero and reset state + dma_buf_size = n_of_leds * 12 + (reset_delay + 1) * 2; ws28xx_pixels = malloc(sizeof(CRGB) * n_of_leds); if (ws28xx_pixels == NULL) { return ESP_ERR_NO_MEM; @@ -37,20 +45,23 @@ esp_err_t ws28xx_init(int pin, led_strip_model_t model, int num_of_leds, CRGB** *led_buffer_ptr = ws28xx_pixels; spi_settings.buscfg.mosi_io_num = pin; spi_settings.buscfg.max_transfer_sz = dma_buf_size; - err = spi_bus_initialize(spi_settings.host, &spi_settings.buscfg, spi_settings.dma_chan); + err = spi_bus_initialize(spi_settings.host, &spi_settings.buscfg, + spi_settings.dma_chan); if (err != ESP_OK) { free(ws28xx_pixels); return err; } - err = spi_bus_add_device(spi_settings.host, &spi_settings.devcfg, &spi_settings.spi); + err = spi_bus_add_device(spi_settings.host, &spi_settings.devcfg, + &spi_settings.spi); if (err != ESP_OK) { free(ws28xx_pixels); return err; } - reset_delay = (model == WS2812B) ? 3 : 30; // insrease if something breaks. values are less that - // recommended in datasheets but seem stable - dma_buffer = heap_caps_malloc(dma_buf_size, - MALLOC_CAP_DMA); // Critical to be DMA memory. + // Insrease if something breaks. values are less than recommended in + // datasheets but seem stable + reset_delay = (model == WS2812B) ? 3 : 30; + // Critical to be DMA memory. + dma_buffer = heap_caps_malloc(dma_buf_size, MALLOC_CAP_DMA); if (dma_buffer == NULL) { free(ws28xx_pixels); return ESP_ERR_NO_MEM; @@ -65,15 +76,13 @@ void ws28xx_fill_all(CRGB color) { } esp_err_t ws28xx_update() { - uint16_t timing_bits[16] = {0x1111, 0x7111, 0x1711, 0x7711, 0x1171, 0x7171, 0x1771, 0x7771, - 0x1117, 0x7117, 0x1717, 0x7717, 0x1177, 0x7177, 0x1777, 0x7777}; esp_err_t err; int n = 0; memset(dma_buffer, 0, dma_buf_size); - dma_buffer[n++] = 0; for (int i = 0; i < n_of_leds; i++) { - uint32_t temp = ws28xx_pixels[i].num; // Data you want to write to each LEDs + // Data you want to write to each LEDs + uint32_t temp = ws28xx_pixels[i].num; if (led_model == WS2815) { // Red dma_buffer[n++] = timing_bits[0x0f & (temp >> 4)]; diff --git a/esp_ws28xx.h b/esp_ws28xx.h index 308d8f1..a371e73 100644 --- a/esp_ws28xx.h +++ b/esp_ws28xx.h @@ -1,9 +1,9 @@ #ifndef MAIN_ESP_WS28XX_H_ #define MAIN_ESP_WS28XX_H_ -#include -#include #include "driver/gpio.h" #include "driver/spi_master.h" +#include +#include typedef struct { union { @@ -42,7 +42,8 @@ typedef enum { WS2815, } led_strip_model_t; -esp_err_t ws28xx_init(int pin, led_strip_model_t model, int num_of_leds, CRGB** led_buffer_ptr); +esp_err_t ws28xx_init(int pin, led_strip_model_t model, int num_of_leds, + CRGB **led_buffer_ptr); void ws28xx_fill_all(CRGB color); esp_err_t ws28xx_update();