From b10d5eaca2a24cf2aa4ff00c630156e94442b894 Mon Sep 17 00:00:00 2001 From: nathan Date: Sat, 24 Dec 2016 00:16:24 -0600 Subject: [PATCH] adding digistump encoder arduino code Former-commit-id: ae89e87ec766ff19f1c3ec92934430065a9205b6 --- .../encoder_loop_debounce.ino | 92 +++++++++++++ .../encoder_loop_debounce_002.ino | 125 ++++++++++++++++++ digistump_firmware/libraries/readme.txt | 1 + grid_pc.py | 60 +++++++++ 4 files changed, 278 insertions(+) create mode 100644 digistump_firmware/encoder_loop_debounce/encoder_loop_debounce.ino create mode 100644 digistump_firmware/encoder_loop_debounce_002/encoder_loop_debounce_002.ino create mode 100644 digistump_firmware/libraries/readme.txt create mode 100755 grid_pc.py diff --git a/digistump_firmware/encoder_loop_debounce/encoder_loop_debounce.ino b/digistump_firmware/encoder_loop_debounce/encoder_loop_debounce.ino new file mode 100644 index 0000000..fdf5f25 --- /dev/null +++ b/digistump_firmware/encoder_loop_debounce/encoder_loop_debounce.ino @@ -0,0 +1,92 @@ +#include "DigiKeyboard.h" + +#define encoder0PinA 0 +#define encoder0PinB 2 + +#define aIn //5 is 0 on the digistump + +#define avgPeriod 100 + +static boolean rotating=false; + +float avgRead = 0; +float lastRead = 0; +unsigned char avgCount = 0; + +float buttonState = 0; + +bool lastButtonState = 0; + +int rotIncrement = 0; + +void setup() { + + pinMode(encoder0PinA, INPUT); + digitalWrite(encoder0PinA, HIGH); + pinMode(encoder0PinB, INPUT); + digitalWrite(encoder0PinB, HIGH); + + pinMode(1, INPUT); + digitalWrite(1, HIGH); + + attachInterrupt(0, rotEncoder, CHANGE); +} + +void rotEncoder(){ + rotating=true; +} + +void loop() { + DigiKeyboard.update(); + + avgRead += (float)analogRead(0) / avgPeriod; + avgCount++; + + buttonState += (float)digitalRead(1) / avgPeriod; + + if(avgCount >= avgPeriod) { + if(abs(avgRead - lastRead) >= 1) { + DigiKeyboard.print(String((int)avgRead, HEX)); + DigiKeyboard.println(""); + lastRead = avgRead; + } + + if((buttonState > .5) != lastButtonState) { + lastButtonState = (buttonState > .5); + + if(lastButtonState) { + DigiKeyboard.print("n"); + } else { + DigiKeyboard.print("m"); + } + + } + + avgRead = 0; + avgCount = 0; + buttonState = 0; + + } + + while(rotating) { + delay(2); + + if (digitalRead(encoder0PinA) == digitalRead(encoder0PinB)) { + rotIncrement++; + } + else { + rotIncrement--; + } + rotating=false; // Reset the flag back to false + } + + if(rotIncrement >= 2) { + DigiKeyboard.print("z"); + rotIncrement = 0; + } + + if(rotIncrement <= -2) { + DigiKeyboard.print("x"); + rotIncrement = 0; + } +} diff --git a/digistump_firmware/encoder_loop_debounce_002/encoder_loop_debounce_002.ino b/digistump_firmware/encoder_loop_debounce_002/encoder_loop_debounce_002.ino new file mode 100644 index 0000000..d1347a4 --- /dev/null +++ b/digistump_firmware/encoder_loop_debounce_002/encoder_loop_debounce_002.ino @@ -0,0 +1,125 @@ +#include "DigiKeyboard.h" + +#define encoderPinA 0 +#define encoderPinB 2 +#define selectPin 1 +#define sqlPin 0 //5 is 0 on the digistump + +#define bufferPeriod 100 + +float encoderPinABuffer = 0; +float encoderPinBBuffer = 0; +float selectPinBuffer = 0; +float sqlPinBuffer = 0; + +int encoderCounter = 0; + +bool encoderPinALast = 0; +bool encoderPinBLast = 0; +bool selectPinLast = 0; +float sqlPinLast = 0; + +int bufferCounter = 0; + +int8_t lookup_table[] = {0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0}; + +uint8_t index = 0; + +void setup() { + + pinMode(encoderPinA, INPUT); + digitalWrite(encoderPinA, HIGH); + pinMode(encoderPinB, INPUT); + digitalWrite(encoderPinB, HIGH); + + pinMode(selectPin, INPUT); + digitalWrite(selectPin, HIGH); + + pinMode(sqlPin, INPUT); +} + +void loop() { + + //update buffers + + //encoderPinABuffer += (float)digitalRead(encoderPinA) / bufferPeriod; + //encoderPinBBuffer += (float)digitalRead(encoderPinB) / bufferPeriod; + selectPinBuffer += (float)digitalRead(selectPin) / bufferPeriod; + sqlPinBuffer += (float)analogRead(sqlPin) / bufferPeriod; + + bufferCounter++; + + bool encoderPinAState = (bool)digitalRead(encoderPinA); + bool encoderPinBState = (bool)digitalRead(encoderPinB); + + if((encoderPinAState != encoderPinALast) | (encoderPinBState != encoderPinBLast)) { + index = (index << 2) & 0b1100; + index = index | ((encoderPinAState << 1) + encoderPinBState); + + encoderCounter += lookup_table[index & 0b1111]; + + if(encoderCounter > 2) { + DigiKeyboard.print("z"); + encoderCounter = 0; + } + + if(encoderCounter < -2) { + DigiKeyboard.print("x"); + encoderCounter = 0; + } + + encoderPinALast = encoderPinAState; + encoderPinBLast = encoderPinBState; + } + + //check pin values + if(bufferCounter > bufferPeriod) { + //usb keepalive + DigiKeyboard.update(); + + //bool encoderPinAState = encoderPinABuffer > .5; + //bool encoderPinBState = encoderPinBBuffer > .5; + bool selectPinState = selectPinBuffer > .5; + + //check encoder pins nd transmit +// if(encoderPinAState != encoderPinALast || encoderPinBState != encoderPinBLast) { +// +// uint8_t index = (encoderPinALast << 4) + (encoderPinBLast << 3) + (encoderPinAState << 1) + encoderPinBState; +// +// //encoderDetentCounter += lookup_table[index]; +// +// if(lookup_table[index] > 0) { +// DigiKeyboard.print("z"); +// } else if (lookup_table[index] < 0) { +// DigiKeyboard.print("x"); +// } +// +// encoderPinALast = encoderPinAState; +// encoderPinBLast = encoderPinBState; +// } + + //check select pin and transmit + if(selectPinState != selectPinLast) { + selectPinLast = selectPinState; + if(selectPinLast) { + DigiKeyboard.print("n"); + } else { + DigiKeyboard.print("m"); + } + } + + //check squelch knob and transmit + if(abs(sqlPinBuffer - sqlPinLast) >= 1) { + DigiKeyboard.print(String((int)sqlPinBuffer, HEX)); + DigiKeyboard.println(""); + sqlPinLast = sqlPinBuffer; + } + + // reset buffers + encoderPinABuffer = 0; + encoderPinBBuffer = 0; + selectPinBuffer = 0; + sqlPinBuffer = 0; + bufferCounter = 0; + } +} diff --git a/digistump_firmware/libraries/readme.txt b/digistump_firmware/libraries/readme.txt new file mode 100644 index 0000000..96ce674 --- /dev/null +++ b/digistump_firmware/libraries/readme.txt @@ -0,0 +1 @@ +For information on installing libraries, see: http://www.arduino.cc/en/Guide/Libraries diff --git a/grid_pc.py b/grid_pc.py new file mode 100755 index 0000000..7ff945f --- /dev/null +++ b/grid_pc.py @@ -0,0 +1,60 @@ +#!/usr/bin/python + +import pygame +import os +from time import sleep + +#Colours +WHITE = (255,255,255) + +#os.putenv('SDL_FBDEV', '/dev/fb1') +pygame.init() +pygame.mouse.set_visible(False) +lcd = pygame.display.set_mode((320, 240)) + +font_big = pygame.font.Font(None, 100) +font_small = pygame.font.Font(None, 18) + +lcd.fill((0,0,0)) +pygame.display.update() + +top_1 = (0,0,80,40); +top_2 = (80,0,80,40); +top_3 = (160,0,80,40); +top_4 = (240,0,80,40); + +pygame.draw.rect(lcd, (100,100,100), top_1, 2) +pygame.draw.rect(lcd, (100,100,100), top_2, 2) +pygame.draw.rect(lcd, (100,100,100), top_3, 2) +pygame.draw.rect(lcd, (100,100,100), top_4, 2) + +text_surface = font_small.render('DEMOD', True, WHITE) +rect = text_surface.get_rect(center=(40, 20)) +lcd.blit(text_surface, rect) + +text_surface = font_small.render('SQLCH', True, WHITE) +rect = text_surface.get_rect(center=(120, 20)) +lcd.blit(text_surface, rect) + +text_surface = font_small.render('GAIN', True, WHITE) +rect = text_surface.get_rect(center=(200, 20)) +lcd.blit(text_surface, rect) + +text_surface = font_small.render('MENU', True, WHITE) +rect = text_surface.get_rect(center=(280, 20)) +lcd.blit(text_surface, rect) + + +sql_gain_box = (0,40,32,200) + +pygame.draw.rect(lcd, (10,10,10), sql_gain_box, 2) + +for x in range(0,6): + pygame.draw.rect(lcd, (10,10,10), (32 + x * 48,40,48,80),2) + text_surface = font_big.render('%d'%x, True, WHITE) + rect = text_surface.get_rect(center=(32 + (x + .5) * 48, 80)) + lcd.blit(text_surface, rect) + +while True: + pygame.display.update() + sleep(1)