Created
May 15, 2013 10:54
-
-
Save hidsh/5583151 to your computer and use it in GitHub Desktop.
通信用リングバッファのサンプルその2。とりあえずモジュール化しといた。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include "mystd.h" | |
| #define RINGBUF_SIZE 8 | |
| #include "ringbuf.h" | |
| RINGBUF rbuf; | |
| void dump(void) | |
| { | |
| u1 dpw[8], dpr[8]; | |
| for(int i=0; i<8; i++) { | |
| dpw[i] = dpr[i] = ' '; | |
| } | |
| dpw[rbuf.pw] = 'w'; | |
| dpr[rbuf.pr] = 'r'; | |
| printf ("-------------------------------------------------\n"); | |
| printf("[%1d] [%1d] [%1d] [%1d] [%1d] [%1d] [%1d] [%1d]\n", 0, 1, 2, 3, 4, 5, 6, 7); | |
| printf(" %1c %1c %1c %1c %1c %1c %1c %1c \n", dpw[0], dpw[1], dpw[2], dpw[3], dpw[4], dpw[5], dpw[6], dpw[7]); | |
| printf("%2X %2X %2X %2X %2X %2X %2X %2X\n", rbuf.buf[0], rbuf.buf[1], rbuf.buf[2], rbuf.buf[3], rbuf.buf[4], rbuf.buf[5], rbuf.buf[6], rbuf.buf[7]); | |
| printf(" %1c %1c %1c %1c %1c %1c %1c %1c \n", dpr[0], dpr[1], dpr[2], dpr[3], dpr[4], dpr[5], dpr[6], dpr[7]); | |
| } | |
| int main(void) | |
| { | |
| u1 v = 3; | |
| ringbuf_init(&rbuf); | |
| dump(); | |
| for(int i=0; i<20; i++){ | |
| u1 data; | |
| if(ringbuf_read(&rbuf, &data)) printf("read:%X\n", data); | |
| } | |
| dump(); | |
| for(int i=0; i<20; i++) ringbuf_write(&rbuf, v++); | |
| dump(); | |
| for(int i=0; i<20; i++){ | |
| u1 data; | |
| if(ringbuf_read(&rbuf, &data)) printf("read:%X\n", data); | |
| } | |
| dump(); | |
| for(int i=0; i<3; i++) ringbuf_write(&rbuf, v++); | |
| dump(); | |
| for(int i=0; i<20; i++){ | |
| u1 data; | |
| if(ringbuf_read(&rbuf, &data)) printf("read:%X\n", data); | |
| } | |
| dump(); | |
| for(int i=0; i<20; i++) ringbuf_write(&rbuf, v++); | |
| dump(); | |
| for(int i=0; i<20; i++){ | |
| u1 data; | |
| if(ringbuf_read(&rbuf, &data)) printf("read:%X\n", data); | |
| } | |
| dump(); | |
| return EXIT_SUCCESS; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* mystd.h */ | |
| #ifndef __MYSTD_H__ | |
| #define __MYSTD_H__ | |
| /* types and values */ | |
| typedef unsigned char u1; | |
| typedef unsigned short u2; | |
| typedef unsigned long u4; | |
| typedef signed char s1; | |
| typedef signed short s2; | |
| typedef signed long s4; | |
| typedef unsigned char bool; | |
| #define FALSE 0 | |
| #define TRUE 1 | |
| #define LO 0 | |
| #define HI 1 | |
| #define OFF 0 | |
| #define ON 1 | |
| #define NG 0 | |
| #define OK 1 | |
| #define EXIT_SUCCESS 0 | |
| #define EXIT_FAILURE 1 | |
| /* pseudo functions */ | |
| #define __ei() sei() | |
| #define __di() cli() | |
| #endif /* __MYSTD_H__ */ | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $gcc -std=c99 main.c && ./a.out | |
| ------------------------------------------------- | |
| [0] [1] [2] [3] [4] [5] [6] [7] | |
| w | |
| FF FF FF FF FF FF FF FF | |
| r | |
| ------------------------------------------------- | |
| [0] [1] [2] [3] [4] [5] [6] [7] | |
| w | |
| FF FF FF FF FF FF FF FF | |
| r | |
| ------------------------------------------------- | |
| [0] [1] [2] [3] [4] [5] [6] [7] | |
| w | |
| FF 3 4 5 6 7 8 9 | |
| r | |
| read:3 | |
| read:4 | |
| read:5 | |
| read:6 | |
| read:7 | |
| read:8 | |
| read:9 | |
| ------------------------------------------------- | |
| [0] [1] [2] [3] [4] [5] [6] [7] | |
| w | |
| FF 3 4 5 6 7 8 9 | |
| r | |
| ------------------------------------------------- | |
| [0] [1] [2] [3] [4] [5] [6] [7] | |
| w | |
| 17 18 19 5 6 7 8 9 | |
| r | |
| read:17 | |
| read:18 | |
| read:19 | |
| ------------------------------------------------- | |
| [0] [1] [2] [3] [4] [5] [6] [7] | |
| w | |
| 17 18 19 5 6 7 8 9 | |
| r | |
| ------------------------------------------------- | |
| [0] [1] [2] [3] [4] [5] [6] [7] | |
| w | |
| 1F 20 19 1A 1B 1C 1D 1E | |
| r | |
| read:1A | |
| read:1B | |
| read:1C | |
| read:1D | |
| read:1E | |
| read:1F | |
| read:20 | |
| ------------------------------------------------- | |
| [0] [1] [2] [3] [4] [5] [6] [7] | |
| w | |
| 1F 20 19 1A 1B 1C 1D 1E | |
| r | |
| $ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* ringbuf.h */ | |
| #ifndef __RINGBUF_H__ | |
| #define __RINGBUF_H__ | |
| #include "mystd.h" | |
| #ifdef RINGBUF_SIZE | |
| #define SIZE RINGBUF_SIZE | |
| #else | |
| #error "You need to define RINGBUF_SIZE" | |
| #endif | |
| /* types and values */ | |
| typedef struct { | |
| u1 buf[SIZE]; | |
| u1 pr, pw; | |
| } RINGBUF; | |
| /* service functions */ | |
| void ringbuf_init(RINGBUF *r) | |
| { | |
| for(u1 i=0; i<SIZE; i++) | |
| r->buf[i] = 0xFF; | |
| r->pr = r->pw = 0; | |
| } | |
| bool ringbuf_write(RINGBUF *r, u1 ch) | |
| { | |
| u1 nextw = (r->pw+1) % SIZE; | |
| if(nextw == r->pr) return NG; | |
| r->pw = nextw; | |
| r->buf[nextw] = ch; | |
| return OK; | |
| } | |
| bool ringbuf_read(RINGBUF *r, u1 *ch) | |
| { | |
| u1 nextw = (r->pw+1) % SIZE; | |
| u1 nextr = (r->pr+1) % SIZE; | |
| if(nextw == nextr) return NG; | |
| r->pr = nextr; | |
| *ch = r->buf[nextr]; | |
| return OK; | |
| } | |
| #undef SIZE | |
| #endif /* __RINGBUF_H__ */ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment