void readSettings() { int n,cc; char ch, buff[24]; char buff2[80]; unsigned int ndx=0; int read=0; if (!settingsFile.open(&root, settingsFileName, O_READ)) { console.printf("DBG: Creating New Settings File\r\n"); console.flush(); writeSettings(); return; } console.printf("DBG: Opened Settings File: %s\r\n",settingsFileName); settingsFile.seekSet(0); while ((read=settingsFile.read(buff, sizeof(buff))) > 0) { //console.printf("DBG: READ %d\r\n",read); console.flush(); for (cc=0; cc < read ; cc++) { ch=buff[cc]; //console.putChar(ch); console.flush(); buff2[ndx]=ch; if (ndx<MAX_INPUT_LINE) {ndx++;} if (ch=='\n'){//console.putChar('\r'); if(ndx) { ndx--; // eat the newline. } buff2[ndx]='\0'; //console.puts(buff2); if ((ndx>4) && buff2[3]=='!') {//fix this when the files are better buff2[3]='\0'; n=lookupIndex(buff2); switch (n) { case _NPW_: networkSetPassword(buff2+4); break; case _NJN_: Watchdog_Reset(); networkJoin(buff2 +4); break; case _PKY_: strncpy(pachubeKey, buff2+4, MAX_PATCHUBE_KEY_LENGHT-1); break; case _PFD_: strncpy(pachubeFeed, buff2+4, MAX_PATCHUBE_FEED_LENGHT-1); break; default: break; } } ndx=0; } } } console.printf("DBG: Finished with Settings File: %s\r\n",settingsFileName); settingsFile.close(); }
void HAL::syncEEPROM() { // store to disk if changed millis_t time = millis(); if (eprSyncTime && (time - eprSyncTime > 15000)) // Buffer writes only every 15 seconds to pool writes { eprSyncTime = 0; bool failed = false; if (!sd.sdactive) // not mounted { if (eepromFile.isOpen()) eepromFile.close(); Com::printErrorF("Could not write eeprom to sd card - no sd card mounted"); Com::println(); return; } if (!eepromFile.seekSet(0)) failed = true; if(!failed && !eepromFile.write(virtualEeprom, EEPROM_BYTES) == EEPROM_BYTES) failed = true; if(failed) { Com::printErrorF("Could not write eeprom to sd card"); Com::println(); } } }
// grafix has to be stored at /gfx/ int speedo_disp::sd2ssd(char filename[10],int frame){ send_command(0x15); send_command(0x00); send_command(0x7F); send_command(0x75); send_command(0x00); send_command(0x3F); SdFile root; SdFile file; SdFile subdir; root.openRoot(&pSD->volume); if(!subdir.open(&root, "gfx", O_READ)) { return 1; }; if(!file.open(&subdir, filename, O_READ)) { return 2; }; unsigned long frame_seeker=(unsigned long)frame*64*64; if(!file.seekSet(frame_seeker)) { return 3; }; // ein bild ist 64*64 Byte groß, da wir 64 lines zu je 64*2*4 Bit Breite haben uint8_t buf[65]; //int n; //while ((n = file.read(buf, sizeof(byte)*64)) > 0) { for (int zeile=0; (file.read(buf, sizeof(byte)*64)>0) && zeile<64; zeile++ ) { for(int j=0;j<64;j++){ send_char(buf[j]); }; }; file.close(); subdir.close(); root.close(); return 0; };
File SDClass::open(const char *filepath, uint8_t mode) { /* Open the supplied file path for reading or writing. The file content can be accessed via the `file` property of the `SDClass` object--this property is currently a standard `SdFile` object from `sdfatlib`. Defaults to read only. If `write` is true, default action (when `append` is true) is to append data to the end of the file. If `append` is false then the file will be truncated first. If the file does not exist and it is opened for writing the file will be created. An attempt to open a file for reading that does not exist is an error. */ int pathidx; // do the interative search SdFile parentdir = getParentDir(filepath, &pathidx); // no more subdirs! filepath += pathidx; if (! filepath[0]) { // it was the directory itself! return File(parentdir, "/"); } // Open the file itself SdFile file; // failed to open a subdir! if (!parentdir.isOpen()) return File(); if ( ! file.open(parentdir, filepath, mode)) { return File(); } // close the parent parentdir.close(); if (mode & (O_APPEND | O_WRITE)) file.seekSet(file.fileSize()); return File(file, filepath); }
idigi_callback_status_t iDigiFileSystem::app_process_file_lseek(idigi_file_lseek_request_t * const request_data, idigi_file_lseek_response_t * const response_data) { idigi_callback_status_t status = idigi_callback_continue; SdFile *file = (SdFile *) request_data->handle; uint8_t result = 0; switch(request_data->origin) { case IDIGI_SEEK_SET: result = file->seekSet(request_data->offset); break; case IDIGI_SEEK_END: if (request_data->offset != 0) { // not supported by underlying file-system implementation response_data->error->error_status = idigi_file_invalid_parameter; goto done; } result = file->seekEnd(); break; case IDIGI_SEEK_CUR: default: result = file->seekCur(request_data->offset); break; } if (result) // it's a boolean response_data->offset = request_data->offset; else response_data->offset = 0; APP_DEBUG("lseek offset %ld, origin %d returned %ld\n", request_data->offset, request_data->origin, response_data->offset); done: return status; }
void loop() { unsigned long tick = millis(); long diff = tick - lastTick; avgTickDelay = expAvg(avgTickDelay, diff); lastTick = tick; if (printTicks > printTicksI) { Serial.print("tick delay: "); Serial.print(diff); Serial.println("ms"); } // Radio tick. radio->tick(); // Send helo. diff = tick - lastSent; if ((lastAck < lastSent && diff > MAX_SEND_WAIT) || diff > MIN_SEND_WAIT ) { avgSendDelay = expAvg(avgSendDelay, diff); lastSent = tick; if (printTicks > printTicksI) { printTicksI++; Serial.print("send delay: "); Serial.print(diff); Serial.println("ms"); } if (sendInfx) { Message msg("he"); radio->send(&msg); } else { Message msg("lo"); radio->send(&msg); } sendInfx = !sendInfx; } // Update metrics. tick = millis(); if (tick - lastUpdate > UPDATE_WAIT) { memcpy(&mLast, &m, sizeof(metrics)); lastUpdate = tick; lastVcc = readVcc(); sinceLastAck = (int) ((lastUpdate - lastAck) / 1000L); if (sinceLastAck < 0) { #ifdef DEBUG Serial.println("sinceLastAck less than 0"); Serial.print("round(("); Serial.print(lastUpdate); Serial.print(" - "); Serial.print(lastAck); Serial.print(") / 1000.0) = "); Serial.println(sinceLastAck); #endif sinceLastAck = 0; } if (lastAck + TIMEOUT_WAIT < lastSent) { lastRoundtrip = NO_READING_INT; lastRssi = NO_READING_INT; } writeLog(); } // Pipeline tick. pipe->tick(); // Serial commands. if (Serial.available()) { String cmd = Serial.readStringUntil('\n'); if (cmd[0] == 'D') { dataFile.close(); if (!dataFile.open(root, logFilename, O_READ)) { Serial.println(); Serial.println("Could not open file for reading"); return; } uint32_t offset; long cmdOffset = cmd.substring(1).toInt(); if (cmdOffset < 0) { offset = dataFile.fileSize() + cmdOffset; } else { offset = cmdOffset; } dataFile.seekSet(offset); char buf[128]; int16_t read; bool firstRead = true; do { read = dataFile.read(buf, 127); buf[read] = 0; if (firstRead && read > 0) { firstRead = false; char *firstNewline = strchr(buf, '\n'); Serial.print(++firstNewline); } else { Serial.print(buf); } } while (read > 0); Serial.println(); dataFile.close(); } else if (cmd[0] == 'L') { printCardInfo(); } else if (cmd[0] == 'I') { Serial.print(F("Average tick delay: ")); Serial.print(avgTickDelay); Serial.println(F("ms")); Serial.print(F("Average send delay: ")); Serial.print(avgSendDelay); Serial.println(F("ms")); Serial.print(F("RAM free: ")); Serial.print(freeRam()); Serial.println(F(" bytes")); printTicks = (int) cmd.substring(1).toInt(); printTicksI = 0; } else if (cmd[0] == 'G') { Serial.print(F("Location: ")); Serial.print(m.latitude, 6); Serial.print(F(",")); Serial.println(m.longitude, 6); } else if (cmd[0] == 'U') { Message msg("up"); radio->send(&msg); } else if (cmd[0] == 'F') { if (m.logging != MODULE_ENABLED) { Serial.println(F("Requesting to enable logging")); } else { Serial.println(F("Requesting to disable logging")); } Message msg("tl"); radio->send(&msg); } else if (cmd[0] == 'T') { Serial.print(F("Remote data logging is ")); Serial.println(m.logging == MODULE_ENABLED ? "enabled" : "disabled"); } } }
int UTFT_SdRaw::pan(int dispx, int dispy, int sx, int sy, unsigned long offsetx, unsigned long offsety, unsigned long sizex, unsigned long sizey, char *filename, bool iswap) { char buffer[2*sx]; int cx, cy, cp; word temp, result; unsigned long temp1,temp2; temp1=sizex*2; temp2=offsetx*2; if (dataFile.open(filename)) { cbi(_UTFT->P_CS, _UTFT->B_CS); cx=0; cy=0; result=sx; if (_UTFT->orient==PORTRAIT) { _UTFT->setXY(dispx, dispy, dispx+sx-1, dispy+sy-1); } for(int n=0;n<sy;n++) { dataFile.seekSet(((n+offsety)*temp1)+temp2); result=dataFile.read(&buffer,2*sx); if (_UTFT->orient==PORTRAIT) { for (int i=0; i<result; i+=2) { if(iswap==1) { _UTFT->LCD_Write_DATA(buffer[i+1],buffer[i]); } else { _UTFT->LCD_Write_DATA(buffer[i],buffer[i+1]); } } } else { cp=0; while (cp<result) { if (((result-cp)/2)<(sx-cx)) { _UTFT->setXY(dispx+cx, dispy+cy, dispx+cx+((result-cp)/2)-1, dispy+cy); for (int i=(result-cp)-2; i>=0; i-=2) { if(iswap==1) { _UTFT->LCD_Write_DATA(buffer[cp+i+1],buffer[cp+i]); } else { _UTFT->LCD_Write_DATA(buffer[cp+i],buffer[cp+i+1]); } } cx+=((result-cp)/2); cp=result; } else { _UTFT->setXY(dispx+cx, dispy+cy, dispx+sx-1, dispy+cy); for (int i=sx-cx-1; i>=0; i--) { if(iswap==1) { _UTFT->LCD_Write_DATA(buffer[cp+(i*2)+1],buffer[cp+(i*2)]); } else { _UTFT->LCD_Write_DATA(buffer[cp+(i*2)],buffer[cp+(i*2)+1]); } } cp+=(sx-cx)*2; cx=0; cy++; } } } } dataFile.close(); _UTFT->setXY(0,0,_UTFT->getDisplayXSize()-1,_UTFT->getDisplayYSize()-1); sbi(_UTFT->P_CS, _UTFT->B_CS); return 0; } else { return 99; } }