📄 sos.c
字号:
/* sos.c - small VxWorks application */#include "vxWorks.h"#include "taskLib.h"#include "sysLib.h"#define INTER_MESSAGE_MSECS 2000#define CLOCK sysClkRateGet()#ifndef COUNT_MSECS#define COUNT_MSECS 100#endif /* COUNT_MSECS */#define DELAY_COUNTS(n) taskDelay (((n) * COUNT_MSECS * CLOCK + 500) / 1000)#ifdef LED#define DIT ledDit()#define DAH ledDah()#define INTRA_CHAR_SPC DELAY_COUNTS(1)#define INTER_CHAR_SPC DELAY_COUNTS(3)#define WORD_SPC DELAY_COUNTS(7)#define MSG_SPC#define mv162 1#ifndef BSP#define BSP mv162#endif /* BSP */#if BSP == mv162static volatile UINT32 * pBcr = (UINT32 *) 0xFFF40060;#define BDFLO 0x02000000#define LED_ON *pBcr |= BDFLO#define LED_OFF *pBcr &= (~ BDFLO)#else#error "Unknown BSP."#endifvoid ledDit (void) { LED_ON; taskDelay ((COUNT_MSECS * CLOCK + 500) / 1000); LED_OFF; }void ledDah (void) { LED_ON; taskDelay ((3 * COUNT_MSECS * CLOCK + 500) / 1000); LED_OFF; }#else /* LED not defined */#include "stdio.h"#define DIT \ do {\ printf (".");\ DELAY_COUNTS(1);\ } while (FALSE)#define DAH \ do {\ printf ("-");\ DELAY_COUNTS(3);\ } while (FALSE)#define INTRA_CHAR_SPC DELAY_COUNTS(1)#define INTER_CHAR_SPC \ do {\ printf (" ");\ DELAY_COUNTS(3);\ } while (FALSE)#define WORD_SPC \ do {\ printf (" ");\ DELAY_COUNTS(7);\ } while (FALSE);#define MSG_SPC printf ("\n")#endif /* LED */typedef struct coderec { UINT8 n; /* number of dits or dahs */ UINT8 code; /* binary encoding of morse char: dit=0, dah=1 */ } CODEREC;LOCAL CODEREC letters [] = { {2, 0x01}, /* A .- */ {4, 0x08}, /* B -... */ {4, 0x0a}, /* C -.-. */ {3, 0x04}, /* D -.. */ {1, 0x00}, /* E . */ {4, 0x02}, /* F ..-. */ {3, 0x06}, /* G --. */ {4, 0x00}, /* H .... */ {2, 0x00}, /* I .. */ {4, 0x07}, /* J .--- */ {3, 0x05}, /* K -.- */ {4, 0x04}, /* L .-.. */ {2, 0x03}, /* M -- */ {2, 0x02}, /* N -. */ {3, 0x07}, /* O --- */ {4, 0x06}, /* P .--. */ {4, 0x0d}, /* Q --.- */ {3, 0x02}, /* R .-. */ {3, 0x00}, /* S ... */ {1, 0x01}, /* T - */ {3, 0x01}, /* U ..- */ {4, 0x01}, /* V ...- */ {3, 0x03}, /* W .-- */ {4, 0x09}, /* X -..- */ {4, 0x0b}, /* Y -.-- */ {4, 0x0c} /* Z --.. */ };LOCAL CODEREC numbers [] = { {5, 0x1f}, /* 0 ----- */ {5, 0x0f}, /* 1 .---- */ {5, 0x07}, /* 2 ..--- */ {5, 0x03}, /* 3 ...-- */ {5, 0x01}, /* 4 ....- */ {5, 0x00}, /* 5 ..... */ {5, 0x10}, /* 6 -.... */ {5, 0x18}, /* 7 --... */ {5, 0x1c}, /* 8 ---.. */ {5, 0x1e} /* 9 ----. */ };LOCAL CODEREC punctuation [] = { {6, 0x15}, /* Full-Stop (period) .-.-.- */ {6, 0x33}, /* Comma --..-- */ {6, 0x38}, /* Colon ---... */ {6, 0x0c}, /* Question mark ..--.. */ {6, 0x1e}, /* Apostrophe .----. */ {6, 0x21}, /* Hyphen -....- */ {5, 0x12}, /* Fraction bar -..-. */ {6, 0x2d}, /* Brackets (paren.) -.--.- */ {6, 0x12} /* Quotation marks .-..-. */ };LOCAL void codeOut ( CODEREC * cr ) { unsigned int mask = 1 << (cr->n - 1); UINT8 code = cr->code; while (mask != 0) { if ((mask & code) != 0) DAH; else DIT; mask >>= 1; if (mask != 0) INTRA_CHAR_SPC; } }LOCAL STATUS charOut ( char ch ) { STATUS stat = OK; unsigned char uc = (unsigned) ch; if ('A' <= uc && uc <= 'Z') codeOut (& letters [uc - 'A']); else if ('a' <= uc && uc <= 'z') codeOut (& letters [uc - 'a']); else if ('0' <= uc && uc <= '9') codeOut (& numbers [uc - '0']); else { int i = -1; switch (uc) { case '.' : i = 0; break; case ',' : i = 1; break; case ':' : i = 2; break; case '?' : i = 3; break; case '\'' : i = 4; break; case '-' : i = 5; break; case '/' : i = 6; break; case '(' : case ')' : case '[' : case ']' : case '{' : case '}' : i = 7; break; case '"' : i = 8; break; } if (i != -1) codeOut (& punctuation [i]); else stat = ERROR; } return stat; }STATUS morse ( char * msg ) { BOOL charLast = FALSE; char ch; while ( (ch = *msg++) != '\0') { if (ch == ' ') { WORD_SPC; charLast = FALSE; } else { if (charLast) INTER_CHAR_SPC; if (charOut (ch) != OK) return ERROR; charLast = TRUE; } } MSG_SPC; return OK; }void sos ( char * message ) { FOREVER { morse (message); taskDelay (INTER_MESSAGE_MSECS * sysClkRateGet() / 1000); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -