Used for Flow Graph
Node.cpp@0:be00bb0372eb, 2011-10-30 (annotated)
- Committer:
- henryeherman
- Date:
- Sun Oct 30 11:29:10 2011 +0000
- Revision:
- 0:be00bb0372eb
For 202A
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
henryeherman | 0:be00bb0372eb | 1 | #include "Node.h" |
henryeherman | 0:be00bb0372eb | 2 | |
henryeherman | 0:be00bb0372eb | 3 | unsigned int crc_table[16] = { |
henryeherman | 0:be00bb0372eb | 4 | 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac, |
henryeherman | 0:be00bb0372eb | 5 | 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c, |
henryeherman | 0:be00bb0372eb | 6 | 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, |
henryeherman | 0:be00bb0372eb | 7 | 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c |
henryeherman | 0:be00bb0372eb | 8 | }; |
henryeherman | 0:be00bb0372eb | 9 | |
henryeherman | 0:be00bb0372eb | 10 | unsigned long crc_update(unsigned long crc, unsigned int data) |
henryeherman | 0:be00bb0372eb | 11 | { |
henryeherman | 0:be00bb0372eb | 12 | char tbl_idx; |
henryeherman | 0:be00bb0372eb | 13 | tbl_idx = crc ^ (data >> (0*4)); |
henryeherman | 0:be00bb0372eb | 14 | crc = crc_table[tbl_idx & 0x0f] ^ (crc >> 4); |
henryeherman | 0:be00bb0372eb | 15 | tbl_idx = crc ^ (data >> (1*4)); |
henryeherman | 0:be00bb0372eb | 16 | crc = crc_table[tbl_idx & 0x0f] ^ (crc >> 4); |
henryeherman | 0:be00bb0372eb | 17 | return crc; |
henryeherman | 0:be00bb0372eb | 18 | } |
henryeherman | 0:be00bb0372eb | 19 | |
henryeherman | 0:be00bb0372eb | 20 | unsigned long crc_string(char *s) { |
henryeherman | 0:be00bb0372eb | 21 | unsigned long crc = ~0L; |
henryeherman | 0:be00bb0372eb | 22 | while(*s) |
henryeherman | 0:be00bb0372eb | 23 | crc = crc_update(crc, *s++); |
henryeherman | 0:be00bb0372eb | 24 | crc = ~crc; |
henryeherman | 0:be00bb0372eb | 25 | return crc; |
henryeherman | 0:be00bb0372eb | 26 | } |
henryeherman | 0:be00bb0372eb | 27 | |
henryeherman | 0:be00bb0372eb | 28 | |
henryeherman | 0:be00bb0372eb | 29 | unsigned long crc_len(char *s, unsigned int len) { |
henryeherman | 0:be00bb0372eb | 30 | unsigned long crc = ~0L; |
henryeherman | 0:be00bb0372eb | 31 | for(int idx=0;idx<len;idx++) |
henryeherman | 0:be00bb0372eb | 32 | crc = crc_update(crc, *s++); |
henryeherman | 0:be00bb0372eb | 33 | crc = ~crc; |
henryeherman | 0:be00bb0372eb | 34 | return crc; |
henryeherman | 0:be00bb0372eb | 35 | } |
henryeherman | 0:be00bb0372eb | 36 | |
henryeherman | 0:be00bb0372eb | 37 | Node::Node(unsigned int n, unsigned int k, PinName pinid) : pin(pinid) { |
henryeherman | 0:be00bb0372eb | 38 | setupInputOutput(n,k); |
henryeherman | 0:be00bb0372eb | 39 | delay=0; |
henryeherman | 0:be00bb0372eb | 40 | } |
henryeherman | 0:be00bb0372eb | 41 | |
henryeherman | 0:be00bb0372eb | 42 | Node::Node(unsigned int n, unsigned int k, PinName pinid, unsigned int delay_ms) : pin(pinid) { |
henryeherman | 0:be00bb0372eb | 43 | setupInputOutput(n,k); |
henryeherman | 0:be00bb0372eb | 44 | delay=delay_ms; |
henryeherman | 0:be00bb0372eb | 45 | } |
henryeherman | 0:be00bb0372eb | 46 | |
henryeherman | 0:be00bb0372eb | 47 | int Node::getSize() { |
henryeherman | 0:be00bb0372eb | 48 | return ksize; |
henryeherman | 0:be00bb0372eb | 49 | } |
henryeherman | 0:be00bb0372eb | 50 | |
henryeherman | 0:be00bb0372eb | 51 | char *Node::execute(char *data) { |
henryeherman | 0:be00bb0372eb | 52 | toggleHigh(); |
henryeherman | 0:be00bb0372eb | 53 | *tempbuffer=crc_len(data, nsize); |
henryeherman | 0:be00bb0372eb | 54 | for(int idx=1;idx<ksize;idx++) { |
henryeherman | 0:be00bb0372eb | 55 | *(tempbuffer+idx)=(char)crc_update(0,(*(tempbuffer+idx-1))); |
henryeherman | 0:be00bb0372eb | 56 | } |
henryeherman | 0:be00bb0372eb | 57 | wait_ms(delay); |
henryeherman | 0:be00bb0372eb | 58 | toggleLow(); |
henryeherman | 0:be00bb0372eb | 59 | return tempbuffer; |
henryeherman | 0:be00bb0372eb | 60 | } |
henryeherman | 0:be00bb0372eb | 61 | |
henryeherman | 0:be00bb0372eb | 62 | |
henryeherman | 0:be00bb0372eb | 63 | void Node::setupInputOutput(unsigned int n, unsigned int k) { |
henryeherman | 0:be00bb0372eb | 64 | nsize=n; |
henryeherman | 0:be00bb0372eb | 65 | ksize=k; |
henryeherman | 0:be00bb0372eb | 66 | data_ = (char *)malloc(nsize); |
henryeherman | 0:be00bb0372eb | 67 | tempbuffer = (char *)malloc(nsize+ksize); |
henryeherman | 0:be00bb0372eb | 68 | } |
henryeherman | 0:be00bb0372eb | 69 | |
henryeherman | 0:be00bb0372eb | 70 | void Node::toggleHigh() { |
henryeherman | 0:be00bb0372eb | 71 | pin=1; |
henryeherman | 0:be00bb0372eb | 72 | } |
henryeherman | 0:be00bb0372eb | 73 | void Node::toggleLow() { |
henryeherman | 0:be00bb0372eb | 74 | pin=0; |
henryeherman | 0:be00bb0372eb | 75 | } |