Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fast Read/Write #2

Open
lmeucchi opened this issue Dec 3, 2021 · 2 comments
Open

Fast Read/Write #2

lmeucchi opened this issue Dec 3, 2021 · 2 comments

Comments

@lmeucchi
Copy link

lmeucchi commented Dec 3, 2021

Hello, how are you ... I'm using the PS2 library to control a trackpad, after seeing your code I used some functions and settings to be able to send midi messages. It works very well but I needed more speed in reading the trackpad, that's why I made a test code to measure how many cycles per second the "PS2" and "PS2bs" library takes ... but I have no good news.

With Arduino Mega:
ps2: 112 Cycles per Second
ps2bs: 103 Cycles per Second

I wanted to know if there is something I am doing wrong, but the library "digitalWriteFast" does not allow to use constants and that is why it works in a normal "slow" way.

For the test code I use the files:

  • ps2.h
  • ps2.cpp
  • digitalWriteFast.h
  • ps2bs.h
  • ps2bs.cpp

Here is the code, I use the two libraries at the same time but I choose each one in line 15 and 16
//PS2 mouse(MOUSE_CLOCK, MOUSE_DATA); // usage: PS2 mouse(int clk, int data)
PS2bs mouse(MOUSE_CLOCK, MOUSE_DATA); // usage: PS2bs mouse(int clk, int data)

Thank you very much, regards

/*
 * 
 *    ps2:    112 Cycles per Second
 * 
 *    ps2bs:  103 Cycles per Second
 * 
 */

#include "ps2.h"
#include "ps2bs.h"

#define MOUSE_DATA 15
#define MOUSE_CLOCK 14

//PS2 mouse(MOUSE_CLOCK, MOUSE_DATA);  //  usage: PS2      mouse(int clk, int data)
PS2bs mouse(MOUSE_CLOCK, MOUSE_DATA);  //  usage: PS2bs  mouse(int clk, int data)

int x = 0;
int y = 0; 
int z = 0;

byte mstat1;
byte mstat2;
byte mxy;
byte mx;
byte my;
byte mz;



void setup() 
{
  Serial.begin(115200); 

  mouse_init();
}



void loop()
{
  
  Debug_Cycles_Per_Second ();
  
  mouse_read_loop (); 
  
}


void Debug_Cycles_Per_Second ()
{
  static unsigned long cycles = 0;
  static unsigned long start = 0;
  static unsigned long current = 0;
  cycles++;
  current = millis();
  if (current - start >= 1000)
  {
    Serial.print("Cycles x Seg: ");
    Serial.println(cycles);   
    cycles = 0;
    start = current;
  }
}




void mouse_read_loop ()
{
  mouse.write(0xeb);
  mouse.read();
  
  mstat1 = mouse.read();    //State 1     enable/disable    button on/off
  mxy = mouse.read();       //Both Axis
  mz = mouse.read();        //Pressure    finger pressure value
  mstat2 = mouse.read();    //State 2     different pad positions 
  mx = mouse.read();        //Axis X      value
  my = mouse.read();        //Axis Y      value
 
  // collect the bits for x and y
  x = (((mstat2 & 0x10) << 8) | ((mxy & 0x0F) << 8) | mx );
  y = (((mstat2 & 0x20) << 7) | ((mxy & 0xF0) << 4) | my );
  z = mz;
}




void mouse_init()
{
  mouse.write(0xff);      // Reset
  mouse.read();           // Acknowledge byte      byte reconocimiento
  mouse.read();           // Blank */
  mouse.read();           // Blank */
  mouse.write(0xf0);      // Set Remote Mode
  mouse.read();           // Ack byte
  delayMicroseconds(100);
  mouse.write(0xe8);      // Set Resolution (argument 0-3)
  mouse.read();           // Ack byte
  mouse.write(0x03);      // x1  ( x1 * 64  +  x2 * 16  +  x3 * 4  +  x4   == modebyte )
  mouse.read();           // Ack byte
  mouse.write(0xe8);      // Set Resolution
  mouse.read();           // Ack byte
  mouse.write(0x00);      // x2
  mouse.read();           // Ack byte
  mouse.write(0xe8);      // Set Resolution
  mouse.read();           // Ack byte
  mouse.write(0x01);      // x3
  mouse.read();           // Ack byte
  mouse.write(0xe8);      // Set Resolution
  mouse.read();           // Ack byte
  mouse.write(0x00);      // x4
  mouse.read();           // Ack byte
  mouse.write(0xf3);      // Set Samplerate 20 (stores mode) (argument $00-$FF)
  mouse.read();           // Ack byte
  mouse.write(0x14);      // Samplerate Argument 20
  mouse.read();           // Ack byte
  delayMicroseconds(100); 
}
@Turbosavski
Copy link
Owner

Thanks for exhaustive comment! At the time i was also reaching out for speed - unsuccessfully it seems. But the machine is totally useful as it is - so i will drop the digitalWriteFast library for now.

With your comments and code i might look into it again - or you might?! But maybe the digitalRead(), pinMode() and digotalWrite() arduino functions are fast enough.

I think i remember also having some other issues regarding ps2.h library - and I somehow solved it with ps2bs.h

Cheers!

@lmeucchi
Copy link
Author

Hello
I've been looking for a solution, but I think it's impossible

I was able to reduce the time, adding this library "https://github.com/Locoduino/DIO2"
and i got these results:

ps2: Total Loop duration 9020 µs
ps2bs: Total Loop duration 9924 µs
ps2_Fast: Total Loop duration 1568 µs

It is really fast, more than I expected.
But I don't know why, it can't read any value from the trackpad ...
I think that the ps2 protocol cannot be handled as fast as it can with the DIO2 library.

The simplest solution was to go back and use the original ps2 library, but creating two functions ...

The first one, reads only 1 bytes from the trackpad and compares it with a decimal value (in my case, 128 not pressed and 160 pressed)

The second, really when the trackpad is pressed (160 decimal)
Start reading all the bytes of the trackpad and send midi message

example:

void TrackPad ()
{
  Trackpad.write(0xeb);        //Read Data
  Trackpad.read();
  
  mstat1 = Trackpad.read();    //State 1     enable/disable    button on/off

  // If the trackpad is pressed, then call the function to read all the full bytes
  if (mstat1 == 160)
  {
    TrackPad_Enable ();
  }
}

 
void TrackPad_Enable ()
{
  Trackpad.write(0xeb);        //Read Data
  Trackpad.read();
  
  mstat1 = Trackpad.read();    //State 1     enable/disable    button on/off
  mxy = Trackpad.read();       //Both Axis
  mz = Trackpad.read();        //Pressure    finger pressure value
  mstat2 = Trackpad.read();    //State 2     different pad positions 
  mx = Trackpad.read();        //Axis X      value
  my = Trackpad.read();        //Axis Y      value
 

  // collect the bits for x and y
  x = (((mstat2 & 0x10) << 8) | ((mxy & 0x0F) << 8) | mx );
  y = (((mstat2 & 0x20) << 7) | ((mxy & 0xF0) << 4) | my );
  z = mz;


//do an action and send midi message


}

It is a simple but effective solution

Anyway, if you want to see how I modify the ps2 library, tell me and I'll share it here ..... read very quickly but it doesn't work

Thank you very much, regards

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants