Skip to content

Commit

Permalink
Cleanup (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
AstaRom authored and awawa-dev committed Jun 5, 2023
1 parent 9126e94 commit 50196dd
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 57 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/push-master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
with:
release: '12.2.Rel1'

- name: Build packages
- name: Build packages
shell: bash
run: |
mkdir build
Expand Down Expand Up @@ -57,7 +57,7 @@ jobs:
needs: [HyperSerialPico]
runs-on: ubuntu-latest
permissions:
contents: write
contents: write
steps:
# generate environment variables
- name: Generate environment variables from version and tag
Expand Down
8 changes: 4 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ set(CMAKE_CXX_STANDARD 17)

IF(CMAKE_COMPILER_IS_GNUCC)
string(REGEX REPLACE "(\-O[011123456789])" "" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
string(REGEX REPLACE "(\-O[011123456789])" "" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
string(REGEX REPLACE "(\-O[011123456789])" "" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Og")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -Og")
ENDIF(CMAKE_COMPILER_IS_GNUCC)
Expand All @@ -47,15 +47,15 @@ set(HyperSerialPicoCompanionLibs FreeRTOS-Kernel FreeRTOS-Kernel-Heap1 pico_stdl
set(HyperSerialPicoCompanionIncludes ${CMAKE_SOURCE_DIR}/include ${CMAKE_SOURCE_DIR}/sdk/config)
file(MAKE_DIRECTORY ${CMAKE_SOURCE_DIR}/generated)
file(MAKE_DIRECTORY ${CMAKE_SOURCE_DIR}/firmwares)

macro(HyperSerialPicoTarget HyperSerialPicoTargetName)
add_executable(${HyperSerialPicoTargetName} ${CMAKE_SOURCE_DIR}/source/main.cpp)
target_include_directories(${HyperSerialPicoTargetName} PRIVATE ${HyperSerialPicoCompanionIncludes})
target_link_libraries(${HyperSerialPicoTargetName} ${HyperSerialPicoCompanionLibs})
pico_add_extra_outputs(${HyperSerialPicoTargetName})
pico_enable_stdio_usb(${HyperSerialPicoTargetName} 1)
pico_enable_stdio_uart(${HyperSerialPicoTargetName} 0)
pico_generate_pio_header(${HyperSerialPicoTargetName} ${CMAKE_SOURCE_DIR}/pio/neopixel.pio OUTPUT_DIR ${CMAKE_SOURCE_DIR}/generated)
pico_generate_pio_header(${HyperSerialPicoTargetName} ${CMAKE_SOURCE_DIR}/pio/neopixel.pio OUTPUT_DIR ${CMAKE_SOURCE_DIR}/generated)
add_custom_command(TARGET ${HyperSerialPicoTargetName} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_BINARY_DIR}/${HyperSerialPicoTargetName}.uf2 ${CMAKE_SOURCE_DIR}/firmwares)
endmacro()

Expand Down Expand Up @@ -84,5 +84,5 @@ ELSE()
target_compile_definitions("HyperSerialPico_sk6812Neutral_rev_multisegment_at_${SECOND_SEGMENT_INDEX}" PRIVATE -DNEOPIXEL_RGBW -DDATA_PIN=${OUTPUT_DATA_PIN} -DSECOND_SEGMENT_START_INDEX=${SECOND_SEGMENT_INDEX} -DSECOND_SEGMENT_REVERSED)
HyperSerialPicoTarget("HyperSerialPico_ws2812_rev_multisegment_at_${SECOND_SEGMENT_INDEX}")
target_compile_definitions("HyperSerialPico_ws2812_rev_multisegment_at_${SECOND_SEGMENT_INDEX}" PRIVATE -DNEOPIXEL_RGB -DDATA_PIN=${OUTPUT_DATA_PIN} -DSECOND_SEGMENT_START_INDEX=${SECOND_SEGMENT_INDEX} -DSECOND_SEGMENT_REVERSED)
ENDIF()
ENDIF()
ENDIF()
2 changes: 1 addition & 1 deletion include/calibration.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,5 +130,5 @@ class CalibrationConfig
printf(output);
}
} calibrationConfig;
#endif

#endif
71 changes: 35 additions & 36 deletions include/leds.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ struct ColorGrb
uint8_t B;
uint8_t R;
uint8_t G;


ColorGrb(uint8_t gray) :
R(gray), G(gray), B(gray)
Expand All @@ -116,7 +116,7 @@ struct ColorGrb
};

struct ColorGrbw
{
{
uint8_t W;
uint8_t B;
uint8_t R;
Expand All @@ -134,13 +134,13 @@ struct ColorGrbw
static bool isAlignedTo24()
{
return false;
};
};
};

