Skip to content

Instantly share code, notes, and snippets.

@MGDSStudio
Created April 21, 2023 13:25
Show Gist options
  • Save MGDSStudio/2f1516ca3ff87db321c149455c7483cd to your computer and use it in GitHub Desktop.
Save MGDSStudio/2f1516ca3ff87db321c149455c7483cd to your computer and use it in GitHub Desktop.
Controller for LED-strips with chips ws2811. The code doesn't use SPI and I2C.
#include <iostream>
#include <chrono>
#include <thread>
void switchOnLeds(int firstLed, int lastLed);
void digitalWrite(int pin, bool statement);
int main()
{
switchOnLeds(0, 4);
}
void switchOnLeds(int firstLed, int lastLed) {
const auto BIT_0_HIGH_VOLTAGE_TIME = std::chrono::nanoseconds(250); //nanoseconds to start transfer the logical 0 for ws2811 in fast mode
const auto BIT_0_LOW_VOLTAGE_TIME = std::chrono::nanoseconds(1000); //nanoseconds to end transfer the logical 0 for ws2811 in fast mode
const auto BIT_1_HIGH_VOLTAGE_TIME = std::chrono::nanoseconds(600); //nanoseconds to start transfer the logical 1 for ws2811 in fast mode
const auto BIT_1_LOW_VOLTAGE_TIME = std::chrono::nanoseconds(650); //nanoseconds to end transfer the logical 1 for ws2811 in fast mode
const int BITS_FOR_SINGLE_LED = 24; //how many bits contains a signal for a single LED
const int PIN = 4; //Output pin
const int LEDS = 10;
auto programStart = std::chrono::high_resolution_clock::now();
for (int i = 0; i < LEDS; i++) {
if (i >= firstLed && i <= lastLed) {
for (int bit = 0; bit < BITS_FOR_SINGLE_LED; bit++) {
digitalWrite(PIN, true);
std::this_thread::sleep_for(BIT_1_HIGH_VOLTAGE_TIME);
digitalWrite(PIN, false);
std::this_thread::sleep_for(BIT_1_LOW_VOLTAGE_TIME);
}
}
else {
for (int bit = 0; bit < BITS_FOR_SINGLE_LED; bit++) {
digitalWrite(PIN, true);
std::this_thread::sleep_for(BIT_0_HIGH_VOLTAGE_TIME);
digitalWrite(PIN, false);
std::this_thread::sleep_for(BIT_0_LOW_VOLTAGE_TIME);
}
}
}
auto programFinish = std::chrono::high_resolution_clock::now();
printf("Completed in ");
std::cout << std::chrono::duration_cast<std::chrono::nanoseconds>(programFinish - programStart).count() << " ns but must be no longer as 300 000 nanoseconds on the Raspberry Pi 3b";
}
void digitalWrite(int pin, bool statement) {
//something that change the statement of a single pin of the GPIO. This is a ready for use function in the wiringpi library
//true = HIGH (3.3 VDC)
//false = LOW (0 VDC)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment