I have created a simple program on Java and translated this in C++. But this program on C++ runs relative slowly. I think the trouble is in my method to control the time between operations. I don't have enough C++ experience - I usually develop using Java but Java is for my purpose (change statement of the GPIO-output of a Raspberry Pi 3B 40-65 times slower as C++ (I have tested the both languages by the same operation)).
How can I make this code faster and control exactly the timings between digitalWrite(int, bool) operations?
My Java code (a single call of digitalWrite(int,int) through JNI lasts 3500-6500 nanoseconds on Raspberry Pi 4b):
public static void switchOnLeds(int firstLed, int lastLed){
final int BIT_0_HIGH_VOLTAGE_TIME = 250; //nanoseconds to start transfer the logical 0 for ws2811 in fast mode
final int BIT_0_LOW_VOLTAGE_TIME = 1000; //nanoseconds to end transfer the logical 0 for ws2811 in fast mode
final int BIT_1_HIGH_VOLTAGE_TIME = 600; //nanoseconds to start transfer the logical 1 for ws2811 in fast mode
final int BIT_1_LOW_VOLTAGE_TIME = 650; //nanoseconds to end transfer the logical 1 for ws2811 in fast mode
final int BITS_FOR_SINGLE_LED = 24; //how many bits contains a signal for a single LED
final int PIN = 4; //Output pin
final int LEDS = 10;
long start, end;
for (int i = 0; i < LEDS; i++){
if (i >= firstLed && i <= lastLed){
for (int bit = 0; bit < BITS_FOR_SINGLE_LED; bit++){
start = System.nanoTime();
end = start+BIT_1_HIGH_VOLTAGE_TIME;
digitalWrite(PIN, true);
while (System.nanoTime()<end){
//wait
}
start = System.nanoTime();
end = start+BIT_1_LOW_VOLTAGE_TIME;
digitalWrite(PIN, false);
while (System.nanoTime()<end){
//wait
}
}
}
else {
for (int bit = 0; bit < BITS_FOR_SINGLE_LED; bit++){
start = System.nanoTime();
end = start+BIT_0_HIGH_VOLTAGE_TIME;
digitalWrite(PIN, true);
while (System.nanoTime()<end){
//wait
}
start = System.nanoTime();
end = start+BIT_0_LOW_VOLTAGE_TIME;
digitalWrite(PIN, false);
while (System.nanoTime()<end){
//wait
}
}
}
}
}
My C++ code that must be modernized (a single call of digitalWrite(int,int) lasts 88 nanoseconds on Raspberry Pi 4b):
void switchOnLeds(int firstLed, int lastLed) {
const int BIT_0_HIGH_VOLTAGE_TIME = 250; //nanoseconds to start transfer the logical 0 for ws2811 in fast mode
const int BIT_0_LOW_VOLTAGE_TIME = 1000; //nanoseconds to end transfer the logical 0 for ws2811 in fast mode
const int BIT_1_HIGH_VOLTAGE_TIME = 600; //nanoseconds to start transfer the logical 1 for ws2811 in fast mode
const int BIT_1_LOW_VOLTAGE_TIME = 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;
std::chrono::steady_clock::time_point start = std::chrono::high_resolution_clock::now();
std::chrono::steady_clock::time_point end = 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++) {
start = std::chrono::high_resolution_clock::now();
digitalWrite(PIN, true);
while ((std::chrono::high_resolution_clock::now() - start).count()< BIT_1_HIGH_VOLTAGE_TIME) {
//wait
}
start = std::chrono::high_resolution_clock::now();
digitalWrite(PIN, false);
while ((std::chrono::high_resolution_clock::now() - start).count() < BIT_1_LOW_VOLTAGE_TIME) {
//wait
}
}
}
else {
for (int bit = 0; bit < BITS_FOR_SINGLE_LED; bit++) {
start = std::chrono::high_resolution_clock::now();
digitalWrite(PIN, true);
while ((std::chrono::high_resolution_clock::now() - start).count() < BIT_0_HIGH_VOLTAGE_TIME) {
//wait
}
start = std::chrono::high_resolution_clock::now();
digitalWrite(PIN, false);
while ((std::chrono::high_resolution_clock::now() - start).count() < BIT_0_LOW_VOLTAGE_TIME) {
//wait
}
}
}
}
}