Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- MD Harrington London Kent UK 20-03-2024 Note The Makefile for this you find at this address https://pastebin.com/tAK7Ni8h
- The correct methods of how use threads and pointers to functions
- Demonstrating how to flash leds using the RPI and associated <bcm2835.h>
- Header files
- Note *** Not quite what they have shown you to date which
- 1: locks up
- 2: causes segmentation issues
- 3: Wont read user input etc
- 4: Does not clean up correctly or shuts down the port correctly on the raspberry pi
- This took me a good few days to work this out as to why points 1 to 4
- would not work
- Here ARE some pointers into how which library they used i.e wiringPI AND THEIR EXAMPLES
- https://youtu.be/mLcZJMCcIBE Watch all of thee tutorials carefully because not one of them
- show you this or explain why you cannot interrupt that flash loop using thier approach
- https://youtu.be/Gw-LdBWKxmM
- Below pin mapping using gpio readall explanations also contained in code
- +-----+-----+---------+------+---+---Pi 3B--+---+------+---------+-----+-----+
- | BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |
- +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
- | | | 3.3v | | | 1 || 2 | | | 5v | | |
- | 2 | 8 | SDA.1 | IN | 1 | 3 || 4 | | | 5v | | |
- | 3 | 9 | SCL.1 | IN | 1 | 5 || 6 | | | 0v | | |
- | 4 | 7 | GPIO. 7 | IN | 1 | 7 || 8 | 1 | ALT5 | TxD | 15 | 14 |
- | | | 0v | | | 9 || 10 | 1 | ALT5 | RxD | 16 | 15 |
- | 17 | 0 | GPIO. 0 | OUT | 1 | 11 || 12 | 0 | OUT | GPIO. 1 | 1 | 18 |
- | 27 | 2 | GPIO. 2 | IN | 0 | 13 || 14 | | | 0v | | |
- | 22 | 3 | GPIO. 3 | IN | 0 | 15 || 16 | 0 | IN | GPIO. 4 | 4 | 23 |
- | | | 3.3v | | | 17 || 18 | 0 | IN | GPIO. 5 | 5 | 24 |
- | 10 | 12 | MOSI | IN | 0 | 19 || 20 | | | 0v | | |
- | 9 | 13 | MISO | IN | 0 | 21 || 22 | 0 | IN | GPIO. 6 | 6 | 25 |
- | 11 | 14 | SCLK | IN | 0 | 23 || 24 | 1 | IN | CE0 | 10 | 8 |
- | | | 0v | | | 25 || 26 | 1 | IN | CE1 | 11 | 7 |
- | 0 | 30 | SDA.0 | IN | 1 | 27 || 28 | 1 | IN | SCL.0 | 31 | 1 |
- | 5 | 21 | GPIO.21 | IN | 1 | 29 || 30 | | | 0v | | |
- | 6 | 22 | GPIO.22 | IN | 1 | 31 || 32 | 0 | IN | GPIO.26 | 26 | 12 |
- | 13 | 23 | GPIO.23 | IN | 0 | 33 || 34 | | | 0v | | |
- | 19 | 24 | GPIO.24 | IN | 0 | 35 || 36 | 0 | IN | GPIO.27 | 27 | 16 |
- | 26 | 25 | GPIO.25 | IN | 0 | 37 || 38 | 0 | IN | GPIO.28 | 28 | 20 |
- | | | 0v | | | 39 || 40 | 0 | IN | GPIO.29 | 29 | 21 |
- +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
- | BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |
- +-----+-----+---------+------+---+---Pi 3B--+---+------+---------+-----+-----+
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <bcm2835.h>
- #include <pthread.h>
- #define LED RPI_GPIO_P1_11
- #define LED_2 RPI_GPIO_P1_15
- unsigned int blink = 0;
- void *flashLED() {
- while (1) {
- if (blink) {
- bcm2835_gpio_write(LED, HIGH);
- bcm2835_gpio_write(LED_2, LOW);
- bcm2835_delay(500);// Delay for 500ms (0.5 seconds)
- bcm2835_gpio_write(LED, LOW);
- bcm2835_gpio_write(LED_2, HIGH);
- bcm2835_delay(500);// Delay for 500ms (0.5 seconds)
- } else {
- bcm2835_gpio_write(LED, LOW);
- bcm2835_gpio_write(LED_2, LOW);
- bcm2835_delay(500); // Delay for 100ms (0.1 seconds)
- }
- }
- }
- void cleanup() {
- blink = 0;
- bcm2835_gpio_write(LED_2, LOW);
- bcm2835_gpio_write(LED, LOW);
- bcm2835_close();
- }
- void *menu() {
- int choice;
- do {
- printf("\nChoose an option:\n");
- printf("1: Flash the LED\n");
- printf("2: Stop LED flashing\n");
- printf("3: Exit\n");
- printf("Enter your choice: ");
- scanf("%d", &choice);
- switch (choice) {
- case 1:
- printf("You chose to flash the LED.\n");
- blink = 1; // Stop LED flashing
- break;
- case 2:
- printf("You chose to stop LED flashing.\n");
- blink = 0; // Stop LED flashing
- break;
- case 3:
- printf("You chose to exit.\n");
- cleanup();
- exit(0);
- default:
- printf("Invalid choice. Please try again.\n");
- }
- } while (choice != 0);
- return NULL;
- }
- int main(void) {
- printf("Raspberry Pi blink\n");
- if (!bcm2835_init()) {
- printf("bcm2835_init failed. Are you running as root?\n");
- return 1;
- }
- bcm2835_gpio_fsel(LED, BCM2835_GPIO_FSEL_OUTP);
- bcm2835_gpio_fsel(LED_2, BCM2835_GPIO_FSEL_OUTP);
- pthread_t led_thread, menu_thread;
- pthread_create(&led_thread, NULL, flashLED, NULL);
- pthread_create(&menu_thread, NULL, menu, NULL);
- pthread_join(menu_thread, NULL);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement