/** Initialise the OLED display * */ void oledInit() { i2cConfig(); // Initialise the LCD writeBegin(); writeCommand(0xAF); // Turn display on writeEnd(); }
/** Add a data byte to the output buffer * * @param data the data byte to send */ static void writeData(uint8_t data) { // Flush the buffer if needed if(g_index==BUFFER_SIZE) writeEnd(); // Add the command value g_buffer[g_index++] = BYTE_DTA; g_buffer[g_index++] = data; }
/** Add a command byte to the output buffer * * @param cmd the command byte to send */ static void writeCommand(uint8_t cmd) { // Flush the buffer if needed if(g_index==BUFFER_SIZE) writeEnd(); // Add the command value g_buffer[g_index++] = BYTE_CMD; g_buffer[g_index++] = cmd; }
/** Clear the display */ void oledClear(bool invert) { writeBegin(); setPosition(0, 0); // Fill in the whole display for(int index = 0; index < (WIDTH * LINES); index++) writeData(invert?0xff:0x00); writeEnd(); }
void LLXmlTreeNode::writeNoChild(std::string &buffer, const std::string &indent) const { if (!mContents.empty()) { writeStart(buffer, indent); writeEnd(buffer, indent); } else { buffer += indent + '<' + mName; writeAttributes(buffer); buffer += "/>\n"; } }
/** Write a character to the display */ void oledWriteCh(int x, int y, char ch, bool invert) { // Make sure the character is valid if((ch<0x20)||(ch>0x7f)) ch = 0x20; // Do the write writeBegin(); setPosition(y, x); // Write the character writeData(invert?0xff:0x00); writeGlyph(x + 1, y, BASE_FONT + (ch - 0x20) * (FONT_WIDTH - 2), FONT_WIDTH - 2, 1, invert); writeData(invert?0xff:0x00); writeEnd(); }
/** Clear a region of the display */ void oledClearRect(int y, int height, bool invert) { // Do the clear writeBegin(); while(height) { setPosition(y, 0); // Fill the line for(int index=0; index<WIDTH; index++) writeData(invert?0xff:0x00); // Step to the next line height--; } writeEnd(); }
/** Write a string to the display */ void oledWriteStr(int x, int y, const char *str, bool invert) { writeBegin(); while (*str) { // Make sure the character is valid char ch = *str; if((ch<0x20)||(ch>0x7f)) ch = 0x20; // Set the position setPosition(y, x); // Write the character writeData(invert?0xff:0x00); writeGlyph(x + 1, y, BASE_FONT + (ch - 0x20) * (FONT_WIDTH - 2), FONT_WIDTH - 2, 1, invert); writeData(invert?0xff:0x00); // Move to the next character (and position) str++; x += FONT_WIDTH; } writeEnd(); }
boost::uint64_t Writer::write( boost::uint64_t targetNumPointsToWrite, boost::uint64_t startingPosition) { if (!isInitialized()) { throw pdal_error("stage not initialized"); } boost::uint64_t actualNumPointsWritten = 0; UserCallback* callback = getUserCallback(); do_callback(0.0, callback); const Schema& schema = getPrevStage().getSchema(); if (m_writer_buffer == 0) { boost::uint64_t capacity(targetNumPointsToWrite); if (capacity == 0) { capacity = m_chunkSize; } else { capacity = (std::min)(static_cast<boost::uint64_t>(m_chunkSize), targetNumPointsToWrite) ; } m_writer_buffer = new PointBuffer (schema, capacity); } boost::scoped_ptr<StageSequentialIterator> iter(getPrevStage().createSequentialIterator(*m_writer_buffer)); if (startingPosition) iter->skip(startingPosition); if (!iter) throw pdal_error("Unable to obtain iterator from previous stage!"); // if we don't have an SRS, try to forward the one from the prev stage if (m_spatialReference.empty()) m_spatialReference = getPrevStage().getSpatialReference(); writeBegin(targetNumPointsToWrite); iter->readBegin(); // // The user has requested a specific number of points: proceed a // chunk at a time until we reach that number. (If that number // is 0, we proceed until no more points can be read.) // // If the user requests an interrupt while we're running, we'll throw. // while (true) { // have we hit the end already? if (iter->atEnd()) break; // rebuild our PointBuffer, if it needs to hold less than the default max chunk size if (targetNumPointsToWrite != 0) { const boost::uint64_t numRemainingPointsToRead = targetNumPointsToWrite - actualNumPointsWritten; const boost::uint64_t numPointsToReadThisChunk64 = std::min<boost::uint64_t>(numRemainingPointsToRead, m_chunkSize); // this case is safe because m_chunkSize is a uint32 const boost::uint32_t numPointsToReadThisChunk = static_cast<boost::uint32_t>(numPointsToReadThisChunk64); // we are reusing the buffer, so we may need to adjust the capacity for the last (and likely undersized) chunk if (m_writer_buffer->getCapacity() < numPointsToReadThisChunk) { m_writer_buffer->resize(numPointsToReadThisChunk); } } // read... iter->readBufferBegin(*m_writer_buffer); const boost::uint32_t numPointsReadThisChunk = iter->readBuffer(*m_writer_buffer); iter->readBufferEnd(*m_writer_buffer); assert(numPointsReadThisChunk == m_writer_buffer->getNumPoints()); assert(numPointsReadThisChunk <= m_writer_buffer->getCapacity()); // have we reached the end yet? if (numPointsReadThisChunk == 0) break; // write... writeBufferBegin(*m_writer_buffer); const boost::uint32_t numPointsWrittenThisChunk = writeBuffer(*m_writer_buffer); assert(numPointsWrittenThisChunk == numPointsReadThisChunk); writeBufferEnd(*m_writer_buffer); // update count actualNumPointsWritten += numPointsWrittenThisChunk; do_callback(actualNumPointsWritten, targetNumPointsToWrite, callback); if (targetNumPointsToWrite != 0) { // have we done enough yet? if (actualNumPointsWritten >= targetNumPointsToWrite) break; } // reset the buffer, so we can use it again m_writer_buffer->setNumPoints(0); } iter->readEnd(); writeEnd(actualNumPointsWritten); assert((targetNumPointsToWrite == 0) || (actualNumPointsWritten <= targetNumPointsToWrite)); do_callback(100.0, callback); return actualNumPointsWritten; }
void RayGroup::write(int indent,FILE* fp){ writeBegin(indent,fp); for(int i=0;i<sNum;i++){shapes[i]->write(indent+2,fp);} writeEnd(indent,fp); }
/** Write an arbitrary glyph to the display */ void oledWriteGlyph(int x, int y, const uint8_t *glyph, int width, int height, bool invert) { writeBegin(); writeGlyph(x, y, glyph, width, height, invert); writeEnd(); }
// received a character from UART void GSwifi::parseByte(uint8_t dat) { static uint8_t next_token; // split each byte into tokens (cid,ip,port,length,data) static bool escape = false; char temp[GS_MAX_PATH_LENGTH+1]; if (dat == ESCAPE) { // 0x1B : Escape GSLOG_PRINT("e< "); } else { // if (next_token != NEXT_TOKEN_DATA) { GSLOG_WRITE(dat); } if (gs_mode_ == GSMODE_COMMAND) { if (escape) { // esc switch (dat) { case 'O': case 'F': // ignore break; case 'Z': case 'H': gs_mode_ = GSMODE_DATA_RX_BULK; next_token = NEXT_TOKEN_CID; break; default: // GSLOG_PRINT("!E1 "); GSLOG_PRINTLN2(dat,HEX); break; } escape = false; } else { if (dat == ESCAPE) { escape = true; } else if (dat == '\n') { // end of line parseLine(); } else if (dat != '\r') { if ( ! ring_isfull(_buf_cmd) ) { ring_put(_buf_cmd, dat); } else { GSLOG_PRINTLN("!E2"); } } } return; } else if (gs_mode_ != GSMODE_DATA_RX_BULK) { return; } static uint16_t len; static char len_chars[5]; static int8_t current_cid; static GSREQUESTSTATE request_state; if (next_token == NEXT_TOKEN_CID) { // dat is cid current_cid = x2i(dat); ASSERT((0 <= current_cid) && (current_cid <= 16)); next_token = NEXT_TOKEN_LENGTH; len = 0; } else if (next_token == NEXT_TOKEN_LENGTH) { // Data Length is 4 ascii char represents decimal value i.e. 1400 byte (0x31 0x34 0x30 0x30) len_chars[ len ++ ] = dat; if (len >= 4) { len_chars[ len ] = 0; len = atoi(len_chars); // length of data next_token = NEXT_TOKEN_DATA; if (content_lengths_[ current_cid ] > 0) { // this is our 2nd bulk message from GS for this response // we already swallowed HTTP response headers, // following should be body request_state = GSREQUESTSTATE_BODY; } else { request_state = GSREQUESTSTATE_HEAD1; } ring_clear( _buf_cmd ); // reuse _buf_cmd to store HTTP request } } else if (next_token == NEXT_TOKEN_DATA) { len --; if (cidIsRequest(current_cid)) { // request against us static uint16_t error_code; static int8_t routeid; switch (request_state) { case GSREQUESTSTATE_HEAD1: if (dat != '\n') { if ( ! ring_isfull(_buf_cmd) ) { ring_put( _buf_cmd, dat ); } // ignore overflowed } else { // end of request line // reuse "temp" buffer to parse method and path int8_t result = parseRequestLine((char*)temp, 7); GSMETHOD method = GSMETHOD_UNKNOWN; if ( result == 0 ) { method = x2method(temp); result = parseRequestLine((char*)temp, GS_MAX_PATH_LENGTH); } if ( result != 0 ) { // couldn't detect method or path request_state = GSREQUESTSTATE_ERROR; error_code = 400; ring_clear(_buf_cmd); break; } routeid = router(method, temp); if ( routeid < 0 ) { request_state = GSREQUESTSTATE_ERROR; error_code = 404; ring_clear(_buf_cmd); break; } request_state = GSREQUESTSTATE_HEAD2; continuous_newlines_ = 0; content_lengths_[ current_cid ] = 0; has_requested_with_ = false; ring_clear(_buf_cmd); } break; case GSREQUESTSTATE_HEAD2: if(0 == parseHead2(dat, current_cid)) { request_state = GSREQUESTSTATE_BODY; // dispatched once, at start of body dispatchRequestHandler(current_cid, routeid, GSREQUESTSTATE_BODY_START); } break; case GSREQUESTSTATE_BODY: if (content_lengths_[ current_cid ] > 0) { content_lengths_[ current_cid ] --; } if (ring_isfull(_buf_cmd)) { dispatchRequestHandler(current_cid, routeid, request_state); // POST, user callback should write() } ring_put(_buf_cmd, dat); break; case GSREQUESTSTATE_ERROR: // skip until received whole request break; case GSREQUESTSTATE_RECEIVED: default: break; } // end of bulk transfered data if (len == 0) { gs_mode_ = GSMODE_COMMAND; if ( request_state == GSREQUESTSTATE_ERROR ) { writeHead( current_cid, error_code ); writeEnd(); ring_put( &commands, COMMAND_CLOSE ); ring_put( &commands, current_cid ); } else { if (content_lengths_[ current_cid ] == 0) { // if Content-Length header was longer than <ESC>Z length, // we wait til next bulk transfer request_state = GSREQUESTSTATE_RECEIVED; } // user callback should write(), writeEnd() and close() dispatchRequestHandler(current_cid, routeid, request_state); } ring_clear(_buf_cmd); } } else { // is request from us static uint16_t status_code; switch (request_state) { case GSREQUESTSTATE_HEAD1: if (dat != '\n') { if ( ! ring_isfull(_buf_cmd) ) { // ignore if overflowed ring_put( _buf_cmd, dat ); } } else { uint8_t i=0; // skip 9 characters "HTTP/1.1 " while (i++ < 9) { ring_get( _buf_cmd, &temp[0], 1 ); } // copy 3 numbers representing status code into temp buffer temp[ 3 ] = 0; int8_t count = ring_get( _buf_cmd, temp, 3 ); if (count != 3) { // protocol error // we should receive something like: "200 OK", "401 Unauthorized" status_code = 999; request_state = GSREQUESTSTATE_ERROR; break; } status_code = atoi(temp); request_state = GSREQUESTSTATE_HEAD2; continuous_newlines_ = 0; content_lengths_[ current_cid ] = 0; ring_clear(_buf_cmd); } break; case GSREQUESTSTATE_HEAD2: if(0 == parseHead2(dat, current_cid)) { request_state = GSREQUESTSTATE_BODY; // dispatched once, at start of body dispatchResponseHandler(current_cid, status_code, GSREQUESTSTATE_BODY_START); } break; case GSREQUESTSTATE_BODY: if (content_lengths_[ current_cid ] > 0) { content_lengths_[ current_cid ] --; } if (ring_isfull(_buf_cmd)) { dispatchResponseHandler(current_cid, status_code, GSREQUESTSTATE_BODY); } ring_put(_buf_cmd, dat); break; case GSREQUESTSTATE_ERROR: case GSREQUESTSTATE_RECEIVED: default: break; } if (len == 0) { gs_mode_ = GSMODE_COMMAND; if ( request_state == GSREQUESTSTATE_ERROR ) { dispatchResponseHandler(current_cid, status_code, request_state); } else { if (content_lengths_[ current_cid ] == 0) { // if Content-Length header was longer than <ESC>Z length, // we wait til all response body received. // we need to close our clientRequest before handling it. // GS often locks when closing 2 connections in a row // ex: POST /keys from iPhone (cid:1) -> POST /keys to server (cid:2) // response from server arrives -> close(1) -> close(2) -> lock!! // the other way around: close(2) -> close(1) doesn't lock :( request_state = GSREQUESTSTATE_RECEIVED; } dispatchResponseHandler(current_cid, status_code, request_state); } ring_clear( _buf_cmd ); } } // is response } // (next_token == NEXT_TOKEN_DATA) }