Skip to content

Instantly share code, notes, and snippets.

@parasquid
Created October 27, 2019 09:38
Show Gist options
  • Save parasquid/4ae18d09e80e7248e37ab810b6b226e5 to your computer and use it in GitHub Desktop.
Save parasquid/4ae18d09e80e7248e37ab810b6b226e5 to your computer and use it in GitHub Desktop.
ATTiny85 SoftwareSerial and TinyWireS
https://forum.arduino.cc/index.php?topic=441428.0
#include "TinyWireS.h"
#define I2C_SLAVE_ADDR 0x25
#include "SoftwareSerial.h"
const int Rx = 3; // this is physical pin 2 - you do not need to connect this to DFPlayer
const int Tx = 4; // this is physical pin 3 - connect to RX pin on DFPlayer
SoftwareSerial mySerial(Rx, Tx);
#define Start_Byte 0x7E
#define Version_Byte 0xFF
#define Command_Length 0x06
#define End_Byte 0xEF
#define Acknowledge 0x00 //Returns info with command 0x41 [0x01: info, 0x00: no info]
char myCmd = ' ';
int myPar = 0;
void setup() {
pinMode(Rx, INPUT);
pinMode(Tx, OUTPUT);
mySerial.begin(9600); // send serial data at 9600 bits/sec
TinyWireS.begin(I2C_SLAVE_ADDR); // init I2C Slave mode
TinyWireS.onReceive(receiveEvent);
TinyWireS.onRequest(requestEvent);
delay(500);
execute_CMD(0x06,0,5);
}
void requestEvent()
{
TinyWireS.send(myCmd);
}
void receiveEvent(uint8_t howMany) {
char c=' ';
while (1 < TinyWireS.available()) { // loop through all but the last
c = TinyWireS.receive(); // receive byte as a character
}
int x = TinyWireS.receive(); // receive byte as an integer
myCmd = c;
myPar = x;
}
void execute_CMD(byte CMD, byte Par1, byte Par2) // Excecute the command and parameters
{
// Calculate the checksum (2 bytes)
int16_t checksum = -(Version_Byte + Command_Length + CMD + Acknowledge + Par1 + Par2);
// Build the command line
byte Command_line[10] = { Start_Byte, Version_Byte, Command_Length, CMD, Acknowledge, Par1, Par2, checksum >> 8, checksum & 0xFF, End_Byte};
//Send the command line to the module
for (byte k=0; k<10; k++)
{
mySerial.write( Command_line[k]);
}
}
void loop() {
// put your main code here, to run repeatedly:
switch (myCmd) {
case 'p':
case 'P': execute_CMD(0x03,0,myPar); myCmd = ' '; break;
case 'v':
case 'V': execute_CMD(0x06,0,myPar); myCmd = ' '; break;
}
TinyWireS_stop_check;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment