//------------------------------------------------------------------------------------------- void LCD::setX(byte posX) //0-127 or 0-159 pixels { //Set the X position serial.write(0x7C); serial.write(0x18);//CTRL x serial.write(posX); //characters are 8 pixels tall x 6 pixels wide //The top left corner of a char is where the x/y value will start its print //For example, if you print a char at position 1,1, the bottom right of your char will be at position 7,9. //Therefore, to print a character in the very bottom right corner, you would need to print at the coordinates //x = 154 , y = 120. You should never exceed these values. // Here we have an example using an upper case 'B'. The star is where the character starts, given a set //of x,y coordinates. # represents the blocks that make up the character, and _ represnets the remaining //unused bits in the char space. // *###__ // # #_ // # #_ // ####__ // # #_ // # #_ // ####__ // ______ }
/* This function sets the name of an RN-42 module name should be an up to 20-character value. It MUST BE TERMINATED by a \r character */ uint8_t makeyMateClass::setName(char * name) { if (bluetooth.available()) bluetooth.flush(); // Get rid of any characters in the buffer, we'll need to check it fresh bluetooth.print("SN,"); for (int i=0; i<20; i++) { if (name[i] != '\r') bluetooth.write(name[i]); else break; } bluetooth.write('\r'); delay(BLUETOOTH_RESPONSE_DELAY); bluetoothReceive(rxBuffer); /* Double check the setting, output results in Serial monitor */ bluetooth.flush(); bluetooth.print("GN"); bluetooth.write('\r'); delay(BLUETOOTH_RESPONSE_DELAY); Serial.print("Name set to: "); while (bluetooth.available()) Serial.write(bluetooth.read()); return bluetoothCheckReceive(rxBuffer, "AOK", 3); }
void lcd_setBrightness(int brightness){ // set the brightness - we'll max it (255 is max brightness) lcd.write(0xFE); lcd.write(0x99); lcd.write(255); delay(10); }
void lcd_setCursor(int column, int row){ lcd.write(0xFE); lcd.write(0x47); lcd.write((uint8_t)column); lcd.write((uint8_t)row); delay(10); }
//------------------------------------------------------------------------------------------- void LCD::setBacklight(byte duty) { //changes the back light intensity, range is 0-100. serial.write(0x7C); serial.write(0x02); //CTRL b serial.write(duty); //send a value of 0 - 100 }
void lcd_setContrast(int contrast){ // set the contrast, 200 is a good place to start, adjust as desired lcd.write(0xFE); lcd.write(0x50); lcd.write(200); delay(10); }
void setup() { // Init display mySerial.begin(9600); // set up serial port for 9600 baud delay(500); // wait for display to boot up // Setup DS1820 temp sensor sensors.begin(); sensors.setResolution(Sensor1, 11); sensors.setResolution(Sensor2, 11); sensors.setWaitForConversion(false); sensors.requestTemperatures(); delayInMillis = 750 / (1 << (12 - 11)); //750 for 12bit, 400 for 11bit, 220 for 10bit, 100 for 9bit // calc by delayInMillis = 750 / (1 << (12 - resolution)); lastTempRequest = millis(); // Set next state i FSM menu_FSM = M_PAGE1; menu_last_state = M_PAGE1; system_FSM = S_IDLE; // **************** Set up display ******************* DisplayClear(); MenuShowTime = millis(); // **************** Set up RTC *********************** Wire.begin(); rtc.begin(); //TimeDate(rtc.now(),dateTimeString,1); //DateTime now = rtc.now(); // write on display DisplayGoto(2,0); mySerial.print("Version 0.9B"); // **************** Set up SD card ******************* pinMode(10, OUTPUT); DisplayGoto(1,0); mySerial.write("Init SD -> "); // clear display + legends DisplayGoto(1,11); // see if the card is present and can be initialized: if (!SD.begin()) mySerial.write("Fail"); else mySerial.write("OK"); delay(2000); // ***************** Clear display ******************** DisplayClear(); }
//------------------------------------------------------------------------------------------- void LCD::clearScreen() { //clears the screen, you will use this a lot! serial.write(0x7C); serial.write((byte)0); //CTRL @ //can't send LCD.write(0) or LCD.write(0x00) because it's interprestted as a NULL }
void lcd_setColor(int red, int green, int blue){ lcd.write(0xFE); lcd.write(0xD0); lcd.write((uint8_t)red); lcd.write((uint8_t)green); lcd.write((uint8_t)blue); delay(1000); }
void DisplayGoto(int line ,int pos) { mySerial.write(0xFE); //command flag if(line==1) mySerial.write(128+pos); if(line==2) mySerial.write(128+64+pos); }
//------------------------------------------------------------------------------------------- void LCD::setY(byte posY)//0-63 or 0-127 pixels { //Set the y position serial.write(0x7C); serial.write(0x19);//CTRL y serial.write(posY); }
/* This function sends a key press down. An array of pressed keys is generated and the RN-42 module is commanded to send a keyboard report. The k parameter should either be an HID usage value, or one of the key codes provided for in settings.h Does not release the key! */ uint8_t makeyMateClass::keyPress(uint8_t k) { uint8_t i; if (k >= 136) // Non printing key, these are listed in settings.h { k = k - 136; } else if (k >= 128) // !!! Modifiers, NOT YET IMPLEMENTED { } else { k = asciiToScanCode[k]; if (!k) { return 0; } if (k & 0x80) { modifiers |= 0x02; // Adds the shift key to modifiers variable k &= 0x7F; // k can only be a 7-bit value. } } /* generate the key report into the keyCodes array we can send up to 6 key presses, first make sure k isn't already in there */ if (keyCodes[0] != k && keyCodes[1] != k && keyCodes[2] != k && keyCodes[3] != k && keyCodes[4] != k && keyCodes[5] != k) { for (i=0; i<6; i++) // Add k to the next available array index { if (keyCodes[i] == 0x00) { keyCodes[i] = k; break; } } if (i == 6) // Too many characters to send { return 0; } } bluetooth.write(0xFE); // Keyboard Shorthand Mode bluetooth.write(0x07); // Length bluetooth.write(modifiers); // Modifiers for (int j=0; j<6; j++) { bluetooth.write(keyCodes[j]); // up to six key codes, 0 is nothing } return 1; }
//------------------------------------------------------------------------------------------- void LCD::setPixel(byte x, byte y, byte set) { serial.write(0x7C); serial.write(0x10);//CTRL p serial.write(x); serial.write(y); serial.write(0x01); delay(10); }
//------------------------------------------------------------------------------------------- void LCD::setHome() { serial.write(0x7C); serial.write(0x18); serial.write((byte)0);//set x back to 0 serial.write(0x7C); serial.write(0x19); serial.write((byte)0);//set y back to 0 }
void lcd_begin(){ //lcd = SoftwareSerial(0,2); lcd.begin(9600); // set the size of the display if it isn't 16x2 (you only have to do this once) lcd.write(0xFE); lcd.write(0xD1); lcd.write(16); // 16 columns lcd.write(2); // 2 rows delay(10); }
void bt_command_mode() { serial.write('+'); delay(100); serial.write('+'); delay(100); serial.write('+'); delay(100); serial.write('\r'); delay(100); }
/* This function sends the HID report for mouse movement and clicks You can send any of the mouse clicks in the first parameter (b) Mouse movement (horizontal and vertical) goes in the final two parameters (x and y). */ void makeyMateClass::moveMouse(uint8_t b, uint8_t x, uint8_t y) { bluetooth.write(0xFD); // Send a RAW report bluetooth.write(5); // length bluetooth.write(2); // indicates a Mouse raw report bluetooth.write((byte) b); // buttons bluetooth.write((byte) x); // x movement bluetooth.write((byte) y); // y movement bluetooth.write((byte) 0); // wheel movement NOT YET IMPLEMENTED }
void disconnect() { bt_command_mode(); serial.write('A'); delay(100); serial.write('T'); delay(100); serial.write('H'); delay(100); serial.write('\r'); }
//------------------------------------------------------------------------------------------- void LCD::eraseBlock(byte x1, byte y1, byte x2, byte y2) { //This is just like the draw box command, except the contents of the box are erased to the background color serial.write(0x7C); serial.write(0x05);//CTRL e serial.write(x1); serial.write(y1); serial.write(x2); serial.write(y2); delay(10); }
//------------------------------------------------------------------------------------------- void LCD::drawBox(byte x1, byte y1, byte x2, byte y2, byte set) { //draws a box from two given points. You can set and reset just as the pixel function. serial.write(0x7C); serial.write(0x0F);//CTRL o serial.write(x1); serial.write(y1); serial.write(x2); serial.write(y2); serial.write(0x01); delay(10); }
//------------------------------------------------------------------------------------------- void LCD::drawCircle(byte x, byte y, byte rad, byte set) { //draws a circle from a point x,y with a radius of rad. //Circles can be drawn off-grid, but only those pixels that fall within the //display boundaries will be written. serial.write(0x7C); serial.write(0x03);//CTRL c serial.write(x); serial.write(y); serial.write(rad); serial.write(0x01); delay(10); }
//------------------------------------------------------------------------------------------- void LCD::setBaud(byte baud) { //changes the baud rate. serial.write(0x7C); serial.write(0x07); //CTRL g serial.write(baud); //send a value of 49 - 54 delay(100); /* “1” = 4800bps - 0x31 = 49 “2” = 9600bps - 0x32 = 50 “3” = 19,200bps - 0x33 = 51 “4” = 38,400bps - 0x34 = 52 “5” = 57,600bps - 0x35 = 53 “6” = 115,200bps - 0x36 = 54 */ //these statements change the SoftwareSerial baud rate to match the baud rate of the LCD. if(baud == 49) { serial.end(); serial.begin(4800); } if(baud == 50) { serial.end(); serial.begin(9600); } if(baud == 51) { serial.end(); serial.begin(19200); } if(baud == 52) { serial.end(); serial.begin(38400); } if(baud == 53) { serial.end(); serial.begin(57600); } if(baud == 54) { serial.end(); serial.begin(115200); } }
void loop() // run over and over { if (mySerial.available()) Serial.write(mySerial.read()); if (Serial.available()) mySerial.write(Serial.read()); }
// Sending the buffer long lenght to Serial port void HerkulexClass::sendData(byte* buffer, int lenght) { clearBuffer(); //clear the serialport buffer - try to do it! switch (port) { case SSerial: SwSerial.write(buffer, lenght); delay(1); break; #if defined (__AVR_ATmega1280__) || defined (__AVR_ATmega128__) || defined (__AVR_ATmega2560__) case HSerial1: Serial1.write(buffer, lenght); delay(1); break; case HSerial2: Serial2.write(buffer, lenght); delay(1); break; case HSerial3: Serial3.write(buffer, lenght); delay(1); break; #elif (__AVR_ATmega32U4__) case HSerial1: Serial1.write(buffer, lenght); delay(1); break; #endif } }
/* sendPacket() is called by just about every other member function. It calculates some CRC bytes, then sends the message string. If a response is requested, it'll return that in the response array. Otherwise that and the responseLength variable should be 0. If you're using a bluetooth module that's not the RN-42, this'd be the place to modify. */ void SFE_MetaWatch::sendPacket(unsigned char * data, int length, unsigned char * response, int responseLength) { int crc = ComputeCRC(data, length - 2); // Get the crc values for our string data[length-1] = (crc & 0xFF00) >> 8; // LSB goes first data[length-2] = crc & 0xFF; // the MSB // If you want a response, let's flush out the bt buffer first. if (responseLength > 0) bt.flush(); // Send the data out to the BlueSMiRF for (int i=0; i<length; i++) { bt.write(data[i]); } // If a response was requested, read that into the response array. if (responseLength > 0) { delay(BLUETOOTH_RESPONSE_DELAY); int i=0; while (bt.available() && (i < responseLength)) { response[i++] = bt.read(); } } }
void S4GPS::setupGPS(uint8_t* mesg, uint8_t bits) { //Serial.println(); for(int i = 0; i< bits; i++) { gpsSerial.write(mesg[i]); //Serial.print(mesg[i]); } }
/* This function releases a key press down. If it's there, k will be removed from the keyCodes array, then that new array is sent to the RN-42 which is commanded to send a keyboard report. The k parameter should either be an HID usage value, or one of the key codes provided for in settings.h */ uint8_t makeyMateClass::keyRelease(uint8_t k) { uint8_t i; if (k >= 136) // Non printing key { k = k - 136; } else if (k >= 128) // !!! Modifiers, todo !!! { } else { k = asciiToScanCode[k]; if (!k) { return 0; } if (k & 0x80) { modifiers &= ~0x02; // Clear the modifier k &= 0x7F; } } for (i=0; i<6; i++) { if ((0 != k) && (keyCodes[i] == k)) { keyCodes[i] = 0x00; // set the value that was k to 0 } } /* send the new report: */ bluetooth.write(0xFE); // Keyboard Shorthand Mode bluetooth.write(0x07); // Length bluetooth.write(modifiers); // Modifiers for (int j=0; j<6; j++) { bluetooth.write(keyCodes[j]); // 6 possible scan codes } return 1; }
/* This function returns a 1 if the RN-42 is already in HID mode The module MUST BE IN COMMAND MODE for this function to work! */ uint8_t makeyMateClass::getHIDMode(void) { bluetooth.flush(); bluetooth.print("G~"); // '~' is the RN-42's HID/SPP set command bluetooth.write('\r'); delay(BLUETOOTH_RESPONSE_DELAY); bluetoothReceive(rxBuffer); // receive all response chars into rxBuffer return bluetoothCheckReceive(rxBuffer, "6", 1); }
/* This function will attempt a connection to the stored remote address The first time you connect the the RN-42 HID, the master device will need to initiate the connection. The first time a connection is made the bluetooth address of the master device will be stored on the RN-42. If no remote address is stored, a connection will not be made. */ uint8_t makeyMateClass::connect() { freshStart(); // Get the module disconnected, and out of command mode while (!enterCommandMode()) { // Enter command mode delay(BLUETOOTH_RESPONSE_DELAY); } delay(BLUETOOTH_RESPONSE_DELAY); bluetooth.flush(); /* get the remote address and print it in the serial monitor */ bluetooth.print("GR"); // Get the remote address bluetooth.write('\r'); delay(BLUETOOTH_RESPONSE_DELAY); if (bluetooth.peek() == 'N') // Might say "No remote address stored */ { // (bluetooth address is hex values only, so won'te start with 'N'. Serial.println("Can't connect. No paired device!"); bluetooth.flush(); bluetooth.print("---"); // exit command mode bluetooth.write('\r'); return 0; // No connect is attempted } else if (bluetooth.available() == 0) { // If we can't communicate with the module at all, print error Serial.println("ERROR!"); return 0; // return error } /* otherwise print the address we're trying to connect to */ Serial.print("Attempting to connect to: "); while (bluetooth.available()) Serial.write(bluetooth.read()); /* Attempt to connect */ bluetooth.print("C"); // The connect command bluetooth.write('\r'); delay(BLUETOOTH_RESPONSE_DELAY); while (bluetooth.available()) Serial.write(bluetooth.read()); // Should print "TRYING" return 1; }
/* This function will set the RN-42 into HID mode, from SPP mode. Requires a reboot to take effect! */ uint8_t makeyMateClass::setHIDMode(void) { if (bluetooth.available()) bluetooth.flush(); // Get rid of any characters in the buffer, we'll need to check it fresh bluetooth.print("S~,6"); // Bluetooth HID Mode bluetooth.write('\r'); delay(BLUETOOTH_RESPONSE_DELAY); bluetoothReceive(rxBuffer); /* Double check the setting, output results in Serial monitor */ bluetooth.flush(); bluetooth.print("G~"); bluetooth.write('\r'); delay(BLUETOOTH_RESPONSE_DELAY); Serial.print("Profile set to: "); while (bluetooth.available()) Serial.write(bluetooth.read()); return bluetoothCheckReceive(rxBuffer, "AOK", 3); }