/** * Initialisierung mit den Pins fuer Serial-Data, Serial-Clock und Store-Clock (Latch) */ ShiftRegister::ShiftRegister(byte dataPin, byte clockPin, byte latchPin) { #ifdef SHIFTREGISTER_TURBO DEBUG_PRINTLN(F("ShiftRegister is in TURBO-MODE.")); DEBUG_FLUSH(); #else DEBUG_PRINTLN(F("ShiftRegister is in SLOW-MODE.")); DEBUG_FLUSH(); #endif // slow version _dataPin = dataPin; _clockPin = clockPin; _latchPin = latchPin; pinMode(_latchPin, OUTPUT); pinMode(_clockPin, OUTPUT); pinMode(_dataPin, OUTPUT); // fast version _dataBit = digitalPinToBitMask(dataPin); _dataPort = digitalPinToPort(dataPin); _dataOut = portOutputRegister(_dataPort); _clockBit = digitalPinToBitMask(clockPin); _clockPort = digitalPinToPort(clockPin); _clockOut = portOutputRegister(_clockPort); _latchBit = digitalPinToBitMask(latchPin); _latchPort = digitalPinToPort(latchPin); _latchOut = portOutputRegister(_latchPort); }
/** * Die Uhrzeit auslesen und in den Variablen ablegen */ void MyRTC::readTime() { byte returnStatus, count, result, retries = 0; do { // Reset the register pointer Wire.beginTransmission(_address); Wire.write((uint8_t) 0x00); result = Wire.endTransmission(false); // false, damit der Bus nicht freigegeben wird und eventuell andere dazwischen kommen (in Multi-MCU-Umgebungen) DEBUG_PRINT(F("Wire.endTransmission(false) = ")); DEBUG_PRINTLN(result); count = Wire.requestFrom(_address, 7); DEBUG_PRINT(F("Wire.requestFrom(_address, 7) = ")); DEBUG_PRINTLN(count); DEBUG_FLUSH(); if (count == 7) { // Success // A few of these need masks because certain bits are control bits _seconds = bcdToDec(Wire.read() & 0x7f); _minutes = bcdToDec(Wire.read()); _hours = bcdToDec(Wire.read() & 0x3f); // Need to change this if 12 hour am/pm _dayOfWeek = bcdToDec(Wire.read()); _date = bcdToDec(Wire.read()); _month = bcdToDec(Wire.read()); _year = bcdToDec(Wire.read()); } else { // Fail // keine 7 Byte zurueck gekommen? Buffer verwerfen... for (int i = 0; i < count; i++) { Wire.read(); } retries++; } result = Wire.endTransmission(true); // true, jetzt den Bus freigeben. DEBUG_PRINT(F("Wire.endTransmission(true) = ")); DEBUG_PRINTLN(result); } while ((count != 7) && (retries < 8)); if (retries == 8) { // Es konnte nichts gelesen werden _seconds = 11; _minutes = 11; _hours = 11; _dayOfWeek = 1; _date = 1; _month = 1; _year = 2014; } DEBUG_PRINT(F("Time: ")); DEBUG_PRINT(getHours()); DEBUG_PRINT(F(":")); DEBUG_PRINT(getMinutes()); DEBUG_PRINT(F(":")); DEBUG_PRINTLN(getSeconds()); DEBUG_FLUSH(); }
void API2::Test2::Context::onMarketDataEvent(UNSIGNED_LONG symbolId) { API2::DATA_TYPES::RiskStatus risk; // DEBUG_MESSAGE(reqQryDebugLog(), __PRETTY_FUNCTION__ ); // DEBUG_FLUSH(reqQryDebugLog()); // reqQryMarketData(symbolId)->dump(); // return; if(_orderWrapper._isReset) { DEBUG_MESSAGE(reqQryDebugLog(), "Sending New Order"); if(!_orderWrapper.newOrder(risk,getPrice(),_userParams._qty)) { DEBUG_VARSHOW(reqQryDebugLog(), "Failed Sending New Order, Reason:", risk); DEBUG_FLUSH(reqQryDebugLog()); reqAddStrategyComment(API2::CONSTANTS::RSP_StrategyComment_RMS_FAILURE); reqTerminateStrategy(); } } else { switch(reqQryOrderStatus(_orderWrapper._orderId)) { case API2::CONSTANTS::RSP_OrderStatus_PENDING: return; case API2::CONSTANTS::RSP_OrderStatus_FILLED: case API2::CONSTANTS::RSP_OrderStatus_CANCELED: _orderWrapper.reset(); onMarketDataEvent(symbolId); return; case API2::CONSTANTS::RSP_OrderStatus_CONFIRMED: case API2::CONSTANTS::RSP_OrderStatus_PARTIALLY_FILLED: case API2::CONSTANTS::RSP_OrderStatus_REPLACED: if(_orderWrapper.replaceOrder(risk,getPrice(),_userParams._qty)){ DEBUG_MESSAGE(reqQryDebugLog(), "Sent Replace Order"); } else{ DEBUG_VARSHOW(reqQryDebugLog(), "Failed Replace Order, reason",risk); } } } DEBUG_FLUSH(reqQryDebugLog()); }
/** * Die Helligkeit des Displays anpassen. * * @param brightnessInPercent Die Helligkeit. */ void LedDriverUeberPixel::setBrightness(unsigned int brightnessInPercent) { if(_brightness != brightnessInPercent) { _brightness = brightnessInPercent; DEBUG_PRINT(F("MAX7219: Brightness: ")); DEBUG_PRINTLN(_brightness); DEBUG_FLUSH(); byte val = map(_brightness, 0, 100, 1, 15); DEBUG_PRINT(F(" val: ")); DEBUG_PRINTLN(val); DEBUG_FLUSH(); for(byte i = 0; i < 4; i++) { _ledControl->setIntensity(i, val); } } }
bool OrderWrapper::processConfirmation(OrderConfirmation &confirmation) { LegDetail *legOrder = getLegOrder(confirmation.getSymbolId()); if(!legOrder) { DEBUG_VARSHOW(_context->reqQryDebugLog(),"leg Order Found Null SymbolID:",confirmation.getSymbolId()); DEBUG_FLUSH(_context->reqQryDebugLog()); } if(confirmation.getOrderStatus() == API2::CONSTANTS::RSP_OrderStatus_CONFIRMED) _exchangeOrderId = _context->reqQryExchangeOrderId(legOrder->orderId); switch(confirmation.getOrderStatus()) { case API2::CONSTANTS::RSP_OrderStatus_CONFIRMED: legOrder->updateOrderWrapper(confirmation.getOrderQuantity(),confirmation.getOrderPrice(),0); case API2::CONSTANTS::RSP_OrderStatus_NEW_REJECTED: _isPendingNew = false;break; case API2::CONSTANTS::RSP_OrderStatus_REPLACED: legOrder->updateOrderWrapper(confirmation.getOrderQuantity(),confirmation.getOrderPrice(),0); case API2::CONSTANTS::RSP_OrderStatus_REPLACE_REJECTED: _isPendingReplace = false;break; case API2::CONSTANTS::RSP_OrderStatus_CANCELED: case API2::CONSTANTS::RSP_OrderStatus_CANCELED_OF_IOC: legOrder->resetOrderWrapper(); case API2::CONSTANTS::RSP_OrderStatus_CANCEL_REJECTED: _isPendingCancel = false;break; case API2::CONSTANTS::RSP_OrderStatus_FILLED: if(confirmation.getLastFillQuantity() + getLastFilledQuantity() != legOrder->_lastQuantity)//PartiallyFILLed { legOrder->updateOrderWrapper(getLastQuantity(),legOrder->_lastQuotedPrice,confirmation.getLastFillQuantity()); break; } legOrder->resetOrderWrapper(); break; } if(strcmp(confirmation.getExchangeOrderId().c_str(),_exchangeOrderId.c_str())) { DEBUG_MESSAGE(_context->reqQryDebugLog(),"Mismatch in order id"); DEBUG_VARSHOW(_context->reqQryDebugLog(),"confirmation ExchangeOrderId",confirmation.getExchangeOrderId().c_str()); DEBUG_VARSHOW(_context->reqQryDebugLog(),"_exchangeOrderId",_exchangeOrderId.c_str()); return false; } return true; }
bool API2::Test2::Context::setInternalParameters(API2::UserParams *params) { #ifndef TESTING if(params->getValue("SYMBOL LEG1",_userParams._symbolId) != API2::UserParamsError_OK) { DEBUG_MESSAGE(reqQryDebugLog(), "Issue in SYMBOL LEG1"); return false; } if(params->getValue("Order Mode 1",_userParams._side) != API2::UserParamsError_OK) { DEBUG_MESSAGE(reqQryDebugLog(), "Order Mode 1"); return false; } if(params->getValue("Price Type",_userParams._priceType) != API2::UserParamsError_OK) { DEBUG_MESSAGE(reqQryDebugLog(), "issue in Price Type"); return false; } if(params->getValue("Order Qty",_userParams._qty) != API2::UserParamsError_OK) { DEBUG_MESSAGE(reqQryDebugLog(), "issue in Qty"); return false; } #else setTestParameters(); #endif std::stringstream ss; _userParams.dump(ss, "Frontend Params:"); DEBUG_MESSAGE(reqQryDebugLog(),ss.str().c_str()); try{ _Instrument = createNewInstrument(_userParams._symbolId,true,true); _orderWrapper = API2::COMMON::OrderWrapper(_Instrument,API2::DATA_TYPES::OrderMode(_userParams._side),API2::CONSTANTS::CMD_OrderType_LIMIT, this); return true; } catch(API2::MarketDataSubscriptionFailedException e) { DEBUG_VARSHOW(reqQryDebugLog(), "Exception occured", e.what()); return false; } DEBUG_FLUSH(reqQryDebugLog()); }
TestContext::TestContext(API2::StrategyParameters *params): API2::SGContext(params,"Test_Strategy"), _riskStatus(API2::CONSTANTS::RSP_RiskStatus_MAX), _buyOrder(API2::CONSTANTS::CMD_OrderMode_BUY), _sellOrder(API2::CONSTANTS::CMD_OrderMode_SELL), _modify(false), _terminate(false), _isTbtEnabled(false), _buyTotalTradedLots(0), _sellTotalTradedLots(0) { /** * convert api paramaters into your own data structure */ API2::UserParams* customParams = (API2::UserParams*) params->getInfo(); if(!setInternalParameters(customParams)) { DEBUG_MESSAGE(reqQryDebugLog(), "Front End Parameters Failed"); terminateStrategyComment(API2::CONSTANTS::RSP_StrategyComment_STRATEGY_ERROR_STATE); return; } /* * * register symbolids * */ try { registerSymbols(); } catch(API2::MarketDataSubscriptionFailedException &e) { std::cout<<e.what()<< std::endl; DEBUG_MESSAGE(reqQryDebugLog(), "TBT Subscription Failed"); terminateStrategyComment(API2::CONSTANTS::RSP_StrategyComment_TBT_SUBSCRIPTION_FAILED); return; } catch(API2::InstrumentNotFoundException& e) { std::cout<<e.what() <<std::endl; std::cout << "TBT Subscription Failed, terminating strategy\n"; DEBUG_MESSAGE(reqQryDebugLog(), "TBT Subscription Failed"); terminateStrategyComment(API2::CONSTANTS::RSP_StrategyComment_TBT_SUBSCRIPTION_FAILED); return; } catch(std::exception &e) { std::cout<<e.what() <<std::endl; std::cout << "TBT Subscription Failed, terminating strategy\n"; DEBUG_MESSAGE(reqQryDebugLog(), "TBT Subscription Failed"); terminateStrategyComment(API2::CONSTANTS::RSP_StrategyComment_TBT_SUBSCRIPTION_FAILED); return; } if(_terminate) return; setOrderWrapper(); dump(); reqTimerEvent(10000); DEBUG_FLUSH(reqQryDebugLog()); }
/** * Decodierung des Telegramms... */ boolean MyDCF77::decode() { int c = 0; // bitcount for checkbit boolean ok = true; DEBUG_PRINTLN(F("Decoding telegram...")); DEBUG_FLUSH(); if (_bits[20] != 1) { ok = false; DEBUG_PRINTLN(F("Check-bit S failed.")); DEBUG_FLUSH(); } if (_bits[17] == _bits[18]) { ok = false; DEBUG_PRINTLN(F("Check Z1 != Z2 failed.")); DEBUG_FLUSH(); } // // minutes // _minutes = 0; c = 0; if (_bits[21] == 1) { c++; _minutes += _bits[21] * 1; } if (_bits[22] == 1) { c++; _minutes += _bits[22] * 2; } if (_bits[23] == 1) { c++; _minutes += _bits[23] * 4; } if (_bits[24] == 1) { c++; _minutes += _bits[24] * 8; } if (_bits[25] == 1) { c++; _minutes += _bits[25] * 10; } if (_bits[26]) { c++; _minutes += _bits[26] * 20; } if (_bits[27]) { c++; _minutes += _bits[27] * 40; } DEBUG_PRINT(F("Minutes: ")); DEBUG_PRINTLN(_minutes); DEBUG_FLUSH(); if ((c + _bits[28]) % 2 != 0) { ok = false; DEBUG_PRINTLN(F("Check-bit P1: minutes failed.")); DEBUG_FLUSH(); } // // hour // _hours = 0; c = 0; if (_bits[29] == 1) { c++; _hours += _bits[29] * 1; } if (_bits[30] == 1) { c++; _hours += _bits[30] * 2; } if (_bits[31] == 1) { c++; _hours += _bits[31] * 4; } if (_bits[32] == 1) { c++; _hours += _bits[32] * 8; } if (_bits[33] == 1) { c++; _hours += _bits[33] * 10; } if (_bits[34] == 1) { c++; _hours += _bits[34] * 20; } DEBUG_PRINT(F("Hours: ")); DEBUG_PRINTLN(_hours); DEBUG_FLUSH(); if ((c + _bits[35]) % 2 != 0) { ok = false; DEBUG_PRINTLN(F("Check-bit P2: hours failed.")); DEBUG_FLUSH(); } // // date // _date = 0; c = 0; if (_bits[36] == 1) { c++; _date += _bits[36] * 1; } if (_bits[37] == 1) { c++; _date += _bits[37] * 2; } if (_bits[38] == 1) { c++; _date += _bits[38] * 4; } if (_bits[39] == 1) { c++; _date += _bits[39] * 8; } if (_bits[40] == 1) { c++; _date += _bits[40] * 10; } if (_bits[41] == 1) { c++; _date += _bits[41] * 20; } DEBUG_PRINT(F("Date: ")); DEBUG_PRINTLN(_date); DEBUG_FLUSH(); // // day of week // _dayOfWeek = 0; if (_bits[42] == 1) { c++; _dayOfWeek += _bits[42] * 1; } if (_bits[43] == 1) { c++; _dayOfWeek += _bits[43] * 2; } if (_bits[44] == 1) { c++; _dayOfWeek += _bits[44] * 4; } DEBUG_PRINT(F("Day of week: ")); DEBUG_PRINTLN(_dayOfWeek); DEBUG_FLUSH(); // // month // _month = 0; if (_bits[45] == 1) { c++; _month += _bits[45] * 1; } if (_bits[46] == 1) { c++; _month += _bits[46] * 2; } if (_bits[47] == 1) { c++; _month += _bits[47] * 4; } if (_bits[48] == 1) { c++; _month += _bits[48] * 8; } if (_bits[49] == 1) { c++; _month += _bits[49] * 10; } DEBUG_PRINT(F("Month: ")); DEBUG_PRINTLN(_month); DEBUG_FLUSH(); // // year // _year = 0; if (_bits[50] == 1) { c++; _year += _bits[50] * 1; } if (_bits[51] == 1) { c++; _year += _bits[51] * 2; } if (_bits[52] == 1) { c++; _year += _bits[52] * 4; } if (_bits[53] == 1) { c++; _year += _bits[53] * 8; } if (_bits[54] == 1) { c++; _year += _bits[54] * 10; } if (_bits[55] == 1) { c++; _year += _bits[55] * 20; } if (_bits[56] == 1) { c++; _year += _bits[56] * 40; } if (_bits[57] == 1) { c++; _year += _bits[57] * 80; } DEBUG_PRINT(F("Year: ")); DEBUG_PRINTLN(_year); DEBUG_FLUSH(); if ((c + _bits[58]) % 2 != 0) { ok = false; DEBUG_PRINTLN(F("Check-bit P3: date failed.")); DEBUG_FLUSH(); } if (!ok) { // discard date... _minutes = 0; _hours = 0; _date = 0; _dayOfWeek = 0; _month = 0; _year = 0; } return ok; }
/** * Welchen Wert hat der LDR? */ unsigned int LDR::value() { unsigned int val = analogRead(_pin); if(val != _lastValue) { _lastValue = val; //Autoscale defines the min and max value to determine the boundaries of the Brightness #ifdef LDR_AUTOSCALE if(val < _min) { _min = val; } if(val > _max) { _max = val; } #else val = constrain(val, _min, _max); #endif unsigned int mapVal = map(val, _min, _max, 100, 0); DEBUG_PRINT(F(" _min: ")); DEBUG_PRINT(_min); DEBUG_PRINT(F(" _max: ")); DEBUG_PRINT(_max); DEBUG_PRINT(F(" ldr: ")); DEBUG_PRINT(val); DEBUG_PRINT(F(" mapValue: ")); DEBUG_PRINTLN(mapVal); DEBUG_FLUSH(); // glaetten if (_BufferFull != true) { // buffer is not yet full if (_meanpointer == 0) { _outputValue == mapVal; _TotalMeanValues == mapVal; } else { // store value in buffer position _meanvalues[_meanpointer] = mapVal; // calculate running average _TotalMeanValues == _TotalMeanValues + mapVal; _outputValue = (unsigned int)(_TotalMeanValues)/(_meanpointer+1); } } //endif _meanpointer == 0 else { // buffer is full // First remove the 'old' value from the buffer and substract this from the TotalMeanValues _TotalMeanValues == _TotalMeanValues - _meanvalues[_meanpointer]; // now add MapVal _TotalMeanValues == (_TotalMeanValues + mapVal); // store new value in buffer position _meanvalues[_meanpointer] = mapVal; // Calculate average _outputValue = (unsigned int)(_TotalMeanValues)/LDR_MEAN_COUNT; } _meanpointer++; // increase meanpointer if(_meanpointer == LDR_MEAN_COUNT) { _meanpointer = 0; // reset meanpointer if array index length is exceeded _BufferFull = true; // This only happens once because _Bufferfull is only false at initialisation. When the whole buffer is filled with data, the buffer becomes a circular buffer } return _outputValue; } }