Gemma M0 mouse nudger

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
sembazuru
 
Posts: 141
Joined: Sat Mar 30, 2013 2:50 am

Gemma M0 mouse nudger

Post by sembazuru »

Here at work IT has set all computers to lock after 15 minutes of inactivity. This can get quite intrusive during online meetings. So, I wrote the following sketch to move the mouse 1 pixel at a random diagonal direction every minute. (Basically removing everything but the LED color cycling demo "main.py" that was installed on one of my Gemma M0s and then adding my mouse nudging bit.)

My design decisions were:
  • Power on indicator: Use the color cycling at a low brightness of the Dotstar to indicate that the Gemma is powered on.
  • Move mouse 1/minute: Randomly choose -1 pixel or 1 pixel motion for each axis. The mouse will always move on screen edges, and will move 75% of the time in the corners.
  • Indicate movement to user: Because the movement is only 1 pixel and easy to not notice, flash the Dotstar white at full brightness when moving the mouse.
Here is the code in-line (I couldn't attach the file because the extension "py" is not allowed by the forum software.)

Code: Select all

# Nudge the mouse once a minute to defeat timed lock using Gemma
# Using AdaFruit's "wheel" helper function to cycle colors on the on-board LED

import board
import time
import random
from analogio import AnalogIn
import adafruit_dotstar as dotstar
from adafruit_hid.mouse import Mouse

# Configuration variables
cycleBrightness = 0.1         # Brightness of LED during normal color cycling. Default = 0.1
nudgeBrightness = 1           # Brightness of LED for nudge flash. Default = 1
nudgeColor = [255, 255, 255]  # Color of LED for nudge flash. Default = [255, 255, 255]
nudgeDistance = 1             # Distance to move mouse during the nudge. Default = 1
nudgeDelay = 60               # Interval between nudges (in whole seconds). Default = 60
nudgeFlashLength = 0.1        # Length of nudge flash. This is a blocking delay. Default = 0.1

# One pixel connected internally!
dot = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1)

# Analog input on A1
analogin = AnalogIn(board.A1)

# initialize random number generator
random.seed(analogin.value)

# HID mouse
m = Mouse()


# ######################## HELPERS ##############################

# Helper to nudge mouse randomly, 
def nudge():
    x = random.choice([(nudgeDistance * -1), nudgeDistance])
    y = random.choice([(nudgeDistance * -1), nudgeDistance])
    m.move(x, y, 0)
    dot.brightness = nudgeBrightness
    dot[0] = nudgeColor
    time.sleep(0.1)

# Helper to give us a nice color swirl
def wheel(pos):
    # Input a value 0 to 255 to get a color value.
    # The colours are a transition r - g - b - back to r.
    if (pos < 0):
        return [0, 0, 0]
    if (pos > 255):
        return [0, 0, 0]
    if (pos < 85):
        return [int(pos * 3), int(255 - (pos*3)), 0]
    elif (pos < 170):
        pos -= 85
        return [int(255 - pos*3), 0, int(pos*3)]
    else:
        pos -= 170
        return [0, int(pos*3), int(255 - pos*3)]

# ######################## MAIN LOOP ##############################

i = 0
currentNudgeTimeCheck = 0
lastNudgeTimeCheck = 0
while True:
    # spin internal LED around!
    dot.brightness = cycleBrightness
    dot[0] = wheel(i)
    dot.show()

    currentNudgeTimeCheck = time.monotonic() // 1  # round to 1 second accuracy
    if (((currentNudgeTimeCheck % nudgeDelay) == 0) &
       (currentNudgeTimeCheck != lastNudgeTimeCheck)):
        nudge()
        lastNudgeTimeCheck = currentNudgeTimeCheck

    i = (i+1) % 256  # run from 0 to 255
Enjoy, all!
Last edited by sembazuru on Wed May 02, 2018 2:30 pm, edited 1 time in total.

User avatar
johnpark
 
Posts: 985
Joined: Wed Mar 25, 2009 2:15 pm

Re: Gemma M0 mouse nudger

Post by johnpark »

Yay, I love this project! When I made the Phantom Mouse Jiggler project https://learn.adafruit.com/phantom-mous ... r?view=all I was inspired by USB mouse nudgers I'd read about that were used in corporate environments to prevent slide presentations from getting hijacked by IT mandated mouse activity : ]

If you end up sprinkling this around your office, you may want to try the 1" heat shrink tubing trick and a short USB cable or OTG adapter to make them discreet. https://learn.adafruit.com/the-foul-fow ... l-upgrades

Great job!

User avatar
sembazuru
 
Posts: 141
Joined: Sat Mar 30, 2013 2:50 am

Re: Gemma M0 mouse nudger

Post by sembazuru »

Since my application is intentionally non-covert, using black shrink tube would defeat the cycling & flashing LED indication and any shrink tube would defeat access to the power switch. I actually have mine at the end of a long USB cable so it is dangling right over the corner of my monitor bezel. I should probably tape it down at some point, probably will use a 3M Command adhesive strip so I can move it if I ever change my monitor.

I have a question about the applicability of the OSG adapter. Looking at the product page for it, it appears to mimic the contact portion of a USB-A female port (minus the shroud). As such, would it fit plugging into a regular USB-A female port? I would think that even if it did fit, the connections would be reversed. Or is my thinking backwards?

Thanx for the kudos though. :-D

User avatar
johnpark
 
Posts: 985
Joined: Wed Mar 25, 2009 2:15 pm

Re: Gemma M0 mouse nudger

Post by johnpark »

Haha, sorry, I'm still stuck in "covert spy" mode from working on AdaBox 007 ; ]

Yes, that's exactly how you can use the little OTG adapter -- It's just like using any USB cable as far as connections go. I've tried it with a Gemma M0, and you end up with a neat little dongle setup.

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”