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/scripts/json-files/usage-example-references.json b/scripts/json-files/usage-example-references.json index c41688ec6..554152979 100644 --- a/scripts/json-files/usage-example-references.json +++ b/scripts/json-files/usage-example-references.json @@ -2,7 +2,7 @@ "audio": [ { "funcKey": "play_music_named", - "title": "Music Playback", + "title": "Music Playback\r", "url": "/api/audio/#play-music-named", "functions": [ "audio_ready", @@ -14,7 +14,7 @@ }, { "funcKey": "resume_music", - "title": "Pausing and Resuming Game Music", + "title": "Pausing and Resuming Game Music\r", "url": "/api/audio/#resume-music", "functions": [ "audio_ready", @@ -142,7 +142,7 @@ }, { "funcKey": "closest_point_on_circle", - "title": "Closest Point to Mouse on Circle", + "title": "Closest Point to Mouse on Circle\r", "url": "/api/geometry/#closest-point-on-circle", "functions": [ "open_window", @@ -161,6 +161,25 @@ "close_all_windows" ] }, + { + "funcKey": "lines_intersect", + "title": "Simple Line Intersect Check", + "url": "/api/geometry/#lines-intersect", + "functions": [ + "open_window", + "Point2D", + "line_from_point_to_point", + "draw_line_record", + "color_red", + "draw_text_no_font_no_size", + "color_black", + "color_blue", + "color_green", + "refresh_screen", + "delay", + "close_all_windows" + ] + }, { "funcKey": "line_intersects_rect", "title": "Avoid the Rectangle", @@ -256,25 +275,6 @@ "close_all_windows" ] }, - { - "funcKey": "lines_intersect", - "title": "Simple Line Intersect Check", - "url": "/api/geometry/#lines-intersect", - "functions": [ - "open_window", - "Point2D", - "line_from_point_to_point", - "draw_line_record", - "color_red", - "draw_text_no_font_no_size", - "color_black", - "color_blue", - "color_green", - "refresh_screen", - "delay", - "close_all_windows" - ] - }, { "funcKey": "point_at", "title": "Flower Grid", @@ -508,7 +508,7 @@ }, { "funcKey": "random_window_point", - "title": "Random Portals", + "title": "Random Portals\r", "url": "/api/geometry/#random-window-point", "functions": [ "open_window", @@ -524,6 +524,32 @@ "close_window" ] }, + { + "funcKey": "rectangle_around", + "title": "A perpetually moving circle which increases and decreases in size, surrounded by a rectangle shape", + "url": "/api/geometry/#rectangle-around", + "functions": [ + "open_window", + "create_timer", + "start_timer", + "quit_requested", + "point_at", + "cosine", + "sine", + "circle_at", + "timer_ticks", + "reset_timer", + "process_events", + "clear_screen_to_white", + "draw_rectangle_record", + "color_black", + "rectangle_around_circle", + "fill_circle_record", + "color_red", + "refresh_screen", + "close_all_windows" + ] + }, { "funcKey": "same_point", "title": "Point 2D Guessing Game", @@ -561,6 +587,24 @@ "close_all_windows" ] }, + { + "funcKey": "bitmap_center", + "title": "Draw a bitmap with a red dot at its center\r", + "url": "/api/graphics/#bitmap-center", + "functions": [ + "open_window", + "load_bitmap", + "clear_screen", + "color_white", + "draw_bitmap", + "fill_circle_record", + "color_red", + "circle_at", + "refresh_screen", + "delay", + "close_all_windows" + ] + }, { "funcKey": "clear_screen", "title": "Background Color", @@ -575,7 +619,7 @@ }, { "funcKey": "draw_bitmap_named", - "title": "SplashKit Logo", + "title": "SplashKit Logo\r", "url": "/api/graphics/#draw-bitmap-named", "functions": [ "open_window", @@ -1041,7 +1085,7 @@ }, { "funcKey": "fill_rectangle_on_bitmap", - "title": "Cityscape", + "title": "Cityscape\r", "url": "/api/graphics/#fill-rectangle-on-bitmap", "functions": [ "open_window", @@ -1112,7 +1156,7 @@ }, { "funcKey": "fill_triangle_on_bitmap", - "title": "Hooray! A Red Hat", + "title": "Hooray! A Red Hat\r", "url": "/api/graphics/#fill-triangle-on-bitmap", "functions": [ "open_window", @@ -1172,7 +1216,7 @@ }, { "funcKey": "free_font", - "title": "Freeing Fonts", + "title": "Freeing Fonts\r", "url": "/api/graphics/#free-font", "functions": [ "open_window", @@ -1211,7 +1255,7 @@ }, { "funcKey": "load_font", - "title": "Using Fonts", + "title": "Using Fonts\r", "url": "/api/graphics/#load-font", "functions": [ "open_window", @@ -1242,7 +1286,7 @@ }, { "funcKey": "text_width_font_named", - "title": "Underline Text using Text Width", + "title": "Underline Text using Text Width\r", "url": "/api/graphics/#text-width-font-named", "functions": [ "open_window", @@ -1303,7 +1347,7 @@ "physics": [ { "funcKey": "sprite_bitmap_collision", - "title": "Does SplashKit have bugs?", + "title": "Does SplashKit have bugs?\r", "url": "/api/physics/#sprite-bitmap-collision", "functions": [ "open_window", @@ -1323,7 +1367,7 @@ }, { "funcKey": "sprite_collision", - "title": "Colliding Sprites", + "title": "Colliding Sprites\r", "url": "/api/physics/#sprite-collision", "functions": [ "open_window", @@ -1342,7 +1386,7 @@ }, { "funcKey": "sprite_point_collision", - "title": "Determine Sprite Collisions with Points", + "title": "Determine Sprite Collisions with Points\r", "url": "/api/physics/#sprite-point-collision", "functions": [ "open_window", @@ -1365,7 +1409,7 @@ }, { "funcKey": "sprite_rectangle_collision", - "title": "Detect Sprite Collisions with Rectangles", + "title": "Detect Sprite Collisions with Rectangles\r", "url": "/api/physics/#sprite-rectangle-collision", "functions": [ "open_window", @@ -1390,7 +1434,7 @@ "raspberry": [ { "funcKey": "raspi_i2c_write_data", - "title": "LED Text Scrolling", + "title": "LED Text Scrolling\r", "url": "/api/raspberry/#raspi-i2c-write-data", "functions": [ "write_line", @@ -1407,7 +1451,7 @@ "sprites": [ { "funcKey": "create_sprite", - "title": "Creating a Player Sprite", + "title": "Creating a Player Sprite\r", "url": "/api/sprites/#create-sprite", "functions": [ "open_window", @@ -1425,7 +1469,7 @@ }, { "funcKey": "draw_sprite", - "title": "Drawing a Player Sprite", + "title": "Drawing a Player Sprite\r", "url": "/api/sprites/#draw-sprite", "functions": [ "open_window", @@ -1443,7 +1487,7 @@ }, { "funcKey": "free_sprite", - "title": "Freeing a Sprite", + "title": "Freeing a Sprite\r", "url": "/api/sprites/#free-sprite", "functions": [ "open_window", @@ -1465,7 +1509,7 @@ }, { "funcKey": "sprite_set_position", - "title": "Setting Sprite Position", + "title": "Setting Sprite Position\r", "url": "/api/sprites/#sprite-set-position", "functions": [ "open_window", @@ -1485,7 +1529,7 @@ }, { "funcKey": "sprite_set_velocity", - "title": "Setting Velocity of Sprite", + "title": "Setting Velocity of Sprite\r", "url": "/api/sprites/#sprite-set-velocity", "functions": [ "open_window", @@ -1507,7 +1551,7 @@ }, { "funcKey": "sprite_set_x", - "title": "Setting Sprite X-coordinate", + "title": "Setting Sprite X-coordinate\r", "url": "/api/sprites/#sprite-set-x", "functions": [ "open_window", @@ -1524,7 +1568,7 @@ }, { "funcKey": "sprite_set_y", - "title": "Setting Sprite Y-coordinate", + "title": "Setting Sprite Y-coordinate\r", "url": "/api/sprites/#sprite-set-y", "functions": [ "open_window", @@ -1541,7 +1585,7 @@ }, { "funcKey": "sprite_x", - "title": "Sprite x-coordinate", + "title": "Sprite x-coordinate\r", "url": "/api/sprites/#sprite-x", "functions": [ "open_window", @@ -1561,7 +1605,7 @@ }, { "funcKey": "sprite_y", - "title": "Sprite y-coordinate", + "title": "Sprite y-coordinate\r", "url": "/api/sprites/#sprite-y", "functions": [ "open_window", @@ -1673,7 +1717,7 @@ }, { "funcKey": "current_ticks", - "title": "How many ticks?", + "title": "How many ticks?\r", "url": "/api/utilities/#current-ticks", "functions": [ "write_line", @@ -1790,5 +1834,30 @@ "write_line" ] } + ], + "windows": [ + { + "funcKey": "close_window", + "title": "Starts a countdown to close the window at the push of a button.", + "url": "/api/windows/#close-window", + "functions": [ + "open_window", + "create_timer", + "quit_requested", + "process_events", + "clear_window", + "color_white", + "button_at_position", + "rectangle_from", + "start_timer", + "draw_text_font_as_string", + "color_black", + "timer_ticks", + "reset_timer", + "draw_interface", + "refresh_window", + "close_all_windows" + ] + } ] } \ No newline at end of file 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..f88aea4e7 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 @@ -47,6 +47,9 @@ Below is an example program that will detect button presses to increase or decre + + + ```cpp #include "splashkit.h" @@ -110,9 +113,278 @@ int main() } ``` + + + +:::note[Remote GPIO Setup Required] +For remote GPIO operations, ensure the `pigpio` daemon is running on your Raspberry Pi. For detailed setup instructions, see the [Remote Access Guide](https://programmers.guide/book/appendix/0-installation/4-0-remote-access-pi/). +::: + +```cpp +#include "splashkit.h" + +int main() +{ + int brightness = 128; + const int max_brightness = 255; + unsigned long last_read_time = 0; + const unsigned long read_interval = 400; + + // Initialize remote connection to Raspberry Pi + // Parameters: connection name, IP address, port (default: 8888) + connection pi_connection = remote_raspi_init("my_pi", "192.168.1.100", 8888); + + gpio_pin led_pin = PIN_11; + gpio_pin increase_btn_pin = PIN_13; + gpio_pin decrease_btn_pin = PIN_29; + + remote_raspi_set_mode(pi_connection, led_pin, GPIO_OUTPUT); + remote_raspi_set_pwm_dutycycle(pi_connection, led_pin, brightness); + + remote_raspi_set_mode(pi_connection, increase_btn_pin, GPIO_INPUT); + remote_raspi_set_mode(pi_connection, decrease_btn_pin, GPIO_INPUT); + + remote_raspi_set_pull_up_down(pi_connection, increase_btn_pin, PUD_DOWN); + remote_raspi_set_pull_up_down(pi_connection, 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(remote_raspi_read(pi_connection, increase_btn_pin) == GPIO_HIGH) + { + brightness += 25; + if(brightness > max_brightness) + brightness = max_brightness; + remote_raspi_set_pwm_dutycycle(pi_connection, led_pin, brightness); + last_read_time = current_time; + } + if(remote_raspi_read(pi_connection, decrease_btn_pin) == GPIO_HIGH) + { + brightness -= 25; + if(brightness < 0) + brightness = 0; + remote_raspi_set_pwm_dutycycle(pi_connection, led_pin, brightness); + last_read_time = current_time; + } + } + } + + close_all_windows(); + stop_timer(run_timer); + free_all_timers(); + remote_raspi_cleanup(pi_connection); + return 0; +} +``` + + + + + + + +```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(); + } + } +} +``` + + + + + + + +### Remote GPIO Operations + +When using remote GPIO functions, you control GPIO pins on a Raspberry Pi from another computer over a network connection. This is particularly useful for: + +- Developing and testing GPIO code from your main development machine +- Controlling multiple Raspberry Pis simultaneously from one program +- Running resource-intensive programs on a more powerful computer while controlling Pi hardware + +#### Understanding `remote_raspi_init()` + +The [`remote_raspi_init()`](https://splashkit.io/api/raspberry/#remote-raspi-init) function establishes a network connection to your Raspberry Pi. It requires three parameters: + +1. **Name** (string): A unique identifier for this connection + - Used to reference this specific Pi with functions like [`connection_named()`](https://splashkit.io/api/networking/#connection-named) + - Useful when controlling multiple Pis: e.g., "living_room_pi", "garage_pi" + +2. **IP Address** (string): The network address of your Raspberry Pi + - Find it by running `ifconfig` or `hostname -I` on your Pi + - Example: "192.168.1.100" or "10.0.0.50" + +3. **Port** (integer): The port where the pigpio daemon is listening + - Default is `8888` + - Change with the `-p` flag when starting the daemon: `sudo pigpiod -p ` + +**Return Value**: Returns a `connection` object that must be passed as the first parameter to all other remote GPIO functions. + +#### Using Remote Functions + +All remote GPIO functions follow this pattern: + +- They have the `remote_` prefix (e.g., `remote_raspi_set_mode`, `remote_raspi_read`) +- The **first parameter** is always the `connection` object returned by `remote_raspi_init()` +- **Remaining parameters** are identical to the local version of the function + +Example comparison: + +```cpp +// Local version +raspi_set_mode(PIN_11, GPIO_OUTPUT); + +// Remote version +connection pi = remote_raspi_init("my_pi", "192.168.1.100", 8888); +remote_raspi_set_mode(pi, PIN_11, GPIO_OUTPUT); +``` + +:::tip[Controlling Multiple Raspberry Pis] +You can control multiple Pis simultaneously by creating multiple connections: + +```cpp +connection pi1 = remote_raspi_init("bedroom_pi", "192.168.1.100", 8888); +connection pi2 = remote_raspi_init("kitchen_pi", "192.168.1.101", 8888); + +remote_raspi_write(pi1, PIN_11, GPIO_HIGH); // Control first Pi +remote_raspi_write(pi2, PIN_13, GPIO_HIGH); // Control second Pi +``` +::: + ### Understanding the code Okay, now lets break down this code and look at its sections. @@ -128,6 +400,9 @@ Okay, now lets break down this code and look at its sections. + + + ```cpp {5-8} #include "splashkit.h" @@ -148,7 +423,6 @@ 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); @@ -190,6 +464,210 @@ Okay, now lets break down this code and look at its sections. } ``` + + + + ```cpp + #include "splashkit.h" + + int main() + { + int brightness = 128; + const int max_brightness = 255; + unsigned long last_read_time = 0; + const unsigned long read_interval = 400; + + connection pi_connection = remote_raspi_init("my_pi", "192.168.1.100", 8888); + gpio_pin led_pin = PIN_11; + gpio_pin increase_btn_pin = PIN_13; + gpio_pin decrease_btn_pin = PIN_29; + + remote_raspi_set_mode(pi_connection, led_pin, GPIO_OUTPUT); + remote_raspi_set_pwm_dutycycle(pi_connection, led_pin, brightness); + + remote_raspi_set_mode(pi_connection, increase_btn_pin, GPIO_INPUT); + remote_raspi_set_mode(pi_connection, decrease_btn_pin, GPIO_INPUT); + remote_raspi_set_pull_up_down(pi_connection, increase_btn_pin, PUD_DOWN); + remote_raspi_set_pull_up_down(pi_connection, 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(remote_raspi_read(pi_connection, increase_btn_pin) == GPIO_HIGH) + { + brightness += 25; + if(brightness > max_brightness) + brightness = max_brightness; + remote_raspi_set_pwm_dutycycle(pi_connection, led_pin, brightness); + last_read_time = current_time; + } + if(remote_raspi_read(pi_connection, decrease_btn_pin) == GPIO_HIGH) + { + brightness -= 25; + if(brightness < 0) + brightness = 0; + remote_raspi_set_pwm_dutycycle(pi_connection, led_pin, brightness); + last_read_time = current_time; + } + } + } + + close_all_windows(); + stop_timer(run_timer); + free_all_timers(); + remote_raspi_cleanup(pi_connection); + 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(); + } + } + } + ``` + + + + @@ -262,6 +740,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 +949,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 +1196,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 +1407,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 +1616,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(); + } + } + } + ``` + + + +