uint16_t ClientAJAKumoTCP::calculateChecksum(uint8_t* packet, uint8_t len) { uint16_t retVal = 0; uint16_t sum = 0; uint8_t idx = 1; while(idx++ < len) sum += packet[idx-1]; sum = 256 - (sum & 255); retVal = toASCII(sum >> 4) << 8; retVal |= toASCII(sum & 0xF); return retVal; }
/* *Contains algorithm to turn an image into ASCII art! *Parameters: *filename: the file to write our art to. *pixAmount: the amount of pixels per character (in a square) *img: a pointer to our image in memory. * *return 0 if no errors were encountered, 1 otherwise. */ int ascii(Image* img, char* filename, int pixAmount) { // if we don't have a file in memory, say so. if(img->data == NULL) { fprintf(stderr, "Error, no file currently in memory\n"); return 1; } //first we need to pixelate pixelate(img, pixAmount); //then turn it into grayscale toGrayscale(img); //our file to write to FILE* fout= fopen(filename, "w"); // our indicator of brightness int brightness = 0; //the character we want to use to write char c = ' '; //for each "square" of our image for(int j=0; j<(img->rowNum/pixAmount); j++) { for(int i=0; i<(img->colNum/pixAmount); i++) { //our brightness value is equal to the actual brightness over 8, because we're //using 31 different characters to create our image. brightness =(int)((img->data[pixAmount*((j*img->colNum)+i)].r)/(2.34375)); //mapping brightness to character c = toASCII(brightness); //printing the character twice so we can have a square image. fprintf(fout,"%c%c",c,c); } //print a line break. fprintf(fout,"\n"); } //close our file! fclose(fout); return 0; }
void ClientAJAKumoTCP::transmitPacket(char* data) { uint8_t len = strlen(data); uint8_t *_buffer = (uint8_t*)malloc(6 + len); // Prepare packet _buffer[0] = 0x01; // SOH _buffer[1] = 'N'; // Protocol ID _buffer[2] = toASCII(_sessionID & 0xF); for(uint8_t i = 0; i < len; i++) { if(data[i] == ',') { _buffer[3+i] = 0x09; // HT } else { _buffer[3+i] = data[i]; } } uint16_t checksum = calculateChecksum(_buffer, 3+len); _buffer[3+len] = checksum >> 8; _buffer[4+len] = checksum & 0xFF; _buffer[5+len] = 0x04; // EOT if(_client.connected()) { if(_serialOutput > 1) { for(uint8_t j=0; j<6+len; j++) { Serial << _HEXPADL(_buffer[j], 2, "0") << ":"; } Serial << "\n"; } _client.write(_buffer, 6 + len); } free(_buffer); }
void toUSB_sendhex8( uint8_t value) { toUSB_send('0'); toUSB_send('x'); toUSB_send(toASCII(value >> 4)); toUSB_send(toASCII(value & 0x0f)); }
/// Read the content of the named file. The file content is read into the /// buffer in one-shot and then digested. /// /// This function determines the version of the dictionary and invokes the /// necessary reading function to perform the actual reading operations. /// Currently there are three possible version of dictioanries /// 0x02000000 - the version produced by the current write function, /// 0x01000000 - the version with 64-bit offsets, consecutive kyes, strings /// are stored in key order /// 0x00000000 - the version 32-bit offsets and stores strings in /// sorted order. /// unmarked - the version without a header, only has the bare strings in /// the code order. int ibis::dictionary::read(const char* name) { if (name == 0 || *name == 0) return -1; std::string evt = "dictionary::read"; if (ibis::gVerbose > 1) { evt += '('; evt += name; evt += ')'; } // open the file to read int ierr = 0; FILE* fptr = fopen(name, "rb"); if (fptr == 0) { LOGGER(ibis::gVerbose > 3) << "Warning -- " << evt << " failed to open the file ... " << (errno ? strerror(errno) : "no free stdio stream"); return -2; } ibis::util::timer mytimer(evt.c_str(), 4); IBIS_BLOCK_GUARD(fclose, fptr); ierr = fseek(fptr, 0, SEEK_END); // to the end if (ierr != 0) { LOGGER(ibis::gVerbose > 1) << "Warning -- " << evt << " failed to seek to the end of the file"; return -3; } uint32_t version = 0xFFFFFFFFU; long int sz = ftell(fptr); // file size if (sz > 24) { char header[20]; ierr = fseek(fptr, 0, SEEK_SET); if (ierr != 0) { LOGGER(ibis::gVerbose > 1) << "Warning -- " << evt << " failed to seek to the beginning " "of the file"; return -4; } ierr = fread(header, 1, 20, fptr); if (ierr != 20) { LOGGER(ibis::gVerbose > 1) << "Warning -- " << evt << " failed to read the 20-byte header"; return -5; } if (header[0] == _fastbit_dictionary_header[0] && header[1] == _fastbit_dictionary_header[1] && header[2] == _fastbit_dictionary_header[2] && header[3] == _fastbit_dictionary_header[3] && header[4] == _fastbit_dictionary_header[4] && header[5] == _fastbit_dictionary_header[5] && header[6] == _fastbit_dictionary_header[6] && header[7] == _fastbit_dictionary_header[7] && header[8] == _fastbit_dictionary_header[8] && header[9] == _fastbit_dictionary_header[9] && header[10] == _fastbit_dictionary_header[10] && header[11] == _fastbit_dictionary_header[11] && header[12] == _fastbit_dictionary_header[12] && header[13] == _fastbit_dictionary_header[13] && header[14] == _fastbit_dictionary_header[14] && header[15] == _fastbit_dictionary_header[15]) { version = (header[16] << 24 | header[17] << 16 | header[18] << 8 | header[19]); LOGGER(ibis::gVerbose > 3) << evt << " detected dictionary version 0x" << std::hex << version << std::dec; } else { LOGGER(ibis::gVerbose > 2) << evt << " did not find the expected header, assume " "to have no header (oldest version of dictioinary)"; } } // invoke the actual reader based on version number switch (version) { case 0x02000000: ierr = readKeys2(evt.c_str(), fptr); break; case 0x01000000: ierr = readKeys1(evt.c_str(), fptr); break; case 0x00000000: ierr = readKeys0(evt.c_str(), fptr); break; default: ierr = readRaw(evt.c_str(), fptr); break; } if (ibis::gVerbose > 3) { ibis::util::logger lg; lg() << evt << " completed with "; toASCII(lg()); } return ierr; } // ibis::dictionary::read
void send_serial_hex_USB( uint8_t value) { send_serial_USB('0'); send_serial_USB('x'); send_serial_USB(toASCII(value >> 4)); send_serial_USB(toASCII(value & 0x0f)); }