diff --git a/scripts/json-files/guides.json b/scripts/json-files/guides.json index b2e9f8292..b91dae214 100644 --- a/scripts/json-files/guides.json +++ b/scripts/json-files/guides.json @@ -352,7 +352,6 @@ "raspi_set_pull_up_down", "create_timer", "start_timer", - "raspi_set_pwm_frequency", "open_window", "any_key_pressed", "process_events", diff --git a/scripts/json-files/guides/raspberry-gpio/raspberry-gpio.json b/scripts/json-files/guides/raspberry-gpio/raspberry-gpio.json index d21d4cd53..a50129f9c 100644 --- a/scripts/json-files/guides/raspberry-gpio/raspberry-gpio.json +++ b/scripts/json-files/guides/raspberry-gpio/raspberry-gpio.json @@ -55,7 +55,6 @@ "raspi_set_pull_up_down", "create_timer", "start_timer", - "raspi_set_pwm_frequency", "open_window", "any_key_pressed", "process_events", diff --git a/src/content/docs/guides/raspberry-gpio/3-pwm-button-control.mdx b/src/content/docs/guides/raspberry-gpio/3-pwm-button-control.mdx index f0d720d47..a721801fb 100644 --- a/src/content/docs/guides/raspberry-gpio/3-pwm-button-control.mdx +++ b/src/content/docs/guides/raspberry-gpio/3-pwm-button-control.mdx @@ -110,6 +110,143 @@ int main() } ``` + + + + + + +```csharp +using static SplashKitSDK.SplashKit; + +int brightness = 128; +const int maxBrightness = 255; +ulong lastReadTime = 0; +const ulong readInterval = 400; + +RaspiInit(); +Pins ledPin = Pins.Pin11; +Pins increaseBtnPin = Pins.Pin13; +Pins decreaseBtnPin = Pins.Pin29; + +RaspiSetMode(ledPin, PinModes.GpioOutput); +RaspiSetPwmDutycycle(ledPin, brightness); + +RaspiSetMode(increaseBtnPin, PinModes.GpioInput); +RaspiSetMode(decreaseBtnPin, PinModes.GpioInput); + +RaspiSetPullUpDown(increaseBtnPin, PullUpDown.PudDown); +RaspiSetPullUpDown(decreaseBtnPin, PullUpDown.PudDown); + +Timer runTimer = CreateTimer("run_timer"); +StartTimer(runTimer); +ulong currentTime = 0; + +OpenWindow("dummy_window", 1, 1); +while (!AnyKeyPressed()) +{ + ProcessEvents(); + currentTime = TimerTicks(runTimer); + if (currentTime - lastReadTime > readInterval) + { + if (RaspiRead(increaseBtnPin) == PinValues.GpioHigh) + { + brightness += 25; + if (brightness > maxBrightness) + brightness = maxBrightness; + RaspiSetPwmDutycycle(ledPin, brightness); + lastReadTime = currentTime; + } + if (RaspiRead(decreaseBtnPin) == PinValues.GpioHigh) + { + brightness -= 25; + if (brightness < 0) + brightness = 0; + RaspiSetPwmDutycycle(ledPin, brightness); + lastReadTime = currentTime; + } + } +} + +CloseAllWindows(); +StopTimer(runTimer); +FreeAllTimers(); +RaspiCleanup(); +``` + + + + +```csharp +using SplashKitSDK; + +namespace PwmButtonControlExample +{ + public class Program + { + public static void Main() + { + int brightness = 128; + const int maxBrightness = 255; + ulong lastReadTime = 0; + const ulong readInterval = 400; + + SplashKit.RaspiInit(); + Pins ledPin = Pins.Pin11; + Pins increaseBtnPin = Pins.Pin13; + Pins decreaseBtnPin = Pins.Pin29; + + SplashKit.RaspiSetMode(ledPin, PinModes.GpioOutput); + SplashKit.RaspiSetPwmDutycycle(ledPin, brightness); + + SplashKit.RaspiSetMode(increaseBtnPin, PinModes.GpioInput); + SplashKit.RaspiSetMode(decreaseBtnPin, PinModes.GpioInput); + + SplashKit.RaspiSetPullUpDown(increaseBtnPin, PullUpDown.PudDown); + SplashKit.RaspiSetPullUpDown(decreaseBtnPin, PullUpDown.PudDown); + + Timer runTimer = SplashKit.CreateTimer("run_timer"); + SplashKit.StartTimer(runTimer); + ulong currentTime = 0; + + SplashKit.OpenWindow("dummy_window", 1, 1); + while (!SplashKit.AnyKeyPressed()) + { + SplashKit.ProcessEvents(); + currentTime = SplashKit.TimerTicks(runTimer); + if (currentTime - lastReadTime > readInterval) + { + if (SplashKit.RaspiRead(increaseBtnPin) == PinValues.GpioHigh) + { + brightness += 25; + if (brightness > maxBrightness) + brightness = maxBrightness; + SplashKit.RaspiSetPwmDutycycle(ledPin, brightness); + lastReadTime = currentTime; + } + if (SplashKit.RaspiRead(decreaseBtnPin) == PinValues.GpioHigh) + { + brightness -= 25; + if (brightness < 0) + brightness = 0; + SplashKit.RaspiSetPwmDutycycle(ledPin, brightness); + lastReadTime = currentTime; + } + } + } + + SplashKit.CloseAllWindows(); + SplashKit.StopTimer(runTimer); + SplashKit.FreeAllTimers(); + SplashKit.RaspiCleanup(); + } + } +} +``` + + + + @@ -148,6 +285,186 @@ Okay, now lets break down this code and look at its sections. raspi_set_mode(increase_btn_pin, GPIO_INPUT); raspi_set_mode(decrease_btn_pin, GPIO_INPUT); + raspi_set_pull_up_down(increase_btn_pin, PUD_DOWN); + raspi_set_pull_up_down(decrease_btn_pin, PUD_DOWN); + + timer run_timer = create_timer("run_timer"); + start_timer(run_timer); + unsigned long current_time = 0; + + open_window("dummy_window", 1, 1); + while(!any_key_pressed()) + { + process_events(); + current_time = timer_ticks(run_timer); + if(current_time - last_read_time > read_interval) + { + if(raspi_read(increase_btn_pin) == GPIO_HIGH) + { + brightness += 25; + if(brightness > max_brightness) + brightness = max_brightness; + raspi_set_pwm_dutycycle(led_pin, brightness); + last_read_time = current_time; + } + if(raspi_read(decrease_btn_pin) == GPIO_HIGH) + { + brightness -= 25; + if(brightness < 0) + brightness = 0; + raspi_set_pwm_dutycycle(led_pin, brightness); + last_read_time = current_time; + } + } + } + + close_all_windows(); + stop_timer(run_timer); + free_all_timers(); + raspi_cleanup(); + return 0; + } + ``` + + + + + + + + ```csharp {1-4} + using static SplashKitSDK.SplashKit; + + int brightness = 128; + const int maxBrightness = 255; + ulong lastReadTime = 0; + const ulong readInterval = 400; + + RaspiInit(); + Pins ledPin = Pins.Pin11; + Pins increaseBtnPin = Pins.Pin13; + Pins decreaseBtnPin = Pins.Pin29; + + RaspiSetMode(ledPin, PinModes.GpioOutput); + RaspiSetPwmDutycycle(ledPin, brightness); + + RaspiSetMode(increaseBtnPin, PinModes.GpioInput); + RaspiSetMode(decreaseBtnPin, PinModes.GpioInput); + + RaspiSetPullUpDown(increaseBtnPin, PullUpDown.PudDown); + RaspiSetPullUpDown(decreaseBtnPin, PullUpDown.PudDown); + + Timer runTimer = CreateTimer("run_timer"); + StartTimer(runTimer); + ulong currentTime = 0; + + OpenWindow("dummy_window", 1, 1); + while (!AnyKeyPressed()) + { + ProcessEvents(); + currentTime = TimerTicks(runTimer); + if (currentTime - lastReadTime > readInterval) + { + if (RaspiRead(increaseBtnPin) == PinValues.GpioHigh) + { + brightness += 25; + if (brightness > maxBrightness) + brightness = maxBrightness; + RaspiSetPwmDutycycle(ledPin, brightness); + lastReadTime = currentTime; + } + if (RaspiRead(decreaseBtnPin) == PinValues.GpioHigh) + { + brightness -= 25; + if (brightness < 0) + brightness = 0; + RaspiSetPwmDutycycle(ledPin, brightness); + lastReadTime = currentTime; + } + } + } + + CloseAllWindows(); + StopTimer(runTimer); + FreeAllTimers(); + RaspiCleanup(); + ``` + + + + + ```csharp {7-10} + using SplashKitSDK; + + namespace PwmButtonControlExample + { + public class Program + { + public static void Main() + { + int brightness = 128; + const int maxBrightness = 255; + ulong lastReadTime = 0; + const ulong readInterval = 400; + + SplashKit.RaspiInit(); + Pins ledPin = Pins.Pin11; + Pins increaseBtnPin = Pins.Pin13; + Pins decreaseBtnPin = Pins.Pin29; + + SplashKit.RaspiSetMode(ledPin, PinModes.GpioOutput); + SplashKit.RaspiSetPwmDutycycle(ledPin, brightness); + + SplashKit.RaspiSetMode(increaseBtnPin, PinModes.GpioInput); + SplashKit.RaspiSetMode(decreaseBtnPin, PinModes.GpioInput); + + SplashKit.RaspiSetPullUpDown(increaseBtnPin, PullUpDown.PudDown); + SplashKit.RaspiSetPullUpDown(decreaseBtnPin, PullUpDown.PudDown); + + Timer runTimer = SplashKit.CreateTimer("run_timer"); + SplashKit.StartTimer(runTimer); + ulong currentTime = 0; + + SplashKit.OpenWindow("dummy_window", 1, 1); + while (!SplashKit.AnyKeyPressed()) + { + SplashKit.ProcessEvents(); + currentTime = SplashKit.TimerTicks(runTimer); + if (currentTime - lastReadTime > readInterval) + { + if (SplashKit.RaspiRead(increaseBtnPin) == PinValues.GpioHigh) + { + brightness += 25; + if (brightness > maxBrightness) + brightness = maxBrightness; + SplashKit.RaspiSetPwmDutycycle(ledPin, brightness); + lastReadTime = currentTime; + } + if (SplashKit.RaspiRead(decreaseBtnPin) == PinValues.GpioHigh) + { + brightness -= 25; + if (brightness < 0) + brightness = 0; + SplashKit.RaspiSetPwmDutycycle(ledPin, brightness); + lastReadTime = currentTime; + } + } + } + + SplashKit.CloseAllWindows(); + SplashKit.StopTimer(runTimer); + SplashKit.FreeAllTimers(); + SplashKit.RaspiCleanup(); + } + } + } + ``` + + + + + + raspi_set_pull_up_down(increase_btn_pin, PUD_DOWN); raspi_set_pull_up_down(decrease_btn_pin, PUD_DOWN); @@ -262,6 +579,143 @@ Okay, now lets break down this code and look at its sections. } ``` + + + + + + + ```csharp {8-11} + using static SplashKitSDK.SplashKit; + + int brightness = 128; + const int maxBrightness = 255; + ulong lastReadTime = 0; + const ulong readInterval = 400; + + RaspiInit(); + Pins ledPin = Pins.Pin11; + Pins increaseBtnPin = Pins.Pin13; + Pins decreaseBtnPin = Pins.Pin29; + + RaspiSetMode(ledPin, PinModes.GpioOutput); + RaspiSetPwmDutycycle(ledPin, brightness); + + RaspiSetMode(increaseBtnPin, PinModes.GpioInput); + RaspiSetMode(decreaseBtnPin, PinModes.GpioInput); + + RaspiSetPullUpDown(increaseBtnPin, PullUpDown.PudDown); + RaspiSetPullUpDown(decreaseBtnPin, PullUpDown.PudDown); + + Timer runTimer = CreateTimer("run_timer"); + StartTimer(runTimer); + ulong currentTime = 0; + + OpenWindow("dummy_window", 1, 1); + while (!AnyKeyPressed()) + { + ProcessEvents(); + currentTime = TimerTicks(runTimer); + if (currentTime - lastReadTime > readInterval) + { + if (RaspiRead(increaseBtnPin) == PinValues.GpioHigh) + { + brightness += 25; + if (brightness > maxBrightness) + brightness = maxBrightness; + RaspiSetPwmDutycycle(ledPin, brightness); + lastReadTime = currentTime; + } + if (RaspiRead(decreaseBtnPin) == PinValues.GpioHigh) + { + brightness -= 25; + if (brightness < 0) + brightness = 0; + RaspiSetPwmDutycycle(ledPin, brightness); + lastReadTime = currentTime; + } + } + } + + CloseAllWindows(); + StopTimer(runTimer); + FreeAllTimers(); + RaspiCleanup(); + ``` + + + + + ```csharp {14-17} + using SplashKitSDK; + + namespace PwmButtonControlExample + { + public class Program + { + public static void Main() + { + int brightness = 128; + const int maxBrightness = 255; + ulong lastReadTime = 0; + const ulong readInterval = 400; + + SplashKit.RaspiInit(); + Pins ledPin = Pins.Pin11; + Pins increaseBtnPin = Pins.Pin13; + Pins decreaseBtnPin = Pins.Pin29; + + SplashKit.RaspiSetMode(ledPin, PinModes.GpioOutput); + SplashKit.RaspiSetPwmDutycycle(ledPin, brightness); + + SplashKit.RaspiSetMode(increaseBtnPin, PinModes.GpioInput); + SplashKit.RaspiSetMode(decreaseBtnPin, PinModes.GpioInput); + + SplashKit.RaspiSetPullUpDown(increaseBtnPin, PullUpDown.PudDown); + SplashKit.RaspiSetPullUpDown(decreaseBtnPin, PullUpDown.PudDown); + + Timer runTimer = SplashKit.CreateTimer("run_timer"); + SplashKit.StartTimer(runTimer); + ulong currentTime = 0; + + SplashKit.OpenWindow("dummy_window", 1, 1); + while (!SplashKit.AnyKeyPressed()) + { + SplashKit.ProcessEvents(); + currentTime = SplashKit.TimerTicks(runTimer); + if (currentTime - lastReadTime > readInterval) + { + if (SplashKit.RaspiRead(increaseBtnPin) == PinValues.GpioHigh) + { + brightness += 25; + if (brightness > maxBrightness) + brightness = maxBrightness; + SplashKit.RaspiSetPwmDutycycle(ledPin, brightness); + lastReadTime = currentTime; + } + if (SplashKit.RaspiRead(decreaseBtnPin) == PinValues.GpioHigh) + { + brightness -= 25; + if (brightness < 0) + brightness = 0; + SplashKit.RaspiSetPwmDutycycle(ledPin, brightness); + lastReadTime = currentTime; + } + } + } + + SplashKit.CloseAllWindows(); + SplashKit.StopTimer(runTimer); + SplashKit.FreeAllTimers(); + SplashKit.RaspiCleanup(); + } + } + } + ``` + + + + @@ -334,6 +788,181 @@ Okay, now lets break down this code and look at its sections. } ``` + + + + + + + ```csharp {13-20} + using static SplashKitSDK.SplashKit; + + int brightness = 128; + const int maxBrightness = 255; + ulong lastReadTime = 0; + const ulong readInterval = 400; + + RaspiInit(); + Pins ledPin = Pins.Pin11; + Pins increaseBtnPin = Pins.Pin13; + Pins decreaseBtnPin = Pins.Pin29; + + RaspiSetMode(ledPin, PinModes.GpioOutput); + RaspiSetPwmDutycycle(ledPin, brightness); + + RaspiSetMode(increaseBtnPin, PinModes.GpioInput); + RaspiSetMode(decreaseBtnPin, PinModes.GpioInput); + + RaspiSetPullUpDown(increaseBtnPin, PullUpDown.PudDown); + RaspiSetPullUpDown(decreaseBtnPin, PullUpDown.PudDown); + + Timer runTimer = CreateTimer("run_timer"); + StartTimer(runTimer); + ulong currentTime = 0; + + OpenWindow("dummy_window", 1, 1); + while (!AnyKeyPressed()) + { + ProcessEvents(); + currentTime = TimerTicks(runTimer); + if (currentTime - lastReadTime > readInterval) + { + if (RaspiRead(increaseBtnPin) == PinValues.GpioHigh) + { + brightness += 25; + if (brightness > maxBrightness) + brightness = maxBrightness; + RaspiSetPwmDutycycle(ledPin, brightness); + lastReadTime = currentTime; + } + if (RaspiRead(decreaseBtnPin) == PinValues.GpioHigh) + { + brightness -= 25; + if (brightness < 0) + brightness = 0; + RaspiSetPwmDutycycle(ledPin, brightness); + lastReadTime = currentTime; + } + } + } + + CloseAllWindows(); + StopTimer(runTimer); + FreeAllTimers(); + RaspiCleanup(); + ``` + + + + + ```csharp {19-26} + using SplashKitSDK; + + namespace PwmButtonControlExample + { + public class Program + { + public static void Main() + { + int brightness = 128; + const int maxBrightness = 255; + ulong lastReadTime = 0; + const ulong readInterval = 400; + + SplashKit.RaspiInit(); + Pins ledPin = Pins.Pin11; + Pins increaseBtnPin = Pins.Pin13; + Pins decreaseBtnPin = Pins.Pin29; + + SplashKit.RaspiSetMode(ledPin, PinModes.GpioOutput); + SplashKit.RaspiSetPwmDutycycle(ledPin, brightness); + + SplashKit.RaspiSetMode(increaseBtnPin, PinModes.GpioInput); + SplashKit.RaspiSetMode(decreaseBtnPin, PinModes.GpioInput); + + SplashKit.RaspiSetPullUpDown(increaseBtnPin, PullUpDown.PudDown); + SplashKit.RaspiSetPullUpDown(decreaseBtnPin, PullUpDown.PudDown); + + Timer runTimer = SplashKit.CreateTimer("run_timer"); + SplashKit.StartTimer(runTimer); + ulong currentTime = 0; + + SplashKit.OpenWindow("dummy_window", 1, 1); + while (!SplashKit.AnyKeyPressed()) + { + SplashKit.ProcessEvents(); + currentTime = SplashKit.TimerTicks(runTimer); + if (currentTime - lastReadTime > readInterval) + { + if (SplashKit.RaspiRead(increaseBtnPin) == PinValues.GpioHigh) + { + brightness += 25; + if (brightness > maxBrightness) + brightness = maxBrightness; + SplashKit.RaspiSetPwmDutycycle(ledPin, brightness); + lastReadTime = currentTime; + } + if (SplashKit.RaspiRead(decreaseBtnPin) == PinValues.GpioHigh) + { + brightness -= 25; + if (brightness < 0) + brightness = 0; + SplashKit.RaspiSetPwmDutycycle(ledPin, brightness); + lastReadTime = currentTime; + } + } + } + + SplashKit.CloseAllWindows(); + SplashKit.StopTimer(runTimer); + SplashKit.FreeAllTimers(); + SplashKit.RaspiCleanup(); + } + } + } + ``` + + + + + + + unsigned long current_time = 0; + + open_window("dummy_window", 1, 1); + while(!any_key_pressed()) + { + process_events(); + current_time = timer_ticks(run_timer); + if(current_time - last_read_time > read_interval) + { + if(raspi_read(increase_btn_pin) == GPIO_HIGH) + { + brightness += 25; + if(brightness > max_brightness) + brightness = max_brightness; + raspi_set_pwm_dutycycle(led_pin, brightness); + last_read_time = current_time; + } + if(raspi_read(decrease_btn_pin) == GPIO_HIGH) + { + brightness -= 25; + if(brightness < 0) + brightness = 0; + raspi_set_pwm_dutycycle(led_pin, brightness); + last_read_time = current_time; + } + } + } + + close_all_windows(); + stop_timer(run_timer); + free_all_timers(); + raspi_cleanup(); + return 0; + } + ``` + @@ -406,6 +1035,143 @@ Okay, now lets break down this code and look at its sections. } ``` + + + + + + + ```csharp {22-24,26-29,50} + using static SplashKitSDK.SplashKit; + + int brightness = 128; + const int maxBrightness = 255; + ulong lastReadTime = 0; + const ulong readInterval = 400; + + RaspiInit(); + Pins ledPin = Pins.Pin11; + Pins increaseBtnPin = Pins.Pin13; + Pins decreaseBtnPin = Pins.Pin29; + + RaspiSetMode(ledPin, PinModes.GpioOutput); + RaspiSetPwmDutycycle(ledPin, brightness); + + RaspiSetMode(increaseBtnPin, PinModes.GpioInput); + RaspiSetMode(decreaseBtnPin, PinModes.GpioInput); + + RaspiSetPullUpDown(increaseBtnPin, PullUpDown.PudDown); + RaspiSetPullUpDown(decreaseBtnPin, PullUpDown.PudDown); + + Timer runTimer = CreateTimer("run_timer"); + StartTimer(runTimer); + ulong currentTime = 0; + + OpenWindow("dummy_window", 1, 1); + while (!AnyKeyPressed()) + { + ProcessEvents(); + currentTime = TimerTicks(runTimer); + if (currentTime - lastReadTime > readInterval) + { + if (RaspiRead(increaseBtnPin) == PinValues.GpioHigh) + { + brightness += 25; + if (brightness > maxBrightness) + brightness = maxBrightness; + RaspiSetPwmDutycycle(ledPin, brightness); + lastReadTime = currentTime; + } + if (RaspiRead(decreaseBtnPin) == PinValues.GpioHigh) + { + brightness -= 25; + if (brightness < 0) + brightness = 0; + RaspiSetPwmDutycycle(ledPin, brightness); + lastReadTime = currentTime; + } + } + } + + CloseAllWindows(); + StopTimer(runTimer); + FreeAllTimers(); + RaspiCleanup(); + ``` + + + + + ```csharp {28-30,32-35,56} + using SplashKitSDK; + + namespace PwmButtonControlExample + { + public class Program + { + public static void Main() + { + int brightness = 128; + const int maxBrightness = 255; + ulong lastReadTime = 0; + const ulong readInterval = 400; + + SplashKit.RaspiInit(); + Pins ledPin = Pins.Pin11; + Pins increaseBtnPin = Pins.Pin13; + Pins decreaseBtnPin = Pins.Pin29; + + SplashKit.RaspiSetMode(ledPin, PinModes.GpioOutput); + SplashKit.RaspiSetPwmDutycycle(ledPin, brightness); + + SplashKit.RaspiSetMode(increaseBtnPin, PinModes.GpioInput); + SplashKit.RaspiSetMode(decreaseBtnPin, PinModes.GpioInput); + + SplashKit.RaspiSetPullUpDown(increaseBtnPin, PullUpDown.PudDown); + SplashKit.RaspiSetPullUpDown(decreaseBtnPin, PullUpDown.PudDown); + + Timer runTimer = SplashKit.CreateTimer("run_timer"); + SplashKit.StartTimer(runTimer); + ulong currentTime = 0; + + SplashKit.OpenWindow("dummy_window", 1, 1); + while (!SplashKit.AnyKeyPressed()) + { + SplashKit.ProcessEvents(); + currentTime = SplashKit.TimerTicks(runTimer); + if (currentTime - lastReadTime > readInterval) + { + if (SplashKit.RaspiRead(increaseBtnPin) == PinValues.GpioHigh) + { + brightness += 25; + if (brightness > maxBrightness) + brightness = maxBrightness; + SplashKit.RaspiSetPwmDutycycle(ledPin, brightness); + lastReadTime = currentTime; + } + if (SplashKit.RaspiRead(decreaseBtnPin) == PinValues.GpioHigh) + { + brightness -= 25; + if (brightness < 0) + brightness = 0; + SplashKit.RaspiSetPwmDutycycle(ledPin, brightness); + lastReadTime = currentTime; + } + } + } + + SplashKit.CloseAllWindows(); + SplashKit.StopTimer(runTimer); + SplashKit.FreeAllTimers(); + SplashKit.RaspiCleanup(); + } + } + } + ``` + + + + @@ -480,6 +1246,143 @@ Okay, now lets break down this code and look at its sections. } ``` + + + + + + + ```csharp {30-49} + using static SplashKitSDK.SplashKit; + + int brightness = 128; + const int maxBrightness = 255; + ulong lastReadTime = 0; + const ulong readInterval = 400; + + RaspiInit(); + Pins ledPin = Pins.Pin11; + Pins increaseBtnPin = Pins.Pin13; + Pins decreaseBtnPin = Pins.Pin29; + + RaspiSetMode(ledPin, PinModes.GpioOutput); + RaspiSetPwmDutycycle(ledPin, brightness); + + RaspiSetMode(increaseBtnPin, PinModes.GpioInput); + RaspiSetMode(decreaseBtnPin, PinModes.GpioInput); + + RaspiSetPullUpDown(increaseBtnPin, PullUpDown.PudDown); + RaspiSetPullUpDown(decreaseBtnPin, PullUpDown.PudDown); + + Timer runTimer = CreateTimer("run_timer"); + StartTimer(runTimer); + ulong currentTime = 0; + + OpenWindow("dummy_window", 1, 1); + while (!AnyKeyPressed()) + { + ProcessEvents(); + currentTime = TimerTicks(runTimer); + if (currentTime - lastReadTime > readInterval) + { + if (RaspiRead(increaseBtnPin) == PinValues.GpioHigh) + { + brightness += 25; + if (brightness > maxBrightness) + brightness = maxBrightness; + RaspiSetPwmDutycycle(ledPin, brightness); + lastReadTime = currentTime; + } + if (RaspiRead(decreaseBtnPin) == PinValues.GpioHigh) + { + brightness -= 25; + if (brightness < 0) + brightness = 0; + RaspiSetPwmDutycycle(ledPin, brightness); + lastReadTime = currentTime; + } + } + } + + CloseAllWindows(); + StopTimer(runTimer); + FreeAllTimers(); + RaspiCleanup(); + ``` + + + + + ```csharp {36-55} + using SplashKitSDK; + + namespace PwmButtonControlExample + { + public class Program + { + public static void Main() + { + int brightness = 128; + const int maxBrightness = 255; + ulong lastReadTime = 0; + const ulong readInterval = 400; + + SplashKit.RaspiInit(); + Pins ledPin = Pins.Pin11; + Pins increaseBtnPin = Pins.Pin13; + Pins decreaseBtnPin = Pins.Pin29; + + SplashKit.RaspiSetMode(ledPin, PinModes.GpioOutput); + SplashKit.RaspiSetPwmDutycycle(ledPin, brightness); + + SplashKit.RaspiSetMode(increaseBtnPin, PinModes.GpioInput); + SplashKit.RaspiSetMode(decreaseBtnPin, PinModes.GpioInput); + + SplashKit.RaspiSetPullUpDown(increaseBtnPin, PullUpDown.PudDown); + SplashKit.RaspiSetPullUpDown(decreaseBtnPin, PullUpDown.PudDown); + + Timer runTimer = SplashKit.CreateTimer("run_timer"); + SplashKit.StartTimer(runTimer); + ulong currentTime = 0; + + SplashKit.OpenWindow("dummy_window", 1, 1); + while (!SplashKit.AnyKeyPressed()) + { + SplashKit.ProcessEvents(); + currentTime = SplashKit.TimerTicks(runTimer); + if (currentTime - lastReadTime > readInterval) + { + if (SplashKit.RaspiRead(increaseBtnPin) == PinValues.GpioHigh) + { + brightness += 25; + if (brightness > maxBrightness) + brightness = maxBrightness; + SplashKit.RaspiSetPwmDutycycle(ledPin, brightness); + lastReadTime = currentTime; + } + if (SplashKit.RaspiRead(decreaseBtnPin) == PinValues.GpioHigh) + { + brightness -= 25; + if (brightness < 0) + brightness = 0; + SplashKit.RaspiSetPwmDutycycle(ledPin, brightness); + lastReadTime = currentTime; + } + } + } + + SplashKit.CloseAllWindows(); + SplashKit.StopTimer(runTimer); + SplashKit.FreeAllTimers(); + SplashKit.RaspiCleanup(); + } + } + } + ``` + + + + @@ -552,6 +1455,143 @@ Okay, now lets break down this code and look at its sections. } ``` + + + + + + + ```csharp {50-53} + using static SplashKitSDK.SplashKit; + + int brightness = 128; + const int maxBrightness = 255; + ulong lastReadTime = 0; + const ulong readInterval = 400; + + RaspiInit(); + Pins ledPin = Pins.Pin11; + Pins increaseBtnPin = Pins.Pin13; + Pins decreaseBtnPin = Pins.Pin29; + + RaspiSetMode(ledPin, PinModes.GpioOutput); + RaspiSetPwmDutycycle(ledPin, brightness); + + RaspiSetMode(increaseBtnPin, PinModes.GpioInput); + RaspiSetMode(decreaseBtnPin, PinModes.GpioInput); + + RaspiSetPullUpDown(increaseBtnPin, PullUpDown.PudDown); + RaspiSetPullUpDown(decreaseBtnPin, PullUpDown.PudDown); + + Timer runTimer = CreateTimer("run_timer"); + StartTimer(runTimer); + ulong currentTime = 0; + + OpenWindow("dummy_window", 1, 1); + while (!AnyKeyPressed()) + { + ProcessEvents(); + currentTime = TimerTicks(runTimer); + if (currentTime - lastReadTime > readInterval) + { + if (RaspiRead(increaseBtnPin) == PinValues.GpioHigh) + { + brightness += 25; + if (brightness > maxBrightness) + brightness = maxBrightness; + RaspiSetPwmDutycycle(ledPin, brightness); + lastReadTime = currentTime; + } + if (RaspiRead(decreaseBtnPin) == PinValues.GpioHigh) + { + brightness -= 25; + if (brightness < 0) + brightness = 0; + RaspiSetPwmDutycycle(ledPin, brightness); + lastReadTime = currentTime; + } + } + } + + CloseAllWindows(); + StopTimer(runTimer); + FreeAllTimers(); + RaspiCleanup(); + ``` + + + + + ```csharp {56-59} + using SplashKitSDK; + + namespace PwmButtonControlExample + { + public class Program + { + public static void Main() + { + int brightness = 128; + const int maxBrightness = 255; + ulong lastReadTime = 0; + const ulong readInterval = 400; + + SplashKit.RaspiInit(); + Pins ledPin = Pins.Pin11; + Pins increaseBtnPin = Pins.Pin13; + Pins decreaseBtnPin = Pins.Pin29; + + SplashKit.RaspiSetMode(ledPin, PinModes.GpioOutput); + SplashKit.RaspiSetPwmDutycycle(ledPin, brightness); + + SplashKit.RaspiSetMode(increaseBtnPin, PinModes.GpioInput); + SplashKit.RaspiSetMode(decreaseBtnPin, PinModes.GpioInput); + + SplashKit.RaspiSetPullUpDown(increaseBtnPin, PullUpDown.PudDown); + SplashKit.RaspiSetPullUpDown(decreaseBtnPin, PullUpDown.PudDown); + + Timer runTimer = SplashKit.CreateTimer("run_timer"); + SplashKit.StartTimer(runTimer); + ulong currentTime = 0; + + SplashKit.OpenWindow("dummy_window", 1, 1); + while (!SplashKit.AnyKeyPressed()) + { + SplashKit.ProcessEvents(); + currentTime = SplashKit.TimerTicks(runTimer); + if (currentTime - lastReadTime > readInterval) + { + if (SplashKit.RaspiRead(increaseBtnPin) == PinValues.GpioHigh) + { + brightness += 25; + if (brightness > maxBrightness) + brightness = maxBrightness; + SplashKit.RaspiSetPwmDutycycle(ledPin, brightness); + lastReadTime = currentTime; + } + if (SplashKit.RaspiRead(decreaseBtnPin) == PinValues.GpioHigh) + { + brightness -= 25; + if (brightness < 0) + brightness = 0; + SplashKit.RaspiSetPwmDutycycle(ledPin, brightness); + lastReadTime = currentTime; + } + } + } + + SplashKit.CloseAllWindows(); + SplashKit.StopTimer(runTimer); + SplashKit.FreeAllTimers(); + SplashKit.RaspiCleanup(); + } + } + } + ``` + + + +