Skip to content

Commit 7b6600e

Browse files
committed
LED 데이터 실시간으로 받는 기능 추가
1 parent b9744a5 commit 7b6600e

File tree

7 files changed

+125
-59
lines changed

7 files changed

+125
-59
lines changed

SCK_Fnkey/SCK_Fnkey/i2c_slave.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <stdbool.h>
77
#include "i2c_status_code.h"
88

9-
#define I2C_GENERAL_BYTES_MAX 8 // I2C max reading bytes (1 ~ 255)
9+
#define I2C_GENERAL_BYTES_MAX 32 // I2C max reading bytes (1 ~ 255)
1010
#define I2C_READING_BYTES_MAX 32 // I2C max reading bytes (1 ~ 255)
1111
#define I2C_WRITING_BYTES_MAX 32 // I2C max writing bytes (1 ~ 255)
1212

@@ -22,20 +22,24 @@ ISR(TWI_vect) {
2222

2323
switch(I2C_SC) {
2424
case I2C_SC_SR_SWA:
25+
I2C_data_count = 0;
2526
break;
2627
case I2C_SC_SR_GNC:
28+
I2C_data_count = 0;
2729
break;
2830
case I2C_SC_SR_DBA:
29-
I2C_reading_data[0] = TWDR;
31+
I2C_reading_data[I2C_data_count] = TWDR;
32+
I2C_data_count++;
3033
break;
3134
case I2C_SC_SR_DBN:
32-
I2C_reading_data[0] = TWDR;
35+
I2C_reading_data[I2C_data_count] = TWDR;
3336
break;
3437
case I2C_SC_SR_GNA:
35-
I2C_general_data[0] = TWDR;
38+
I2C_general_data[I2C_data_count] = TWDR;
39+
I2C_data_count++;
3640
break;
3741
case I2C_SC_SR_GNN:
38-
I2C_general_data[0] = TWDR;
42+
I2C_general_data[I2C_data_count] = TWDR;
3943
break;
4044
case I2C_SC_SR_STO:
4145
TWCR = 0xC5; // clear TWINT, ACK

SCK_Fnkey/SCK_Fnkey/main.cpp

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,30 @@ int main(void) {
6161
power_state = I2C_general_data[0] & 0x80;
6262

6363
if(power_state) {
64-
for(unsigned char i=0; i<LED_COUNT; i++) {
65-
pixel[i] = (rgb_color){15,15,15};
66-
}
64+
if(I2C_general_data[1]) {// I2C_general_data[1] != 0
65+
rgb_color col_temp[6];
66+
// 0xRG, 0xBW
67+
68+
for(unsigned char i=0; i<6; i++) {
69+
col_temp[i].red = I2C_general_data[3*i + 1];
70+
col_temp[i].green = I2C_general_data[3*i + 2];
71+
col_temp[i].blue = I2C_general_data[3*i + 3];
72+
}
73+
74+
for(unsigned char i=0; i<3; i++) {
75+
pixel[i] = col_temp[0];
76+
pixel[i+3] = col_temp[1];
77+
pixel[i+6] = col_temp[2];
78+
pixel[i+10] = col_temp[5];
79+
}
80+
pixel[9] = col_temp[4];
81+
6782
} else {
83+
for(unsigned char i=0; i<LED_COUNT; i++) {
84+
pixel[i] = (rgb_color){15,15,15};
85+
}
86+
}
87+
} else {
6888
for(unsigned char i=0; i<LED_COUNT; i++) {
6989
pixel[i] = (rgb_color){0,0,0};
7090
}

SCK_Macro/SCK_Macro/i2c_slave.h

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <stdbool.h>
77
#include "i2c_status_code.h"
88

9-
#define I2C_GENERAL_BYTES_MAX 8 // I2C max reading bytes (1 ~ 255)
9+
#define I2C_GENERAL_BYTES_MAX 32 // I2C max reading bytes (1 ~ 255)
1010
#define I2C_READING_BYTES_MAX 32 // I2C max reading bytes (1 ~ 255)
1111
#define I2C_WRITING_BYTES_MAX 32 // I2C max writing bytes (1 ~ 255)
1212

@@ -16,49 +16,53 @@ void I2C_address_set(unsigned char address, bool allow_general_call);; // set I2
1616
volatile unsigned char I2C_general_data[I2C_READING_BYTES_MAX] = {0,}; // I2C general call data
1717
volatile unsigned char I2C_reading_data[I2C_READING_BYTES_MAX] = {0,}; // I2C reading data
1818
volatile unsigned char I2C_writing_data[I2C_WRITING_BYTES_MAX] = {0,}; // I2C writing data
19-
19+
volatile unsigned char I2C_data_count = 0; // I2C data count
2020

2121
ISR(TWI_vect) {
2222

2323
switch(I2C_SC) {
2424
case I2C_SC_SR_SWA:
25+
I2C_data_count = 0;
2526
break;
2627
case I2C_SC_SR_GNC:
28+
I2C_data_count = 0;
2729
break;
2830
case I2C_SC_SR_DBA:
29-
I2C_reading_data[0] = TWDR;
31+
I2C_reading_data[I2C_data_count] = TWDR;
32+
I2C_data_count++;
3033
break;
3134
case I2C_SC_SR_DBN:
32-
I2C_reading_data[0] = TWDR;
35+
I2C_reading_data[I2C_data_count] = TWDR;
3336
break;
3437
case I2C_SC_SR_GNA:
35-
I2C_general_data[0] = TWDR;
38+
I2C_general_data[I2C_data_count] = TWDR;
39+
I2C_data_count++;
3640
break;
3741
case I2C_SC_SR_GNN:
38-
I2C_general_data[0] = TWDR;
42+
I2C_general_data[I2C_data_count] = TWDR;
3943
break;
4044
case I2C_SC_SR_STO:
41-
TWCR = 0xC5; // clear TWINT, ACK
45+
TWCR = 0xC5; // clear TWINT, ACK
4246
break;
4347
case I2C_SC_ST_SRA:
44-
I2C_writing_data[0] += rotery_check() << 6;
45-
TWDR = I2C_writing_data[0];
46-
//TWCR = 0x85; // clear TWINT, NOT ACK
47-
TWCR = 0xC5; // clear TWINT, ACK
48+
TWDR = I2C_writing_data[0];
49+
I2C_data_count = 1;
50+
TWCR = 0xC5; // clear TWINT, ACK
4851
break;
4952
case I2C_SC_ST_DBA:
50-
I2C_reading_data[0] = 0x00;
53+
TWDR = I2C_writing_data[I2C_data_count];
54+
I2C_writing_data[I2C_data_count] = 0x00;
55+
I2C_data_count++;
5156
break;
5257
case I2C_SC_ST_DBN:
53-
I2C_reading_data[0] = 0x00;
54-
TWCR = 0xC5; // clear TWINT, ACK
58+
TWCR = 0xC5; // clear TWINT, ACK
5559
break;
5660
case I2C_SC_ST_ARA:
5761
break;
5862
case I2C_SC_SR_AWA:
5963
break;
6064
case I2C_SC_SR_AGA:
61-
// Arbitration unused
65+
// Arbitration unused
6266
break;
6367
case I2C_SC_ER_ERR:
6468
break;

SCK_Macro/SCK_Macro/led_sk6812.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
// These lines specify what pin the LED strip is on.
1818
// You will either need to attach the LED strip's data line to PC0 or change these
1919
// lines to specify a different pin.
20-
#define LED_STRIP_PORT PORTC
21-
#define LED_STRIP_DDR DDRC
22-
#define LED_STRIP_PIN 1
20+
#define LED_STRIP_PORT PORTB
21+
#define LED_STRIP_DDR DDRB
22+
#define LED_STRIP_PIN 0
2323

2424
#include <avr/io.h>
2525
#include <avr/interrupt.h>

SCK_Macro/SCK_Macro/main.cpp

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
// DO NOT PUSH ANY KEY WHILE DOWNLOADING!!!
88

9-
#define F_CPU 16000000UL
9+
#define F_CPU 16000000
1010

1111
#include <avr/io.h>
1212
#include <avr/interrupt.h>
@@ -43,18 +43,11 @@ int main(void) {
4343
I2C_init_slave(my_address);
4444

4545
// boot led start
46-
pixel[0] = (rgb_color){15,15,15};
47-
led_strip_write(pixel, LED_COUNT);
48-
_delay_ms(1000);
49-
50-
for(unsigned char i=1; i<LED_COUNT; i++) {
46+
for(unsigned char i=0; i<LED_COUNT; i++) {
5147
pixel[i] = (rgb_color){15,15,15};
52-
pixel[i-1] = (rgb_color){0,0,0};
5348
led_strip_write(pixel, LED_COUNT);
54-
_delay_ms(1000);
49+
_delay_ms(100);
5550
}
56-
pixel[LED_COUNT-1] = (rgb_color){0,0,0};
57-
led_strip_write(pixel, LED_COUNT);
5851
// boot led end
5952

6053
TWCR |= 0x80; //clear TWINT
@@ -66,15 +59,32 @@ int main(void) {
6659
I2C_writing_data[0] = key_state;
6760
power_state = I2C_general_data[0] & 0x80;
6861

69-
if(power_state) { // LED on
70-
for(unsigned char i=0; i<LED_COUNT; i++) {
71-
pixel[i] = (rgb_color){15,15,15};
72-
}
73-
} else { // LED off
62+
if(power_state) {
63+
if(I2C_general_data[1]) {// I2C_general_data[1] != 0
64+
rgb_color col_temp[6];
65+
// 0xRG, 0xBW
66+
67+
for(unsigned char i=0; i<6; i++) {
68+
col_temp[i].red = I2C_general_data[3*i + 1];
69+
col_temp[i].green = I2C_general_data[3*i + 2];
70+
col_temp[i].blue = I2C_general_data[3*i + 3];
71+
}
72+
73+
for(unsigned char i=0; i<5; i++) {
74+
pixel[i] = col_temp[i+1];
75+
}
76+
77+
} else {
78+
for(unsigned char i=0; i<LED_COUNT; i++) {
79+
pixel[i] = (rgb_color){15,15,15};
80+
}
81+
}
82+
} else {
7483
for(unsigned char i=0; i<LED_COUNT; i++) {
7584
pixel[i] = (rgb_color){0,0,0};
7685
}
7786
}
87+
7888
cli();
7989
led_strip_write(pixel, LED_COUNT);
8090
sei();

SCK_keyPad/SCK_keyPad/i2c_slave.h

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <stdbool.h>
77
#include "i2c_status_code.h"
88

9-
#define I2C_GENERAL_BYTES_MAX 8 // I2C max reading bytes (1 ~ 255)
9+
#define I2C_GENERAL_BYTES_MAX 32 // I2C max reading bytes (1 ~ 255)
1010
#define I2C_READING_BYTES_MAX 32 // I2C max reading bytes (1 ~ 255)
1111
#define I2C_WRITING_BYTES_MAX 32 // I2C max writing bytes (1 ~ 255)
1212

@@ -22,43 +22,47 @@ ISR(TWI_vect) {
2222

2323
switch(I2C_SC) {
2424
case I2C_SC_SR_SWA:
25+
I2C_data_count = 0;
2526
break;
2627
case I2C_SC_SR_GNC:
28+
I2C_data_count = 0;
2729
break;
2830
case I2C_SC_SR_DBA:
29-
I2C_reading_data[0] = TWDR;
31+
I2C_reading_data[I2C_data_count] = TWDR;
32+
I2C_data_count++;
3033
break;
3134
case I2C_SC_SR_DBN:
32-
I2C_reading_data[0] = TWDR;
35+
I2C_reading_data[I2C_data_count] = TWDR;
3336
break;
3437
case I2C_SC_SR_GNA:
35-
I2C_general_data[0] = TWDR;
38+
I2C_general_data[I2C_data_count] = TWDR;
39+
I2C_data_count++;
3640
break;
3741
case I2C_SC_SR_GNN:
38-
I2C_general_data[0] = TWDR;
42+
I2C_general_data[I2C_data_count] = TWDR;
3943
break;
4044
case I2C_SC_SR_STO:
41-
TWCR = 0xC5; // clear TWINT, ACK
45+
TWCR = 0xC5; // clear TWINT, ACK
4246
break;
4347
case I2C_SC_ST_SRA:
44-
TWDR = I2C_writing_data[0];
45-
I2C_data_count = 1;
46-
TWCR = 0xC5; // clear TWINT, ACK
48+
TWDR = I2C_writing_data[0];
49+
I2C_data_count = 1;
50+
TWCR = 0xC5; // clear TWINT, ACK
4751
break;
4852
case I2C_SC_ST_DBA:
49-
TWDR = I2C_writing_data[I2C_data_count];
50-
I2C_writing_data[I2C_data_count] = 0x00;
51-
I2C_data_count++;
53+
TWDR = I2C_writing_data[I2C_data_count];
54+
I2C_writing_data[I2C_data_count] = 0x00;
55+
I2C_data_count++;
5256
break;
5357
case I2C_SC_ST_DBN:
54-
TWCR = 0xC5; // clear TWINT, ACK
58+
TWCR = 0xC5; // clear TWINT, ACK
5559
break;
5660
case I2C_SC_ST_ARA:
5761
break;
5862
case I2C_SC_SR_AWA:
5963
break;
6064
case I2C_SC_SR_AGA:
61-
// Arbitration unused
65+
// Arbitration unused
6266
break;
6367
case I2C_SC_ER_ERR:
6468
break;

SCK_keyPad/SCK_keyPad/main.cpp

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ void get_key_state(void);
3131
void get_key_state_h(unsigned char mask);
3232

3333
unsigned char my_address = 0x00; // I2C address
34-
volatile bool power_state = true; // led on/off
34+
volatile bool power_state = false; // led on/off
3535
volatile unsigned char key_state[4] = {0x00, 0x00, 0x00, 0x00}; // key line H1, H2, H3, H4 {---, ---, ---, key_v1, key_v2, key_v3, key_v4, key_v5}
3636
unsigned char key_temp[4] = {0,};
3737

@@ -80,10 +80,34 @@ int main(void) {
8080
}
8181

8282
if(power_state) {
83-
for(unsigned char i=0; i<LED_COUNT; i++) {
84-
pixel[i] = (rgb_color){15,15,15};
83+
if(I2C_general_data[1]) {// I2C_general_data[1] != 0
84+
rgb_color col_temp[6];
85+
// 0xRG, 0xBW
86+
87+
for(unsigned char i=0; i<6; i++) {
88+
col_temp[i].red = I2C_general_data[3*i + 1];
89+
col_temp[i].green = I2C_general_data[3*i + 2];
90+
col_temp[i].blue = I2C_general_data[3*i + 3];
91+
}
92+
93+
for(unsigned char i=0; i<4; i++) {
94+
pixel[i] = col_temp[1];
95+
pixel[i+4] = col_temp[2];
96+
pixel[i+11] = col_temp[4];
97+
}
98+
pixel[8] = col_temp[3];
99+
pixel[9] = col_temp[3];
100+
pixel[10] = col_temp[3];
101+
102+
pixel[15] = col_temp[5];
103+
pixel[16] = col_temp[5];
104+
105+
} else {
106+
for(unsigned char i=0; i<LED_COUNT; i++) {
107+
pixel[i] = (rgb_color){15,15,15};
108+
}
85109
}
86-
} else {
110+
} else {
87111
for(unsigned char i=0; i<LED_COUNT; i++) {
88112
pixel[i] = (rgb_color){0,0,0};
89113
}

0 commit comments

Comments
 (0)