![](/media/cache/profiles/0f2c9a93eea6f38fabb3acb1c31488c6.50x50_q85.jpg)
A USB/360deg Rotational device, communicating via blue-tooth, test code
Dependencies: FatFileSystem mbed
Utils.cpp@0:87da38093be9, 2012-12-21 (annotated)
- Committer:
- lolpcc
- Date:
- Fri Dec 21 11:37:38 2012 +0000
- Revision:
- 0:87da38093be9
Used to test the Lazy Susan at work, hence the A2D code in Utils.cpp
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lolpcc | 0:87da38093be9 | 1 | |
lolpcc | 0:87da38093be9 | 2 | |
lolpcc | 0:87da38093be9 | 3 | #include "mbed.h" |
lolpcc | 0:87da38093be9 | 4 | #include "Utils.h" |
lolpcc | 0:87da38093be9 | 5 | Serial rs232(p13, p14); // tx, rx |
lolpcc | 0:87da38093be9 | 6 | AnalogIn ain(p15); |
lolpcc | 0:87da38093be9 | 7 | |
lolpcc | 0:87da38093be9 | 8 | extern char global_buf1[0x20]; |
lolpcc | 0:87da38093be9 | 9 | extern char global_buf2[0x20]; |
lolpcc | 0:87da38093be9 | 10 | |
lolpcc | 0:87da38093be9 | 11 | void setup_rs232(void) |
lolpcc | 0:87da38093be9 | 12 | { |
lolpcc | 0:87da38093be9 | 13 | rs232.baud(19200); |
lolpcc | 0:87da38093be9 | 14 | } |
lolpcc | 0:87da38093be9 | 15 | |
lolpcc | 0:87da38093be9 | 16 | int read_ana(void) |
lolpcc | 0:87da38093be9 | 17 | { |
lolpcc | 0:87da38093be9 | 18 | float a; |
lolpcc | 0:87da38093be9 | 19 | int b; |
lolpcc | 0:87da38093be9 | 20 | a = ain.read(); |
lolpcc | 0:87da38093be9 | 21 | a = a * 360; |
lolpcc | 0:87da38093be9 | 22 | b = ftoi(a); |
lolpcc | 0:87da38093be9 | 23 | return(b); |
lolpcc | 0:87da38093be9 | 24 | } |
lolpcc | 0:87da38093be9 | 25 | |
lolpcc | 0:87da38093be9 | 26 | void send_out(char *buf) |
lolpcc | 0:87da38093be9 | 27 | { |
lolpcc | 0:87da38093be9 | 28 | printf("%s",buf); |
lolpcc | 0:87da38093be9 | 29 | rs232.printf("%s",buf); |
lolpcc | 0:87da38093be9 | 30 | } |
lolpcc | 0:87da38093be9 | 31 | |
lolpcc | 0:87da38093be9 | 32 | void printfBytes(const char* s, const u8* data, int len) |
lolpcc | 0:87da38093be9 | 33 | { |
lolpcc | 0:87da38093be9 | 34 | float a; |
lolpcc | 0:87da38093be9 | 35 | int b; |
lolpcc | 0:87da38093be9 | 36 | a = ain.read(); |
lolpcc | 0:87da38093be9 | 37 | a = a*360; |
lolpcc | 0:87da38093be9 | 38 | b = ftoi(a); |
lolpcc | 0:87da38093be9 | 39 | printf("%s %d: POS %d ",s,len,b); |
lolpcc | 0:87da38093be9 | 40 | rs232.printf("%s %d: POS %d ",s,len,b); |
lolpcc | 0:87da38093be9 | 41 | if (len > 256) |
lolpcc | 0:87da38093be9 | 42 | len = 256; |
lolpcc | 0:87da38093be9 | 43 | |
lolpcc | 0:87da38093be9 | 44 | while (len-- > 0){ |
lolpcc | 0:87da38093be9 | 45 | printf(" %02X",*data); |
lolpcc | 0:87da38093be9 | 46 | rs232.printf(" %02X",*data++); |
lolpcc | 0:87da38093be9 | 47 | } |
lolpcc | 0:87da38093be9 | 48 | printf("\n\r"); |
lolpcc | 0:87da38093be9 | 49 | rs232.printf("\n\r"); |
lolpcc | 0:87da38093be9 | 50 | } |
lolpcc | 0:87da38093be9 | 51 | |
lolpcc | 0:87da38093be9 | 52 | void printHexLine(const u8* d, int addr, int len) |
lolpcc | 0:87da38093be9 | 53 | { |
lolpcc | 0:87da38093be9 | 54 | printf("%04X ",addr); |
lolpcc | 0:87da38093be9 | 55 | int i; |
lolpcc | 0:87da38093be9 | 56 | for (i = 0; i < len; i++) |
lolpcc | 0:87da38093be9 | 57 | printf("%02X ",d[i]); |
lolpcc | 0:87da38093be9 | 58 | for (;i < 16; i++) |
lolpcc | 0:87da38093be9 | 59 | printf(" "); |
lolpcc | 0:87da38093be9 | 60 | char s[16+1]; |
lolpcc | 0:87da38093be9 | 61 | memset(s,0,sizeof(s)); |
lolpcc | 0:87da38093be9 | 62 | for (i = 0; i < len; i++) |
lolpcc | 0:87da38093be9 | 63 | { |
lolpcc | 0:87da38093be9 | 64 | int c = d[i]; |
lolpcc | 0:87da38093be9 | 65 | if (c < 0x20 || c > 0x7E) |
lolpcc | 0:87da38093be9 | 66 | c = '.'; |
lolpcc | 0:87da38093be9 | 67 | s[i] = c; |
lolpcc | 0:87da38093be9 | 68 | } |
lolpcc | 0:87da38093be9 | 69 | printf("%s\n\r",s); |
lolpcc | 0:87da38093be9 | 70 | } |
lolpcc | 0:87da38093be9 | 71 | |
lolpcc | 0:87da38093be9 | 72 | void printHex(const u8* d, int len) |
lolpcc | 0:87da38093be9 | 73 | { |
lolpcc | 0:87da38093be9 | 74 | int addr = 0; |
lolpcc | 0:87da38093be9 | 75 | while (len) |
lolpcc | 0:87da38093be9 | 76 | { |
lolpcc | 0:87da38093be9 | 77 | int count = len; |
lolpcc | 0:87da38093be9 | 78 | if (count > 16) |
lolpcc | 0:87da38093be9 | 79 | count = 16; |
lolpcc | 0:87da38093be9 | 80 | printHexLine(d+addr,addr,count); |
lolpcc | 0:87da38093be9 | 81 | addr += 16; |
lolpcc | 0:87da38093be9 | 82 | len -= count; |
lolpcc | 0:87da38093be9 | 83 | } |
lolpcc | 0:87da38093be9 | 84 | } |