struct ColorDotstartBgr
{
{
uint8_t brightness;
uint8_t B;
uint8_t B;
uint8_t G;
uint8_t R;

Expand All @@ -155,16 +155,16 @@ struct ColorDotstartBgr
};

class LedDriver
{
{
protected:

int ledsNumber;
int pin;
int clockPin;
int dmaSize;
uint8_t* buffer;
uint8_t* dma;
uint8_t* dma;

public:

LedDriver(int _ledsNumber, int _pin, int _dmaSize): LedDriver(_ledsNumber, _pin, 0, _dmaSize)
Expand All @@ -191,7 +191,7 @@ class LedDriver
free(dma);
if (LedDriverDmaReceiver == this)
LedDriverDmaReceiver = nullptr;
}
}

static LedDriver* LedDriverDmaReceiver;
};
Expand All @@ -213,21 +213,21 @@ class DmaClient
DmaClient()
{
PICO_DMA_CHANNEL = dma_claim_unused_channel(true);
isDmaBusy = false;
isDmaBusy = false;
lastRenderTime = 0;
};

~DmaClient()
{
for(int i = 0; i < 10 && isDmaBusy; i++)
busy_wait_us(500);
busy_wait_us(500);

dma_channel_abort(PICO_DMA_CHANNEL);
dma_channel_set_irq0_enabled(PICO_DMA_CHANNEL, false);
irq_set_enabled(DMA_IRQ_0, false);

dma_channel_unclaim(PICO_DMA_CHANNEL);
};
};

void dmaConfigure(PIO _selectedPIO, uint _sm)
{
Expand Down Expand Up @@ -270,9 +270,9 @@ class DmaClient
int wait = 200;
while(isDmaBusy && wait-- > 0)
busy_wait_us(50);

return !isDmaBusy;
}
}

bool isReady()
{
Expand All @@ -284,15 +284,15 @@ class DmaClient
if (dma_hw->ints0 & (1u<<DmaClient::PICO_DMA_CHANNEL))
{
dma_hw->ints0 = (1u<<DmaClient::PICO_DMA_CHANNEL);

lastRenderTime = time_us_64();
isDmaBusy = false;
}
}
};

class Neopixel : public LedDriver, public DmaClient
{
{

uint64_t resetTime;

Expand Down Expand Up @@ -329,12 +329,12 @@ class Neopixel : public LedDriver, public DmaClient
pio_sm_set_consecutive_pindirs(selectedPIO, stateIndex, _pin, std::max(lanes, 1), true);
sm_config_set_out_shift(&smConfig, false, true, (alignTo24) ? 24: 32);
sm_config_set_fifo_join(&smConfig, PIO_FIFO_JOIN_TX);
float div = clock_get_hz(clk_sys) / (800000 * 12);
float div = clock_get_hz(clk_sys) / (800000 * 12);
sm_config_set_clkdiv(&smConfig, div);
pio_sm_init(selectedPIO, stateIndex, programAddress, &smConfig);
pio_sm_set_enabled(selectedPIO, stateIndex, true);

initDmaPio(dmaSize / 4);
initDmaPio(dmaSize / 4);
}

uint8_t* getBufferMemory()
Expand All @@ -355,29 +355,29 @@ class Neopixel : public LedDriver, public DmaClient
if (currentTime < resetTime + lastRenderTime)
busy_wait_us(std::min(resetTime + lastRenderTime - currentTime, resetTime));

memcpy(dma, buffer, dmaSize);
memcpy(dma, buffer, dmaSize);

dma_channel_set_read_addr(PICO_DMA_CHANNEL, dma, true);

if (resetBuffer)
memset(buffer, 0, dmaSize);
}
}
};

template<int RESET_TIME, typename colorData>
class NeopixelType : public Neopixel
{
{
public:

NeopixelType(int _ledsNumber, int _pin) : Neopixel(0, RESET_TIME, _ledsNumber, _pin, _ledsNumber * sizeof(colorData), colorData::isAlignedTo24())
{
}

void SetPixel(int index, colorData color)
{
if (index >= ledsNumber)
return;

*(reinterpret_cast<colorData*>(buffer)+index) = color;
}

Expand All @@ -395,7 +395,7 @@ class NeopixelParallel

protected:
static int maxLeds;
const uint8_t myLaneMask;
const uint8_t myLaneMask;
static uint8_t* buffer;

public:
Expand Down Expand Up @@ -432,26 +432,26 @@ class NeopixelParallel
bool isReady()
{
return muxer->isReady();
}
}

void renderAllLanes()
{
muxer->renderDma(true);
}
}
};

