Hello,
I have made a simple application based on Blink example in Visual Studio to test functions of high-level application.
Application should work as follows:
1. Turn on 1st LED
2. Wait 1 second and turn off 1st LED then wait 1 second.
3. Turn ON 2nd LED
4. Wait 1 second and turn off 2nd LED then wait 1 second.
3. Turn ON 3rd LED
5. Wait 1 second and turn off 3rd LED then wait 1 second.
6. Start from point 1.
Im using user RGB LED as indicatior. Problem is LEDs does not turn OFF in right order. 1st LED turns ON then 2nd turns ON then 1st turns OFF i belive. There should be 1 second gap of none of LEDs switched ON but there is still 1 or few ON.
My code (Pastebin): https://pastebin.com/n49qdzR3
#include <stdbool.h> #include <errno.h> #include <string.h> #include <time.h> #include <applibs/log.h> #include <applibs/gpio.h> int main(void) { // This minimal Azure Sphere app repeatedly toggles GPIO 9, which is the green channel of RGB // LED 1 on the MT3620 RDB. // Use this app to test that device and SDK installation succeeded that you can build, // deploy, and debug an app with Visual Studio, and that you can deploy an app over the air, // per the instructions here: https://docs.microsoft.com/azure-sphere/quickstarts/qs-overview // // It is NOT recommended to use this as a starting point for developing apps; instead use // the extensible samples here: https://github.com/Azure/azure-sphere-samples Log_Debug( "\nVisit https://github.com/Azure/azure-sphere-samples for extensible samples to use as a " "starting point for full applications.\n"); int fd = GPIO_OpenAsOutput(9, GPIO_OutputMode_PushPull, GPIO_Value_High); if (fd < 0) { Log_Debug( "Error opening GPIO: %s (%d). Check that app_manifest.json includes the GPIO used.\n", strerror(errno), errno); return -1; } int fd2 = GPIO_OpenAsOutput(10, GPIO_OutputMode_PushPull, GPIO_Value_High); if (fd2 < 0) { Log_Debug( "Error opening GPIO: %s (%d). Check that app_manifest.json includes the GPIO used.\n", strerror(errno), errno); return -1; } int fd3 = GPIO_OpenAsOutput(8, GPIO_OutputMode_PushPull, GPIO_Value_High); if (fd3 < 0) { Log_Debug( "Error opening GPIO: %s (%d). Check that app_manifest.json includes the GPIO used.\n", strerror(errno), errno); return -1; } /* static int buttFd = -1; GPIO_Value_Type newButtonState; GPIO_Id GPIO12; buttFd = GPIO_OpenAsInput(27); */ const struct timespec sleepTime = {1, 0}; typedef enum State {ONE=1,TWO,THREE,FOUR,FIVE,SIX} State; State Machine = ONE; while (true) { //int result = GPIO_GetValue(buttFd, &newButtonState); //nanosleep(&sleepTime, NULL); int res; switch (Machine) { case ONE: GPIO_SetValue(fd, GPIO_Value_High); nanosleep(&sleepTime, NULL); Log_Debug("LED 1 switched ON\n"); Machine = TWO; break; case TWO: GPIO_SetValue(fd, GPIO_Value_Low); nanosleep(&sleepTime, NULL); Log_Debug("LED 1 switched OFF\n"); Machine = THREE; break; case THREE: GPIO_SetValue(fd2, GPIO_Value_High); Log_Debug("LED 2 switched ON\n"); Machine = FOUR; break; case FOUR: GPIO_SetValue(fd2, GPIO_Value_Low); nanosleep(&sleepTime, NULL); Log_Debug("LED 2 switched OFF\n"); Machine = FIVE; break; case FIVE: GPIO_SetValue(fd3, GPIO_Value_High); nanosleep(&sleepTime, NULL); Log_Debug("LED 3 switched ON\n"); Machine = SIX; case SIX: GPIO_SetValue(fd3, GPIO_Value_Low); nanosleep(&sleepTime, NULL); Log_Debug("LED 3 switched OFF\n"); Machine = ONE; } } }
I dont know what is the reason of this problem. Is it because GPIO_SetValue is done asynchronously or nanosleep is non blocking and is done in another thread (OS task)?
Best Regards,
Dariusz