Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Ken_CAN_test.cpp@2:11339018dda6, 2022-06-15 (annotated)
- Committer:
- kendunlop
- Date:
- Wed Jun 15 08:48:18 2022 +0000
- Revision:
- 2:11339018dda6
- Parent:
- 1:19d183cf2689
- Child:
- 3:79133dcea836
First iteration of timer function.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kendunlop | 1:19d183cf2689 | 1 | //This program was created by Ken 8th June 2022 to test the CAN output of a CAN Gateway. |
kendunlop | 1:19d183cf2689 | 2 | #define kMajorVersion 0 |
kendunlop | 1:19d183cf2689 | 3 | #define kMinorVersion 5 |
kendunlop | 1:19d183cf2689 | 4 | |
kendunlop | 1:19d183cf2689 | 5 | #include "mbed.h" //Including mbed libraries. This should be all it needs to use the Mbed and CAN. |
kendunlop | 1:19d183cf2689 | 6 | #include <string> //This is needed to make text strings work |
kendunlop | 1:19d183cf2689 | 7 | #include <stdio.h> |
kendunlop | 1:19d183cf2689 | 8 | #include <ctype.h> //For 'is digit' condition |
kendunlop | 1:19d183cf2689 | 9 | #include <sstream> //This is used to convert an integer to hex |
kendunlop | 2:11339018dda6 | 10 | #include <time.h> //To measure how long a process took? Includes clock_t and CLOCKS_PER_SEC |
kendunlop | 1:19d183cf2689 | 11 | |
kendunlop | 1:19d183cf2689 | 12 | RawSerial pc(USBTX, USBRX); // USB UART Terminal. Is needed for 'pc.printf' functions to work. |
kendunlop | 1:19d183cf2689 | 13 | CAN CanBus(p9, p10); //CANBus to use pins 9 and 10. This defines CanBus so other 'CanBus' lines will work. |
kendunlop | 2:11339018dda6 | 14 | //CAN CanBus2(p29, p30); //CANBus2 for recieving to use pins 29 and 30. This defines CanBus so other 'CanBus' lines will work. |
kendunlop | 1:19d183cf2689 | 15 | CANMessage messageIn; |
kendunlop | 1:19d183cf2689 | 16 | |
kendunlop | 1:19d183cf2689 | 17 | //Define a CAN message to send |
kendunlop | 1:19d183cf2689 | 18 | CANMessage messageOut1; |
kendunlop | 1:19d183cf2689 | 19 | |
kendunlop | 1:19d183cf2689 | 20 | //Set messageOutText to a default message so it's defined. |
kendunlop | 1:19d183cf2689 | 21 | string messageOutText = "3AF 8 01 AA BB CC DD EE FF 99"; |
kendunlop | 1:19d183cf2689 | 22 | string part = ""; |
kendunlop | 1:19d183cf2689 | 23 | int partincrement = 0; |
kendunlop | 1:19d183cf2689 | 24 | int incrementer = 0; |
kendunlop | 1:19d183cf2689 | 25 | int CANCount = 0; |
kendunlop | 1:19d183cf2689 | 26 | int spamon = 0; |
kendunlop | 1:19d183cf2689 | 27 | int spamcount = 0; |
kendunlop | 2:11339018dda6 | 28 | int spamstarttime = 0; |
kendunlop | 2:11339018dda6 | 29 | int spamendtime = 0; |
kendunlop | 1:19d183cf2689 | 30 | |
kendunlop | 2:11339018dda6 | 31 | //A function to deal with each CAN message part (e.g.: 301, 8, 01, 02...) |
kendunlop | 1:19d183cf2689 | 32 | void dealwithpart(void) |
kendunlop | 1:19d183cf2689 | 33 | { |
kendunlop | 1:19d183cf2689 | 34 | int hextotal = 0; |
kendunlop | 1:19d183cf2689 | 35 | partincrement = partincrement + 1; |
kendunlop | 1:19d183cf2689 | 36 | //pc.printf("Dealing with part %d. (%s)\r\n", partincrement, part); |
kendunlop | 1:19d183cf2689 | 37 | //int partincrementb = 0; |
kendunlop | 1:19d183cf2689 | 38 | int textlength = part.length(); |
kendunlop | 1:19d183cf2689 | 39 | //int characterinc = 0; |
kendunlop | 1:19d183cf2689 | 40 | //pc.printf("That's %d characters long.\r\n", textlength); |
kendunlop | 1:19d183cf2689 | 41 | for (int i = 0; i < part.size(); i++) |
kendunlop | 1:19d183cf2689 | 42 | { |
kendunlop | 1:19d183cf2689 | 43 | //pc.printf("Examining character %d/%d.\r\n", (i+1), textlength); |
kendunlop | 1:19d183cf2689 | 44 | char individualcharacter = part.at(i); |
kendunlop | 1:19d183cf2689 | 45 | //pc.printf("That's '%c'.\r\n", individualcharacter); |
kendunlop | 1:19d183cf2689 | 46 | int numberized = 0; |
kendunlop | 1:19d183cf2689 | 47 | if(isdigit(individualcharacter)) |
kendunlop | 1:19d183cf2689 | 48 | { |
kendunlop | 1:19d183cf2689 | 49 | //pc.printf("That character is a digit.\r\n"); |
kendunlop | 1:19d183cf2689 | 50 | numberized = individualcharacter - '0'; |
kendunlop | 1:19d183cf2689 | 51 | //pc.printf("Numberized that's '%d'.\r\n", numberized); |
kendunlop | 1:19d183cf2689 | 52 | } |
kendunlop | 1:19d183cf2689 | 53 | else |
kendunlop | 1:19d183cf2689 | 54 | { |
kendunlop | 1:19d183cf2689 | 55 | //pc.printf("That character is NOT a digit.\r\n"); |
kendunlop | 1:19d183cf2689 | 56 | int asciivalue = toupper(individualcharacter); |
kendunlop | 1:19d183cf2689 | 57 | //pc.printf("Ascii value is %d.\r\n", asciivalue); |
kendunlop | 1:19d183cf2689 | 58 | numberized = asciivalue - 55; |
kendunlop | 1:19d183cf2689 | 59 | //pc.printf("Hex value is %d.\r\n", numberized); |
kendunlop | 1:19d183cf2689 | 60 | } |
kendunlop | 1:19d183cf2689 | 61 | //pc.printf("Eventual numberization is %d.\r\n", numberized); |
kendunlop | 1:19d183cf2689 | 62 | //pc.printf("Hex total is now %d.\r\n", hextotal); |
kendunlop | 1:19d183cf2689 | 63 | int powertoraise = part.size() - (i+1); |
kendunlop | 1:19d183cf2689 | 64 | //pc.printf("Must multiply by 16 to the power of %d.\r\n", powertoraise); |
kendunlop | 1:19d183cf2689 | 65 | int amounttoadd = numberized; |
kendunlop | 1:19d183cf2689 | 66 | //pc.printf("powertoraise is '%d'.\r\n", powertoraise); |
kendunlop | 1:19d183cf2689 | 67 | if (powertoraise == 1) |
kendunlop | 1:19d183cf2689 | 68 | { |
kendunlop | 1:19d183cf2689 | 69 | amounttoadd = numberized * 16; |
kendunlop | 1:19d183cf2689 | 70 | //pc.printf("Multiplying by 16.\r\n"); |
kendunlop | 1:19d183cf2689 | 71 | //pc.printf("powertoraise is '%d'.\r\n", powertoraise); |
kendunlop | 1:19d183cf2689 | 72 | } |
kendunlop | 1:19d183cf2689 | 73 | if (powertoraise == 2) |
kendunlop | 1:19d183cf2689 | 74 | { |
kendunlop | 1:19d183cf2689 | 75 | amounttoadd = numberized * 256; |
kendunlop | 1:19d183cf2689 | 76 | //pc.printf("Multiplying by 256.\r\n"); |
kendunlop | 1:19d183cf2689 | 77 | } |
kendunlop | 1:19d183cf2689 | 78 | //pc.printf("Amount to add is therefore %d.\r\n", amounttoadd); |
kendunlop | 1:19d183cf2689 | 79 | hextotal = hextotal + amounttoadd; |
kendunlop | 1:19d183cf2689 | 80 | //pc.printf("hextotal so far for this part is therefore %d.\r\n", hextotal); |
kendunlop | 1:19d183cf2689 | 81 | } |
kendunlop | 1:19d183cf2689 | 82 | //pc.printf("hextotal for whole part is therefore %d.\r\n", hextotal); |
kendunlop | 1:19d183cf2689 | 83 | //pc.printf("Need to convert that into true hex.\r\n"); |
kendunlop | 1:19d183cf2689 | 84 | std::stringstream sstream; |
kendunlop | 1:19d183cf2689 | 85 | sstream << std::hex << hextotal; |
kendunlop | 1:19d183cf2689 | 86 | //pc.printf("StringSteam says '%s'.\r\n", sstream.str()); |
kendunlop | 1:19d183cf2689 | 87 | if (partincrement == 1) |
kendunlop | 1:19d183cf2689 | 88 | {messageOut1.id = hextotal;} |
kendunlop | 1:19d183cf2689 | 89 | if (partincrement == 2) |
kendunlop | 1:19d183cf2689 | 90 | {messageOut1.len = hextotal;} |
kendunlop | 1:19d183cf2689 | 91 | if (partincrement >= 3) |
kendunlop | 1:19d183cf2689 | 92 | {messageOut1.data[partincrement-3] = hextotal;} |
kendunlop | 1:19d183cf2689 | 93 | //pc.printf("Part %d complete.\r\n", partincrement); |
kendunlop | 1:19d183cf2689 | 94 | //pc.printf("--------------------------------------\r\n"); |
kendunlop | 1:19d183cf2689 | 95 | } |
kendunlop | 1:19d183cf2689 | 96 | |
kendunlop | 1:19d183cf2689 | 97 | //An attempted function to get a coherent CAN message from one, uninterrupted string |
kendunlop | 1:19d183cf2689 | 98 | void getCANfromstring(void) |
kendunlop | 1:19d183cf2689 | 99 | { |
kendunlop | 1:19d183cf2689 | 100 | //pc.printf("messageOutText is '%s'\r\n", messageOutText); |
kendunlop | 1:19d183cf2689 | 101 | remove(messageOutText.begin(), messageOutText.end(), ' '); //Remove the spaces from the text to send out so it can be parsed. |
kendunlop | 1:19d183cf2689 | 102 | //pc.printf("After removing spaces, messageOutText is '%s'\r\n", messageOutText); |
kendunlop | 1:19d183cf2689 | 103 | string startofstring = messageOutText.substr(0,20); //Take the first 20 characters of the newly-formed string to get a spaceless CAN message. |
kendunlop | 1:19d183cf2689 | 104 | //pc.printf("String to parse is '%s'.\r\n", startofstring); |
kendunlop | 1:19d183cf2689 | 105 | partincrement = 0; |
kendunlop | 1:19d183cf2689 | 106 | part = startofstring.substr(0,3); |
kendunlop | 1:19d183cf2689 | 107 | dealwithpart(); |
kendunlop | 1:19d183cf2689 | 108 | part = startofstring.substr(3,1); |
kendunlop | 1:19d183cf2689 | 109 | dealwithpart(); |
kendunlop | 1:19d183cf2689 | 110 | part = startofstring.substr(4,2); |
kendunlop | 1:19d183cf2689 | 111 | dealwithpart(); |
kendunlop | 1:19d183cf2689 | 112 | part = startofstring.substr(6,2); |
kendunlop | 1:19d183cf2689 | 113 | dealwithpart(); |
kendunlop | 1:19d183cf2689 | 114 | part = startofstring.substr(8,2); |
kendunlop | 1:19d183cf2689 | 115 | dealwithpart(); |
kendunlop | 1:19d183cf2689 | 116 | part = startofstring.substr(10,2); |
kendunlop | 1:19d183cf2689 | 117 | dealwithpart(); |
kendunlop | 1:19d183cf2689 | 118 | part = startofstring.substr(12,2); |
kendunlop | 1:19d183cf2689 | 119 | dealwithpart(); |
kendunlop | 1:19d183cf2689 | 120 | part = startofstring.substr(14,2); |
kendunlop | 1:19d183cf2689 | 121 | dealwithpart(); |
kendunlop | 1:19d183cf2689 | 122 | part = startofstring.substr(16,2); |
kendunlop | 1:19d183cf2689 | 123 | dealwithpart(); |
kendunlop | 1:19d183cf2689 | 124 | part = startofstring.substr(18,2); |
kendunlop | 1:19d183cf2689 | 125 | dealwithpart(); |
kendunlop | 1:19d183cf2689 | 126 | } |
kendunlop | 1:19d183cf2689 | 127 | |
kendunlop | 1:19d183cf2689 | 128 | void defineCANmessage(void) |
kendunlop | 1:19d183cf2689 | 129 | { |
kendunlop | 1:19d183cf2689 | 130 | //pc.printf("Defining CAN message 1.\r\n"); |
kendunlop | 1:19d183cf2689 | 131 | //messageOut1.format = CANStandard; |
kendunlop | 1:19d183cf2689 | 132 | //messageOut1.id = 0x301; |
kendunlop | 1:19d183cf2689 | 133 | //messageOut1.len = 8; |
kendunlop | 1:19d183cf2689 | 134 | //messageOut1.data[0] = 0x06; |
kendunlop | 1:19d183cf2689 | 135 | //messageOut1.data[1] = 0x3f; |
kendunlop | 1:19d183cf2689 | 136 | //messageOut1.data[2] = 0xb2; |
kendunlop | 1:19d183cf2689 | 137 | //messageOut1.data[3] = 0x29; |
kendunlop | 1:19d183cf2689 | 138 | //messageOut1.data[4] = 0x19; |
kendunlop | 1:19d183cf2689 | 139 | //messageOut1.data[5] = 0x97; |
kendunlop | 1:19d183cf2689 | 140 | //messageOut1.data[6] = 0x67; |
kendunlop | 1:19d183cf2689 | 141 | //messageOut1.data[7] = 0x37; |
kendunlop | 1:19d183cf2689 | 142 | } |
kendunlop | 1:19d183cf2689 | 143 | |
kendunlop | 1:19d183cf2689 | 144 | void printMessageOut (void){ |
kendunlop | 1:19d183cf2689 | 145 | //This function will print out whatever the CAN bus is sending out. Can't be used constantly as it sends '000 8 00 00 00 00 00 00 00 00' all the time. |
kendunlop | 1:19d183cf2689 | 146 | pc.printf("Message OUT: %03X %01X %02X %02X %02X %02X %02X %02X %02X %02X\r\n",messageOut1.id,messageOut1.len,messageOut1.data[0],messageOut1.data[1],messageOut1.data[2],messageOut1.data[3],messageOut1.data[4],messageOut1.data[5],messageOut1.data[6],messageOut1.data[7]); |
kendunlop | 1:19d183cf2689 | 147 | } |
kendunlop | 1:19d183cf2689 | 148 | |
kendunlop | 1:19d183cf2689 | 149 | void printMessageIn (void){ |
kendunlop | 1:19d183cf2689 | 150 | //This function will print out whatever the CAN bus is receiving. |
kendunlop | 1:19d183cf2689 | 151 | pc.printf("Message IN: %03X %01X %02X %02X %02X %02X %02X %02X %02X %02X\r\n",messageIn.id,messageIn.len,messageIn.data[0],messageIn.data[1],messageIn.data[2],messageIn.data[3],messageIn.data[4],messageIn.data[5],messageIn.data[6],messageIn.data[7]); |
kendunlop | 1:19d183cf2689 | 152 | } |
kendunlop | 0:7a500ebaa7a6 | 153 | |
kendunlop | 0:7a500ebaa7a6 | 154 | //The 'main' function will run as soon as the program starts. |
kendunlop | 0:7a500ebaa7a6 | 155 | int main() |
kendunlop | 1:19d183cf2689 | 156 | { |
kendunlop | 1:19d183cf2689 | 157 | pc.baud(115200); // serial port at 115200 |
kendunlop | 1:19d183cf2689 | 158 | CanBus.frequency(500 * 1000); // CAN bus at 500k |
kendunlop | 1:19d183cf2689 | 159 | CanBus.reset(); // clear any bus errors |
kendunlop | 1:19d183cf2689 | 160 | //NOTE: Print messages must be below this line to work. |
kendunlop | 1:19d183cf2689 | 161 | pc.printf("------------------------------------------\r\n"); |
kendunlop | 1:19d183cf2689 | 162 | pc.printf("Welcome to Ken CAN test.\r\n"); |
kendunlop | 1:19d183cf2689 | 163 | pc.printf("Setting CAN bus to 500k.\r\n"); |
kendunlop | 1:19d183cf2689 | 164 | pc.printf("Setting serial port to 115200.\r\n"); |
kendunlop | 1:19d183cf2689 | 165 | pc.printf("Using pins 9 and 10.\r\n"); |
kendunlop | 1:19d183cf2689 | 166 | pc.printf("Version %d.%d\r\n",kMajorVersion,kMinorVersion); |
kendunlop | 1:19d183cf2689 | 167 | pc.printf("Build date %s %s\r\n",__DATE__,__TIME__); |
kendunlop | 1:19d183cf2689 | 168 | pc.printf("------------------------------------------\r\n"); |
kendunlop | 1:19d183cf2689 | 169 | char c; |
kendunlop | 1:19d183cf2689 | 170 | |
kendunlop | 1:19d183cf2689 | 171 | //Check for button presses |
kendunlop | 1:19d183cf2689 | 172 | while (1) |
kendunlop | 0:7a500ebaa7a6 | 173 | { |
kendunlop | 1:19d183cf2689 | 174 | if (pc.readable()) |
kendunlop | 1:19d183cf2689 | 175 | { |
kendunlop | 1:19d183cf2689 | 176 | c = pc.getc(); |
kendunlop | 1:19d183cf2689 | 177 | if ((c != NULL)) |
kendunlop | 1:19d183cf2689 | 178 | { |
kendunlop | 1:19d183cf2689 | 179 | // When the a key is pressed, define a CAN message and send it. |
kendunlop | 1:19d183cf2689 | 180 | //pc.printf("A key was pressed! (%c)\r\n", c); |
kendunlop | 1:19d183cf2689 | 181 | //pc.printf("Will try to send CAN...\r\n"); |
kendunlop | 1:19d183cf2689 | 182 | //defineCANmessage(); |
kendunlop | 1:19d183cf2689 | 183 | messageOutText = ""; |
kendunlop | 1:19d183cf2689 | 184 | if (c == '1') |
kendunlop | 1:19d183cf2689 | 185 | {messageOutText = "301 8 01 01 01 01 01 01 01 01";} |
kendunlop | 1:19d183cf2689 | 186 | if (c == '2') |
kendunlop | 1:19d183cf2689 | 187 | {messageOutText = "302 8 02 02 02 02 02 02 02 02";} |
kendunlop | 1:19d183cf2689 | 188 | if (c == '3') |
kendunlop | 2:11339018dda6 | 189 | {messageOutText = "333 8 00 00 00 00 00 00 00 33";} |
kendunlop | 1:19d183cf2689 | 190 | if (c == '4') |
kendunlop | 1:19d183cf2689 | 191 | {messageOutText = "304 8 04 04 04 04 04 04 04 04";} |
kendunlop | 1:19d183cf2689 | 192 | if (c == '5') |
kendunlop | 1:19d183cf2689 | 193 | {messageOutText = "305 8 05 05 05 05 05 05 05 05";} |
kendunlop | 1:19d183cf2689 | 194 | if (c == '6') |
kendunlop | 1:19d183cf2689 | 195 | {messageOutText = "306 8 06 06 06 06 06 06 06 06";} |
kendunlop | 1:19d183cf2689 | 196 | if (c == '7') |
kendunlop | 1:19d183cf2689 | 197 | {messageOutText = "307 8 07 07 07 07 07 07 07 07";} |
kendunlop | 1:19d183cf2689 | 198 | if (c == '8') |
kendunlop | 1:19d183cf2689 | 199 | {messageOutText = "308 8 08 08 08 08 08 08 08 08";} |
kendunlop | 1:19d183cf2689 | 200 | if (c == '9') |
kendunlop | 1:19d183cf2689 | 201 | {messageOutText = "309 8 09 09 09 09 09 09 09 09";} |
kendunlop | 1:19d183cf2689 | 202 | if (c == 'f') |
kendunlop | 1:19d183cf2689 | 203 | {messageOutText = "FFF 8 FF FF FF FF FF FF FF FF";} |
kendunlop | 1:19d183cf2689 | 204 | if (c == 'q') |
kendunlop | 1:19d183cf2689 | 205 | {pc.printf("Changing CAN bus speed to 125.\r\n"); |
kendunlop | 1:19d183cf2689 | 206 | CanBus.frequency(125 * 1000);} // CAN bus at 125k |
kendunlop | 1:19d183cf2689 | 207 | if (c == 'w') |
kendunlop | 1:19d183cf2689 | 208 | {pc.printf("Changing CAN bus speed to 250.\r\n"); |
kendunlop | 1:19d183cf2689 | 209 | CanBus.frequency(250 * 1000);} // CAN bus at 250k |
kendunlop | 1:19d183cf2689 | 210 | if (c == 'e') |
kendunlop | 1:19d183cf2689 | 211 | {pc.printf("Changing CAN bus speed to 500.\r\n"); |
kendunlop | 1:19d183cf2689 | 212 | CanBus.frequency(500 * 1000);} // CAN bus at 500k |
kendunlop | 1:19d183cf2689 | 213 | if (c == 'r') |
kendunlop | 1:19d183cf2689 | 214 | {pc.printf("Changing CAN bus speed to 1000.\r\n"); |
kendunlop | 1:19d183cf2689 | 215 | CanBus.frequency(1000 * 1000);} // CAN bus at 1000k |
kendunlop | 1:19d183cf2689 | 216 | if (c == 's') |
kendunlop | 1:19d183cf2689 | 217 | { |
kendunlop | 1:19d183cf2689 | 218 | pc.printf("Sending authentic sats message.\r\n"); |
kendunlop | 1:19d183cf2689 | 219 | messageOutText = "301 8 06 3F B2 29 12 97 67 37"; |
kendunlop | 1:19d183cf2689 | 220 | } |
kendunlop | 1:19d183cf2689 | 221 | if (c == 'i') |
kendunlop | 1:19d183cf2689 | 222 | { |
kendunlop | 1:19d183cf2689 | 223 | incrementer = incrementer + 16; |
kendunlop | 1:19d183cf2689 | 224 | std::stringstream sstream; |
kendunlop | 1:19d183cf2689 | 225 | sstream << std::hex << incrementer; |
kendunlop | 1:19d183cf2689 | 226 | string stringsofar = sstream.str(); |
kendunlop | 1:19d183cf2689 | 227 | //pc.printf("Incrementer is now %d.\r\n", incrementer); |
kendunlop | 1:19d183cf2689 | 228 | //pc.printf("StringStream says '%s'.\r\n", stringsofar); |
kendunlop | 1:19d183cf2689 | 229 | int length = stringsofar.length(); |
kendunlop | 1:19d183cf2689 | 230 | //pc.printf("Length is %d/16.\r\n", length); |
kendunlop | 1:19d183cf2689 | 231 | for (int i = 0; i < (16-length); i++) |
kendunlop | 1:19d183cf2689 | 232 | stringsofar = "0" + stringsofar; |
kendunlop | 1:19d183cf2689 | 233 | //pc.printf("stringsofar says '%s'.\r\n", stringsofar); |
kendunlop | 1:19d183cf2689 | 234 | messageOutText = "305 8 " + stringsofar; |
kendunlop | 1:19d183cf2689 | 235 | //pc.printf("Will try to send '%s'.\r\n", messageOutText); |
kendunlop | 1:19d183cf2689 | 236 | } |
kendunlop | 1:19d183cf2689 | 237 | messageOut1.format = CANStandard; |
kendunlop | 1:19d183cf2689 | 238 | if (c == 'z' and spamon == 0) |
kendunlop | 1:19d183cf2689 | 239 | { |
kendunlop | 1:19d183cf2689 | 240 | pc.printf("Starting spam sequence...\r\n"); |
kendunlop | 1:19d183cf2689 | 241 | spamon = 1; |
kendunlop | 1:19d183cf2689 | 242 | incrementer = 0; |
kendunlop | 2:11339018dda6 | 243 | spamcount = 0; |
kendunlop | 2:11339018dda6 | 244 | int spamstarttime = clock(); |
kendunlop | 1:19d183cf2689 | 245 | } |
kendunlop | 1:19d183cf2689 | 246 | //Pressing 'x' switches off spam mode |
kendunlop | 1:19d183cf2689 | 247 | if (c == 'x' and spamon == 1) |
kendunlop | 1:19d183cf2689 | 248 | { |
kendunlop | 1:19d183cf2689 | 249 | spamon = 0; |
kendunlop | 2:11339018dda6 | 250 | pc.printf("Ending spam mode. Spammed %d times.\r\n", spamcount); |
kendunlop | 2:11339018dda6 | 251 | int spamendtime = clock(); |
kendunlop | 2:11339018dda6 | 252 | int spamtime = 0; |
kendunlop | 2:11339018dda6 | 253 | spamtime = spamendtime - spamstarttime; |
kendunlop | 2:11339018dda6 | 254 | pc.printf("Clock right now is %d.\r\n", (spamtime)); |
kendunlop | 2:11339018dda6 | 255 | int spamspersecond = 0; |
kendunlop | 2:11339018dda6 | 256 | spamspersecond = (spamcount / spamtime)/CLOCKS_PER_SEC; |
kendunlop | 2:11339018dda6 | 257 | pc.printf("Spams per second are %d.\r\n", (spamspersecond)); |
kendunlop | 1:19d183cf2689 | 258 | } |
kendunlop | 1:19d183cf2689 | 259 | if (messageOutText != "") |
kendunlop | 1:19d183cf2689 | 260 | { |
kendunlop | 1:19d183cf2689 | 261 | getCANfromstring(); |
kendunlop | 1:19d183cf2689 | 262 | CanBus.write(messageOut1); |
kendunlop | 1:19d183cf2689 | 263 | if (spamon == 0) |
kendunlop | 1:19d183cf2689 | 264 | {printMessageOut();} |
kendunlop | 1:19d183cf2689 | 265 | } |
kendunlop | 1:19d183cf2689 | 266 | } |
kendunlop | 1:19d183cf2689 | 267 | } |
kendunlop | 1:19d183cf2689 | 268 | //If spam mode is on, spam an incrementing CAN message |
kendunlop | 1:19d183cf2689 | 269 | if (spamon == 1) |
kendunlop | 1:19d183cf2689 | 270 | { |
kendunlop | 1:19d183cf2689 | 271 | spamcount ++; |
kendunlop | 1:19d183cf2689 | 272 | incrementer ++; |
kendunlop | 1:19d183cf2689 | 273 | std::stringstream sstream; |
kendunlop | 1:19d183cf2689 | 274 | sstream << std::hex << incrementer; |
kendunlop | 1:19d183cf2689 | 275 | string stringsofar = sstream.str(); |
kendunlop | 1:19d183cf2689 | 276 | int length = stringsofar.length(); |
kendunlop | 1:19d183cf2689 | 277 | for (int i = 0; i < (16-length); i++) |
kendunlop | 1:19d183cf2689 | 278 | stringsofar = "0" + stringsofar; |
kendunlop | 2:11339018dda6 | 279 | messageOutText = "333 8 " + stringsofar; |
kendunlop | 1:19d183cf2689 | 280 | getCANfromstring(); |
kendunlop | 1:19d183cf2689 | 281 | CanBus.write(messageOut1); |
kendunlop | 1:19d183cf2689 | 282 | wait(0.01); |
kendunlop | 1:19d183cf2689 | 283 | } |
kendunlop | 1:19d183cf2689 | 284 | //Check for CAN messages coming in |
kendunlop | 1:19d183cf2689 | 285 | if (CanBus.read(messageIn)) |
kendunlop | 1:19d183cf2689 | 286 | { |
kendunlop | 1:19d183cf2689 | 287 | CANCount ++; |
kendunlop | 1:19d183cf2689 | 288 | printMessageIn(); |
kendunlop | 1:19d183cf2689 | 289 | } |
kendunlop | 1:19d183cf2689 | 290 | //Spam out the message as fast as possible |
kendunlop | 1:19d183cf2689 | 291 | //while (1) |
kendunlop | 1:19d183cf2689 | 292 | //{ |
kendunlop | 1:19d183cf2689 | 293 | // defineCANmessage(); |
kendunlop | 1:19d183cf2689 | 294 | // CanBus.write(messageOut1); |
kendunlop | 1:19d183cf2689 | 295 | //} |
kendunlop | 1:19d183cf2689 | 296 | } |
kendunlop | 1:19d183cf2689 | 297 | } |