Permalink
Browse files

dextr software v1

Project «dextr»
Darkroom exposure timer
postpersonality.ru

Software version: v0.1
Supported hardware versions: HV1a
  • Loading branch information...
postpersonality committed Jul 28, 2013
0 parents commit c09c60ce6710670ab663d163aee877eacb22c6fa
Showing with 967 additions and 0 deletions.
  1. +61 −0 Button.cpp
  2. +26 −0 Button.h
  3. +82 −0 Display.cpp
  4. +33 −0 Display.h
  5. +72 −0 Encoder.cpp
  6. +21 −0 Encoder.h
  7. +49 −0 Events.cpp
  8. +35 −0 Events.h
  9. +38 −0 Events_ids.h
  10. +51 −0 Preferences.h
  11. +97 −0 Relay.cpp
  12. +38 −0 Relay.h
  13. +364 −0 dextr.ino
@@ -0,0 +1,61 @@
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include "Preferences.h"
#include "Events_ids.h"
#include "Button.h"
//init
Button::Button( byte p, byte id=255) {
if( id==255)
id = p;
set_pin( p);
reset();
}
//reset internal variables
void Button::reset( void) {
duration = 0;
duration_prev = 0;
state_prev = false;
state = false;
//state_prev = digitalRead( pin);
//state = digitalRead( pin);
}
//refresh internal variables
void Button::refresh( void) {
state_prev = state;
state = !digitalRead( pin);
if( state==state_prev)
duration++;
else {
duration_prev = duration;
duration = 0;
}
}
//get current state
byte Button::get_event( void) {
if( state) { //press
if( !state_prev) {
return EVENT_BUTTON_DOWN;
}
if( duration && !(duration % BUTTON_LONG_PRESS) ) {
if( duration != BUTTON_LONG_PRESS)
duration_prev = 0;
//duration = 0;
return EVENT_BUTTON_LONG;
}
} else { //release
if( state_prev!=state) {
return EVENT_BUTTON_UP;
}
}
return 0; //no event
}
//set pin, init HW
void Button::set_pin( byte p) {
pin = p;
pinMode( pin, INPUT);
digitalWrite( pin, HIGH);
}
@@ -0,0 +1,26 @@
#ifndef Button_h
#define Button_h
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
class Button {
public:
unsigned long duration; //time from previous state change
unsigned long duration_prev; //time before previous state change
boolean state_prev; //previous state
boolean state; //current state
byte pin; //arduino pin number
byte id; //arduino pin number
//methods
Button( byte, byte);
void refresh( void);
void reset( void);
byte get_event( void);
void set_pin( byte);
};
#endif
@@ -0,0 +1,82 @@
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include <LiquidCrystal_I2C.h>
#include "Preferences.h"
#include "Events_ids.h"
#include "Display.h"
//init
Display::Display( LiquidCrystal_I2C *lcd_pt, unsigned short part_sz) {
lcd_ptr = lcd_pt;
lcd_cols = lcd_ptr->get_cols();
lcd_rows = lcd_ptr->get_rows();
buff_size = lcd_rows*lcd_cols;
part_size = part_sz;
buffer = new char[buff_size+1];
reset();
}
//deinit
Display::~Display( void) {
delete[] buffer;
}
//reset internal variables
void Display::reset( void) {
void reset_buffer( void);
buff_offset = 0;
is_ready = true;
}
//fill buffer with spaces
void Display::reset_buffer( void) {
memset( buffer, ' ', buff_size);
buffer[buff_size] = 0;
}
void Display::prepare( char *str) {
if( str == NULL) {
str = buffer;
}
buff_str_len = strlen( str);
buff_str_len = buff_str_len > buff_size ? buff_size : buff_str_len; //min( len, buff_size)
if( str != buffer) {
reset_buffer();
memcpy( buffer, str, buff_str_len);
buffer[buff_str_len] = 0;
}
is_ready = false;
buff_offset = 0;
}
void Display::prepare_row( char *str, unsigned short row) {
unsigned short len = strlen( str);
len = len>lcd_cols ? lcd_cols : len;
//memset( buffer + row * lcd_cols, ' ', lcd_cols);
memcpy( buffer + ((row+1) * lcd_cols - len), str, len);
//buffer[buff_size] = 0;
}
void Display::show( char *str) {
if( is_ready) { //ready to load buffer
prepare( str);
}
//show next part
buff_offset += part_size;
char tmp = buffer[buff_offset];
buffer[buff_offset] = 0;
buff_offset -= part_size;
lcd_ptr->setCursor( buff_offset % lcd_cols, buff_offset / lcd_cols);
lcd_ptr->print( buffer + buff_offset);
buff_offset += part_size;
buffer[buff_offset] = tmp;
if( buff_offset >= buff_str_len) { //check completeness of show-task
is_ready = true;
}
}
@@ -0,0 +1,33 @@
#ifndef Display_h
#define Display_h
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include <LiquidCrystal_I2C.h>
class Display {
private:
LiquidCrystal_I2C *lcd_ptr; //lcd io instance pointer
unsigned short lcd_cols;
unsigned short lcd_rows;
unsigned short buff_size;
unsigned short part_size;
unsigned short buff_str_len;
unsigned short buff_offset;
public:
char *buffer; //internal buffer to show
boolean is_ready; //current state
//methods
Display( LiquidCrystal_I2C *lcd_pt, unsigned short part_sz);
~Display( void);
void reset( void);
void reset_buffer( void);
void prepare( char *str = NULL);
void prepare_row( char *str, unsigned short row);
void show( char *str = NULL);
};
#endif
@@ -0,0 +1,72 @@
//////////// ENCODER
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include "Preferences.h"
#include "Events_ids.h"
#include "Encoder.h"
short int encoder_val = 0;
byte waitforpin = 2;
short int count = 0;
boolean pinAst;
boolean pinBst;
void doEncoder( byte pin){
//!!! HARD PROGRAMMED PORT/PINS !!! BEWARE
boolean pinA = (PIND & (1 << PIN_ENCODER_A)) >> PIN_ENCODER_A;
boolean pinB = (PIND & (1 << PIN_ENCODER_B)) >> PIN_ENCODER_B;
short int dir = 0;
boolean pinV;
pinV = !pinBst;
pinV = (pinV^pinAst)|pinV;
pinV = !pinV;
if( waitforpin==2) {
pinAst = pin ? !pinA : pinA;
pinAst = !pin ? !pinB : pinB;
}
if( pin) { //A
if( pinA!=pinAst) {
pinAst = pinA;
waitforpin = !pin;
if( pinA==pinBst)
dir--;
else
dir++;
}
} else { //B
if( pinB!=pinBst) {
pinBst = pinB;
waitforpin = !pin;
if( pinB==pinAst)
dir++;
else
dir--;
}
}
count += dir;
if( count==4) {
count=0;
encoder_val--;
}
if( count==-4) {
count=0;
encoder_val++;
}
}
// Interrupt on A changing state
void doEncoderA( void){
doEncoder( 0);
}
void doEncoderB( void){
doEncoder( 1);
}
@@ -0,0 +1,21 @@
//////////// ENCODER
#ifndef Encoder_h
#define Encoder_h
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
extern short int encoder_val;
extern byte waitforpin;
extern short int count;
extern boolean pinAst;
extern boolean pinBst;
void doEncoder( byte pin);
void doEncoderA( void);
void doEncoderB( void);
#endif
@@ -0,0 +1,49 @@
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include "Preferences.h"
#include "Events_ids.h"
#include "Events.h"
//////////////////// EVENTS CLASS ////////////////////////
Event::Event( unsigned short t, unsigned short d=0) {
type = t;
data = d;
}
//reset all internals
Events::Events( void) {
first = 0;
count = 0;
overflow = false;
}
//add to the end and increase count
void Events::add( unsigned short type, unsigned short data=0) {
if( count==EVENTS_MAX) {
overflow = true;
return;
}
queue[(first + count) % EVENTS_MAX] = new Event( type, data);
count++;
}
//get first event
Event* Events::get( void) {
return queue[first];
}
//and move pointer to next
void Events::next( void) {
if( count) {
delete queue[first];
first = (first + 1) % EVENTS_MAX;
count--;
}
}
@@ -0,0 +1,35 @@
#ifndef Events_h
#define Events_h
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
class Event {
public:
unsigned short type; //event type
unsigned short data; //additional event data
//methods
Event( unsigned short type, unsigned short data);
};
class Events {
public:
Event* queue[EVENTS_MAX];
unsigned short first;
unsigned short count;
boolean overflow;
//methods
Events( void); //reset all internals
void add( unsigned short type, unsigned short data); //add to the end and increase count
Event* get( void); //get first event
void next( void); //and move pointer to next
};
#endif
Oops, something went wrong.

0 comments on commit c09c60c

Please sign in to comment.