Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 | 31 |
Tags
- 프로그래밍
- 모의 주식
- Reflex
- java
- frontend
- Python
- 코딩테스트
- 코딩독학
- 프로그램
- Project
- 이웃한 칸
- 사물인터넷
- 코딩
- backend
- java정리
- 풀스택프레임워크
- 백준
- SNS
- 개발자
- 코테
- 코스닥
- C++
- 빅데이터
- 주가예측
- 프로그래머스
- 자바정리
- 개발
- 자바독학
- 자바
- 개인프로젝트
Archives
- Today
- Total
Creative Code
06_THREAD3.c 본문
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <wiringPi.h>
#define LED_1 23
#define LED_2 24
#define SW_1 4
#define SW_2 17
int LED1_flag = 0;
int LED2_flag = 0;
void sw1_interrupt() {
if (LED1_flag == 0) {
LED1_flag = 1;
} else {
LED1_flag = 0;
}
}
void sw2_interrupt() {
if (LED2_flag == 0) {
LED2_flag = 1;
} else {
LED2_flag = 0;
}
}
void* thread1_main(void *arg)
{
while (1) {
if (LED1_flag == 0) {
digitalWrite(LED_1,HIGH);
delay(300);
digitalWrite(LED_1,LOW);
delay(300);
} else {
digitalWrite(LED_1,LOW);
}
}
return NULL;
}
void* thread2_main(void *arg)
{
while (1) {
if (LED2_flag == 0) {
digitalWrite(LED_2,HIGH);
delay(500);
digitalWrite(LED_2,LOW);
delay(500);
} else {
digitalWrite(LED_2,LOW);
}
}
return NULL;
}
int main(int argc, char *argv[])
{
pthread_t t1_id, t2_id;
void *thr_ret;
if (wiringPiSetupGpio() == -1) return -1; // BCM
pinMode(LED_1, OUTPUT);
pinMode(SW_1, INPUT);
pinMode(LED_2,OUTPUT);
pinMode(SW_2, INPUT);
digitalWrite(LED_1, LOW);
digitalWrite(LED_2, LOW);
wiringPiISR(SW_1, INT_EDGE_RISING, &sw1_interrupt);
wiringPiISR(SW_2, INT_EDGE_RISING, &sw2_interrupt);
if (pthread_create(&t1_id, NULL, thread1_main, NULL)) {
puts("pthread_create() error\n");
return -1;
}
if (pthread_create(&t2_id,NULL,thread2_main,NULL)) {
puts("pthread_create() error\n");
return -1;
}
while (1) {
}
//블로킹 함수(스레드가 끝날때까지 다음 명령을 진행하지 않음). t1_id가 끝날때까지 대기
if (pthread_join(t1_id, &thr_ret)!=0) {
puts("pthread_join() error");
return -1;
}
sleep(3);
puts("end of main\n");
return 0;
}
'Raspberry PI(C)' 카테고리의 다른 글
05_THREAD2.c (0) | 2023.10.06 |
---|---|
04_THREAD1.c (0) | 2023.10.06 |
03_THREAD.c (0) | 2023.10.06 |
02_SWITCH_LED_INTERRUPT.c (0) | 2023.10.06 |
01_LED_ON_OFF.c (0) | 2023.10.06 |