template<int RESET_TIME, typename colorData>
class NeopixelParallelType : public NeopixelParallel
{
uint32_t lut[16];

public:
public:

NeopixelParallelType(int _ledsNumber, int _basePinForLanes) : NeopixelParallel(sizeof(colorData), RESET_TIME,
_ledsNumber, _basePinForLanes)
{
for (uint8_t a = 0; a < 16; a++)
{
{
uint8_t* target = reinterpret_cast<uint8_t*>(&(lut[a]));
for (uint8_t b = 0; b < 4; b++)
*(target++) = (uint8_t) ((a & (0b00000001 << b)) ? myLaneMask : 0);
Expand All @@ -462,16 +462,16 @@ class NeopixelParallelType : public NeopixelParallel
{
if (index >= maxLeds)
return;

uint8_t* source = reinterpret_cast<uint8_t*>(&color);
uint32_t* target = reinterpret_cast<uint32_t*>(&(buffer[(index + 1) * 8 * sizeof(colorData)]));

for(int i = 0; i < sizeof(colorData); i++)
{
*(--target) |= lut[ *(source) & 0b00001111];
{
*(--target) |= lut[ *(source) & 0b00001111];
*(--target) |= lut[ *(source++) >> 4];
}
}
}
};

class Dotstar : public LedDriver, public DmaClient
Expand All @@ -485,7 +485,7 @@ class Dotstar : public LedDriver, public DmaClient
LedDriver(_ledsNumber, _datapin, _clockpin, _dmaSize)
{
dmaConfigure(pio0, 0);
resetTime = _resetTime;
resetTime = _resetTime;

spi_init(spi_default, 10000000);
gpio_set_function(PICO_DEFAULT_SPI_RX_PIN, GPIO_FUNC_SPI);
Expand All @@ -496,7 +496,7 @@ class Dotstar : public LedDriver, public DmaClient
bi_decl(bi_1pin_with_name(PICO_DEFAULT_SPI_CSN_PIN, "SPI CS"));

initDmaSpi(_dmaSize);
}
}

uint8_t* getBufferMemory()
{
Expand Down Expand Up @@ -544,7 +544,7 @@ class DotstarType : public Dotstar
memset(buffer,0 ,4);
*(reinterpret_cast<colorData*>(buffer)+ledsNumber+1) = colorData(0xff);
renderDma();
}
}
};

Neopixel* NeopixelParallel::muxer = nullptr;
Expand All @@ -562,4 +562,3 @@ typedef NeopixelType<450, ColorGrbw> sk6812;
typedef NeopixelParallelType<300, ColorGrb> ws2812p;
typedef NeopixelParallelType<80, ColorGrbw> sk6812p;
typedef DotstarType<100, ColorDotstartBgr> apa102;

2 changes: 1 addition & 1 deletion pio/neopixel.pio
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
; SOFTWARE.


.program neopixel
.side_set 1
Expand Down
1 change: 0 additions & 1 deletion sdk/config/FreeRTOSConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,3 @@ to exclude the API function. */
/* A header file that defines trace macro can be included here. */

#endif /* FREERTOS_CONFIG_H */

20 changes: 8 additions & 12 deletions source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
#pragma message(VAR_NAME_VALUE(CLOCK_PIN))
#endif

#if defined(SECOND_SEGMENT_START_INDEX)
#if defined(SECOND_SEGMENT_START_INDEX)
#pragma message("Using parallel mode for segments")

#ifdef NEOPIXEL_RGBW
Expand All @@ -93,7 +93,7 @@
#else
#error "Parallel mode is unsupportd for selected LEDs configuration"
#endif

#pragma message(VAR_NAME_VALUE(LED_DRIVER))
#pragma message(VAR_NAME_VALUE(SECOND_SEGMENT_START_INDEX))
#pragma message(VAR_NAME_VALUE(LED_DRIVER2))
Expand All @@ -117,7 +117,7 @@ static void core1()
{
if (sem_acquire_timeout_us(&base.serialSemaphore, portMAX_DELAY))
{
processData();
processData();
}
}
}
Expand Down Expand Up @@ -145,14 +145,14 @@ static void core0( void *pvParameters )
}

static void serialEvent(void *)
{
sem_release(&base.receiverSemaphore);
{
sem_release(&base.receiverSemaphore);
}

int main(void)
{
stdio_init_all();

sem_init(&base.serialSemaphore, 0, 1);

sem_init(&base.receiverSemaphore, 0, 1);
Expand All @@ -167,11 +167,7 @@ int main(void)
NULL,
(configMAX_PRIORITIES - 1),
&base.processSerialHandle);
vTaskStartScheduler();

vTaskStartScheduler();
panic_unsupported();
}




0 comments on commit 50196dd

Please sign in to comment.