void EepromManager::initializeEeprom() { // clear all eeprom for (uint16_t offset=0; offset<EepromFormat::MAX_EEPROM_SIZE; offset++) eepromAccess.writeByte(offset, 0); deviceManager.setupUnconfiguredDevices(); // fetch the default values tempControl.loadDefaultConstants(); tempControl.loadDefaultSettings(); // write the default constants for (uint8_t c=0; c<EepromFormat::MAX_CHAMBERS; c++) { eptr_t pv = pointerOffset(chambers)+(c*sizeof(ChamberBlock)) ; tempControl.storeConstants(pv+offsetof(ChamberBlock, chamberSettings.cc)); pv += offsetof(ChamberBlock, beer)+offsetof(BeerBlock, cs); for (uint8_t b=0; b<ChamberBlock::MAX_BEERS; b++) { // logDeveloper(PSTR("EepromManager - saving settings for beer %d at %d"), b, (uint16_t)pv); tempControl.storeSettings(pv); pv += sizeof(BeerBlock); // advance to next beer } } // set the version flag - so that storeDevice will work eepromAccess.writeByte(0, EEPROM_FORMAT_VERSION); saveDefaultDevices(); // set state to startup tempControl.init(); }
bool EepromManager::storeProfile(const ProfileConfig& config) { bool ok = hasSettings(); if (ok) eepromAccess.writeBlock(pointerOffset(profile), &config, sizeof(ProfileConfig)); return ok; }
bool EepromManager::storeDevice(const DeviceConfig& config, uint8_t deviceIndex) { bool ok = (hasSettings() && deviceIndex<EepromFormat::MAX_DEVICES); if (ok) eepromAccess.writeBlock(pointerOffset(devices)+sizeof(DeviceConfig)*deviceIndex, &config, sizeof(DeviceConfig)); return ok; }
bool EepromManager::fetchProfile(ProfileConfig& config) { bool ok = hasSettings(); if (ok) eepromAccess.readBlock(&config, pointerOffset(profile), sizeof(ProfileConfig)); return ok; }
bool EepromManager::fetchDevice(DeviceConfig& config, uint8_t deviceIndex) { bool ok = (hasSettings() && deviceIndex<EepromFormat::MAX_DEVICES); if (ok) eepromAccess.readBlock(&config, pointerOffset(devices)+sizeof(DeviceConfig)*deviceIndex, sizeof(DeviceConfig)); return ok; }
bool EepromManager::applySettings() { if (!hasSettings()) return false; // start from a clean state deviceManager.setupUnconfiguredDevices(); logDebug("Applying settings"); // load the one chamber and one beer for now eptr_t pv = pointerOffset(chambers); tempControl.loadConstants(pv+offsetof(ChamberBlock, chamberSettings.cc)); tempControl.loadSettings(pv+offsetof(ChamberBlock, beer[0].cs)); logDebug("Applied settings"); #if BREWPI_TEMP_PROFILE profileControl.loadProfile(); #endif //BREWPI_TEMP_PROFILE DeviceConfig deviceConfig; for (uint8_t index = 0; fetchDevice(deviceConfig, index); index++) { if (deviceManager.isDeviceValid(deviceConfig, deviceConfig, index)) deviceManager.installDevice(deviceConfig); else { clear((uint8_t*)&deviceConfig, sizeof(deviceConfig)); eepromManager.storeDevice(deviceConfig, index); } } return true; }
void EepromManager::storeTempSettings() { uint8_t chamber = 0; eptr_t pv = pointerOffset(chambers); pv += sizeof(ChamberBlock)*chamber; // for now assume just one beer. tempControl.storeSettings(pv+offsetof(ChamberBlock, beer[0].cs)); }
void EepromManager::storeTempConstantsAndSettings() { uint8_t chamber = 0; eptr_t pv = pointerOffset(chambers); pv += sizeof(ChamberBlock)*chamber; tempControl.storeConstants(pv+offsetof(ChamberBlock, chamberSettings.cc)); storeTempSettings(); }
struct sufa *sufaRead(char *fileName, boolean memoryMap) /* Read in a sufa from a file. Does this via memory mapping if you like, * which will be faster typically for about 100 reads, and slower for more * than that (_much_ slower for thousands of reads and more). */ { /* Open file (low level), read in header, and check it. */ int fd = open(fileName, O_RDONLY); if (fd < 0) errnoAbort("Can't open %s", fileName); struct sufaFileHeader h; if (read(fd, &h, sizeof(h)) < sizeof(h)) errnoAbort("Couldn't read header of file %s", fileName); if (h.magic != SUFA_MAGIC) errAbort("%s does not seem to be a sufa file.", fileName); if (h.majorVersion > SUFA_MAJOR_VERSION) errAbort("%s is a newer, incompatible version of sufa format. " "This program works on version %d and below. " "%s is version %d.", fileName, SUFA_MAJOR_VERSION, fileName, h.majorVersion); struct sufa *sufa; verbose(2, "sufa file %s size %lld\n", fileName, h.size); /* Get a pointer to data in memory, via memory map, or allocation and read. */ struct sufaFileHeader *header ; if (memoryMap) { #ifdef MACHTYPE_sparc header = (struct sufaFileHeader *)mmap(NULL, h.size, PROT_READ, MAP_SHARED, fd, 0); #else header = mmap(NULL, h.size, PROT_READ, MAP_FILE|MAP_SHARED, fd, 0); #endif if (header == (void*)(-1)) errnoAbort("Couldn't mmap %s, sorry", fileName); } else { header = needHugeMem(h.size); if (lseek(fd, 0, SEEK_SET) < 0) errnoAbort("Couldn't seek back to start of sufa file %s. " "Splix files must be random access files, not pipes and the like" , fileName); if (read(fd, header, h.size) < h.size) errnoAbort("Couldn't read all of sufa file %s.", fileName); } /* Allocate wrapper structure and fill it in. */ AllocVar(sufa); sufa->header = header; sufa->isMapped = memoryMap; /* Make an array for easy access to chromosome names. */ int chromCount = header->chromCount; char **chromNames = AllocArray(sufa->chromNames, chromCount); char *s = pointerOffset(header, sizeof(*header) ); int i; for (i=0; i<chromCount; ++i) { chromNames[i] = s; s += strlen(s)+1; } /* Keep track of where we are in memmap. */ bits64 mapOffset = sizeof(*header) + header->chromNamesSize; /* Point into chromSizes array. */ bits32 *chromSizes = sufa->chromSizes = pointerOffset(header, mapOffset); mapOffset += sizeof(bits32) * chromCount; verbose(2, "total dna size %lld in %d chromosomes\n", (long long)header->dnaDiskSize, header->chromCount); sufa->allDna = pointerOffset(header, mapOffset); mapOffset += header->dnaDiskSize; /* Calculate chromOffset array. */ bits32 offset = 0; bits32 *chromOffsets = AllocArray(sufa->chromOffsets, chromCount); for (i=0; i<chromCount; ++i) { chromOffsets[i] = offset; offset += chromSizes[i] + 1; verbose(2, "sufa contains %s, %d bases, %d offset\n", sufa->chromNames[i], (int)sufa->chromSizes[i], (int)chromOffsets[i]); } /* Finally point to the suffix array!. */ sufa->array = pointerOffset(header, mapOffset); mapOffset += header->arraySize * sizeof(bits32); assert(mapOffset == header->size); /* Sanity check */ return sufa; }
bool EepromManager::hasSettings() { uint8_t version = eepromAccess.readByte(pointerOffset(version)); return (version==EEPROM_FORMAT_VERSION); }
bool DrivingSimulatorV1::frameRenderingQueued(const Ogre::FrameEvent& evt) { // capture input devices if(inputManager) { keyboard->capture(); mouse->capture(); } // abort scene if window has been closed or escape button has been hit if(renderWindow->isClosed() || keyboard->isKeyDown(OIS::KC_ESCAPE)) return false; // decrease speed (air resistance, etc...) if(speed > 0) speed *= Ogre::Math::Pow(0.92, evt.timeSinceLastFrame); else speed *= Ogre::Math::Pow(0.7, evt.timeSinceLastFrame); // accelerate / brake car if(gear == DRIVE) { // udp input if(UdpListener::throttle >= 0) { speed += UdpListener::throttle * 9 * evt.timeSinceLastFrame; } else { speed += UdpListener::throttle * 30 * evt.timeSinceLastFrame; } // keyboard input if(keyboard->isKeyDown(OIS::KC_UP)) { speed += 9 * evt.timeSinceLastFrame; } if(keyboard->isKeyDown(OIS::KC_DOWN)) { speed -= 30 * evt.timeSinceLastFrame; } if(speed < 0) { speed = 0; } } else if(gear == REVERSE) { // udp input if(UdpListener::throttle <= 0) { speed += UdpListener::throttle * 9 * evt.timeSinceLastFrame; } else { speed += UdpListener::throttle * 30 * evt.timeSinceLastFrame; } // keyboard input if(keyboard->isKeyDown(OIS::KC_UP)) { speed += 30 * evt.timeSinceLastFrame; } if(keyboard->isKeyDown(OIS::KC_DOWN)) { speed -= 9 * evt.timeSinceLastFrame; } if(speed > 0) speed = 0; } else { if(UdpListener::throttle > THROTTLE_THRESHOLD || keyboard->isKeyDown(OIS::KC_UP)) gear = DRIVE; else if(allowReverse && UdpListener::throttle < -THROTTLE_THRESHOLD || keyboard->isKeyDown(OIS::KC_DOWN)) gear = REVERSE; } if(speed == 0 && Ogre::Math::Abs(UdpListener::throttle) < THROTTLE_THRESHOLD && !keyboard->isKeyDown(OIS::KC_UP) && !keyboard->isKeyDown(OIS::KC_DOWN)) gear = NEUTRAL; // update speed information for udp socket UdpListener::speed = Ogre::Math::Abs(speed) * 1.1; // calculate steer intensity Ogre::Real normalizedSpeed = Ogre::Math::Abs(speed / 180); Ogre::Real steerIntensity = 100 / (100 * (Ogre::Math::Pow(normalizedSpeed, 2)) + 1) * Ogre::Math::Pow(normalizedSpeed, 1.5); // rotate car by udp input carNode->yaw(Ogre::Degree(UdpListener::steer * -5 * evt.timeSinceLastFrame * speed)); cameraRotationOffset += UdpListener::steer * 90 * evt.timeSinceLastFrame; // rotate car by keyboard input if(keyboard->isKeyDown(OIS::KC_LEFT)) { keyboardSteer += 270 * evt.timeSinceLastFrame; } if(keyboard->isKeyDown(OIS::KC_RIGHT)) { keyboardSteer -= 270 * evt.timeSinceLastFrame; } if(!keyboard->isKeyDown(OIS::KC_LEFT) && !keyboard->isKeyDown(OIS::KC_RIGHT)) { if(keyboardSteer > 0) keyboardSteer -= std::min(270 * evt.timeSinceLastFrame, keyboardSteer); else if(keyboardSteer < 0) keyboardSteer += std::min(270 * evt.timeSinceLastFrame, keyboardSteer * -1); } if(keyboardSteer < -90) keyboardSteer = -90; else if(keyboardSteer > 90) keyboardSteer = 90; if(gear == DRIVE) { carNode->yaw(Ogre::Degree(steerIntensity * keyboardSteer * evt.timeSinceLastFrame)); cameraRotationOffset -= keyboardSteer * 0.3 * evt.timeSinceLastFrame * steerIntensity; } else if(gear == REVERSE) { carNode->yaw(Ogre::Degree(steerIntensity * keyboardSteer * evt.timeSinceLastFrame * -1)); cameraRotationOffset -= keyboardSteer * 0.3 * evt.timeSinceLastFrame * steerIntensity * -1; } // change camera mode if(keyboard->isKeyDown(OIS::KC_V)) { if(!keyState[OIS::KC_V]) { cameraMode = 1 - cameraMode; if(cameraMode == THIRD_PERSON) { cameraRotationOffset = 0; } } keyState[OIS::KC_V] = true; } else { keyState[OIS::KC_V] = false; } // update car position Ogre::Real xMove = Ogre::Math::Sin(carNode->getOrientation().getYaw()) * speed * evt.timeSinceLastFrame; Ogre::Real zMove = Ogre::Math::Cos(carNode->getOrientation().getYaw()) * speed * evt.timeSinceLastFrame; carNode->translate(xMove, 0, zMove); // update camera if(cameraMode == COCKPIT) { // position camera Ogre::Vector3 cameraOffset(1.3, 4.0, 0.7); camera->setOrientation(carNode->getOrientation() * Ogre::Quaternion(Ogre::Degree(180), Ogre::Vector3::UNIT_Y)); camera->setPosition(carNode->getPosition() + carNode->getOrientation() * cameraOffset); // position car cockpit Ogre::Vector3 cockpitOffset(0, 2, 0.7); cockpitNode->setPosition(carNode->getPosition() + carNode->getOrientation() * cockpitOffset); cockpitNode->setOrientation(carNode->getOrientation()); cockpitNode->yaw(Ogre::Degree(180)); // fix inaccuracy of carNode's orientation and position by assigning it it's own transformations carNode->setOrientation(carNode->getOrientation()); carNode->setPosition(carNode->getPosition()); // position speed pointer Ogre::Vector3 pointerOffset(0.0, 2.7, 2.9); Ogre::Quaternion pointerPitch(Ogre::Degree(15), Ogre::Vector3::UNIT_X); Ogre::Quaternion pointerRoll(Ogre::Degree(45 + Ogre::Math::Abs(UdpListener::speed * 2.25)), Ogre::Vector3::UNIT_Z); pointerNode->setPosition(carNode->getPosition() + carNode->getOrientation() * pointerOffset); pointerNode->setOrientation(carNode->getOrientation() * pointerPitch * pointerRoll); // position steering wheel Ogre::Vector3 steeringWheelOffset(1.3, 2.5, 2.5); Ogre::Quaternion steeringWheelPitch(Ogre::Degree(15), Ogre::Vector3::UNIT_X); Ogre::Quaternion steeringWheelRoll(Ogre::Degree(keyboardSteer * -1.5 + UdpListener::steer * 450), Ogre::Vector3::UNIT_Z); steeringWheelNode->setOrientation(carNode->getOrientation() * steeringWheelPitch * steeringWheelRoll); steeringWheelNode->setPosition(carNode->getPosition() + carNode->getOrientation() * steeringWheelOffset); carNode->setVisible(false); cockpitNode->setVisible(true); pointerNode->setVisible(true); if(showSteeringWheel) steeringWheelNode->setVisible(true); else steeringWheelNode->setVisible(false); } else if(cameraMode == THIRD_PERSON) { if(gear != REVERSE) cameraRotationOffset *= Ogre::Math::Pow(0.1, evt.timeSinceLastFrame); else cameraRotationOffset = cameraRotationOffset * Ogre::Math::Pow(0.1, evt.timeSinceLastFrame) + 180 * (1 - Ogre::Math::Pow(0.1, evt.timeSinceLastFrame)); Ogre::Radian camAngle = carNode->getOrientation().getYaw() + Ogre::Degree(cameraRotationOffset); Ogre::Real camXOffset = -Ogre::Math::Sin(camAngle) * 25; Ogre::Real camYOffset = 8; Ogre::Real camZOffset = -Ogre::Math::Cos(camAngle) * 25; camera->setPosition(carNode->getPosition() + Ogre::Vector3(camXOffset, camYOffset, camZOffset)); camera->lookAt(carNode->getPosition()); carNode->setVisible(true); cockpitNode->setVisible(false); pointerNode->setVisible(false); steeringWheelNode->setVisible(false); } // debug output //std::cout << "Position: " << carNode->getPosition() << " Rotation: " << carNode->getOrientation().getYaw().valueDegrees() << std::endl; // if we reach this position of the code, no error has occurred return true; }