Merge branch 'emulator' of github.com:t-moe/discoverpixy into emulator

This commit is contained in:
id101010
2015-04-27 18:43:08 +02:00
29 changed files with 413 additions and 113 deletions

View File

@@ -1,14 +1,47 @@
#include "app.h"
#include "tft.h"
#include "system.h"
#include "touch.h"
#include "pixy.h"
#include <stdio.h>
#include <stdlib.h>
bool pixy_connected = false;
TOUCH_AREA_STRUCT a1;
void touchCB(void* touchArea, TOUCH_ACTION triggeredAction) {
switch(triggeredAction) {
case PEN_DOWN:
printf("action PEN_DOWN\n");
break;
case PEN_UP:
printf("action PEN_UP\n");
break;
case PEN_MOVE:
printf("action PEN_MOVE\n");
break;
case PEN_ENTER:
printf("action PEN_ENTER\n");
break;
case PEN_LEAVE:
printf("action PEN_LEAVE\n");
break;
default:
printf("action %s\n",triggeredAction);
break;
}
}
void app_init() {
system_init();
tft_init();
touch_init();
pixy_init();
pixy_connected = (pixy_init()==0); //try to connect to pixy
//only testwise
tft_clear(WHITE);
@@ -18,22 +51,53 @@ void app_init() {
tft_fill_rectangle(100,215,200,225,GREEN);
tft_draw_line(10,215,310,225,RGB(0xFF,0,0xFF));
tft_draw_circle(10,10,100, RED);
a1.hookedActions = PEN_DOWN | PEN_UP | PEN_MOVE | PEN_ENTER | PEN_LEAVE;
a1.x1 = 30;
a1.y1 = 30;
a1.x2 = 100;
a1.y2 = 60;
a1.callback = touchCB;
touch_register_area(&a1);
tft_draw_rectangle(30,30,100,60,BLUE);
}
void pixy_led_test();
void pixy_frame_test();
int pixy_led_test();
int pixy_frame_test();
//app event loop
void app_process() {
pixy_service(); //send/receive event data
system_process(); //Let the system handle it's pending events
//Code for tests see below
pixy_led_test();
pixy_frame_test();
//system_delay(500);
//Note: The only way to detect that pixy has been disconnected is if a command fails. There's no pixy_is_connected method yet :'(
if(!pixy_connected) { //Pixy not connected
pixy_close(); //Ensure that all pixy resources are freed (failsafe)
if(pixy_init()==0) { //try to connect to pixy
pixy_connected=true;
}
}
if(pixy_connected) {
pixy_service(); //Send/receive event data from/to pixy failed
//Code for tests see below
if(pixy_led_test()!=0) {
pixy_connected=false;
}
/*if(pixy_frame_test()!=0) {
pixy_connected=false;
}*/
system_delay(500);
}
}
@@ -43,7 +107,7 @@ int colorind;
const uint32_t colors [] = {0xFF0000, 0x00FF00,0x0000FF,0xFFFF00,0x00FFFF,0xFF00FF,0xFFFFFF,0x000000};
const int num_colors = sizeof(colors)/sizeof(uint32_t);
void pixy_led_test() {
int pixy_led_test() {
if(colorind==0) {
pixy_led_set_max_current(5);
}
@@ -52,6 +116,12 @@ void pixy_led_test() {
int return_value;
return_value = pixy_command("led_set", INT32(colors[colorind++]), END_OUT_ARGS, &response, END_IN_ARGS);
colorind%=num_colors;
if(return_value!=0) {
colorind=0; //reset color ind, to start at zero when plugging pixy in again
}
return return_value;
}
//----------------------------------------------------------------------------------------------------------------------------
@@ -59,7 +129,7 @@ void pixy_led_test() {
int renderBA81(uint8_t renderFlags, uint16_t width, uint16_t height, uint32_t frameLen, uint8_t *frame);
void pixy_frame_test() {
int pixy_frame_test() {
uint8_t* videodata;
int32_t response;
@@ -86,15 +156,16 @@ void pixy_frame_test() {
&videodata, // pointer to mem address for returned frame
END_IN_ARGS);
if(return_value==0) {
return_value = renderBA81(renderflags,xwidth,ywidth,size,videodata);
}
}
return return_value;
}
inline void interpolateBayer(uint16_t width, uint16_t x, uint16_t y, uint8_t *pixel, uint8_t* r, uint8_t* g, uint8_t* b)
void interpolateBayer(uint16_t width, uint16_t x, uint16_t y, uint8_t *pixel, uint8_t* r, uint8_t* g, uint8_t* b)
{
if (y&1)
{
@@ -143,24 +214,41 @@ int renderBA81(uint8_t renderFlags, uint16_t width, uint16_t height, uint32_t fr
// don't render top and bottom rows, and left and rightmost columns because of color
// interpolation
//uint32_t decodedimage[(width-2)*(height-2)];
uint16_t decodedimage[(width-2)*(height-2)];
uint16_t* line = decodedimage;
for (y=1; y<height-1; y++)
{
//line = (unsigned int *)img.scanLine(y-1);
frame++;
for (x=1; x<width-1; x++, frame++)
{
interpolateBayer(width, x, y, frame, &r, &g, &b);
//*line++ = (0xff<<24) | (r<<16) | (g<<8) | (b<<0);
*line++ = RGB(r,g,b);
}
frame++;
}
tft_draw_bitmap_unscaled(0,0,width-2,height-2,decodedimage);
uint16_t* decodedimage = malloc(sizeof(uint16_t)*(width-2)*(height-2));
if(decodedimage==NULL) { //not enough free space to decode image in memory
//decode & render image pixel by pixel
uint16_t* line = decodedimage;
for (y=1; y<height-1; y++)
{
frame++;
for (x=1; x<width-1; x++, frame++)
{
interpolateBayer(width, x, y, frame, &r, &g, &b);
tft_draw_pixel(x-1,y-1,RGB(r,g,b));
}
frame++;
}
} else { //enough space
uint16_t* line = decodedimage;
for (y=1; y<height-1; y++)
{
//line = (unsigned int *)img.scanLine(y-1);
frame++;
for (x=1; x<width-1; x++, frame++)
{
interpolateBayer(width, x, y, frame, &r, &g, &b);
//*line++ = (0xff<<24) | (r<<16) | (g<<8) | (b<<0);
*line++ = RGB(r,g,b);
}
frame++;
}
tft_draw_bitmap_unscaled(0,0,width-2,height-2,decodedimage);
free(decodedimage);
}
return 0;
}

View File

@@ -3,4 +3,5 @@
bool ll_system_init();
void ll_system_delay(uint32_t msec);
void ll_system_process();
void ll_system_toggle_led();

View File

@@ -0,0 +1,6 @@
#include <stdint.h>
#include <stdbool.h>
bool ll_touch_init();

View File

@@ -6,10 +6,14 @@ bool system_init() {
return ll_system_init();
}
void system_delay(uint32_t msec) {
ll_system_delay(msec);
}
void system_process() {
ll_system_process();
}
void system_toggle_led() {
ll_system_toggle_led();
}

View File

@@ -3,3 +3,5 @@
bool system_init();
void system_delay(uint32_t msec);
void system_process();
void system_toggle_led();

129
common/touch/touch.c Normal file
View File

@@ -0,0 +1,129 @@
#include "touch.h"
#include "ll_touch.h"
#include <stdio.h>
#define NUM_AREAS 50 //Number of Structs Reserved in Memory for TouchAreas (e.g Buttons)
TOUCH_AREA_STRUCT* areas[NUM_AREAS] = {NULL};
volatile int touchY=0; //Last Y Coordinate in pixels
volatile int touchX=0; //Last X Coordinate in pixels
volatile TOUCH_STATE oldState=TOUCH_UP;
bool touch_init() {
return ll_touch_init();
}
bool touch_add_raw_event(uint16_t x, uint16_t y, TOUCH_STATE state) {
bool penDown = (state==TOUCH_DOWN);
bool oldPenDown = (oldState==TOUCH_DOWN);
oldState=state;
uint16_t touchX = x;
uint16_t touchY = y;
if(penDown)
{
// tftDrawPixel(touchX,touchY,WHITE);
if(!oldPenDown) //First Touch
{
for(int z=0; z < NUM_AREAS; z++) // For every touch area
{
if(areas[z]!=NULL && touchX >= areas[z]->x1 && touchX <= areas[z]->x2 && touchY >= areas[z]->y1 && touchY <= areas[z]->y2 )
{
areas[z]->flags=1; //PenInside=1
if(areas[z]->hookedActions & PEN_DOWN)
areas[z]->callback(areas[z],PEN_DOWN);
}
}
}
else //Second, Third
{
for(int z=0; z < NUM_AREAS; z++) // For every touch area
{
if(areas[z]!=NULL )
{
if(touchX >= areas[z]->x1 && touchX <= areas[z]->x2 && touchY >= areas[z]->y1 && touchY <= areas[z]->y2)
{
if(areas[z]->flags==0) //PenInside ==0
{
areas[z]->flags=1; //PenInside=1
if(areas[z]->hookedActions & PEN_ENTER)
areas[z]->callback(areas[z],PEN_ENTER);
}
}
else if(areas[z]->flags) //PenInside==1
{
areas[z]->flags=0; //PenInside=0
if(areas[z]->hookedActions & PEN_LEAVE)
areas[z]->callback(areas[z],PEN_LEAVE);
}
}
}
}
for(int z=0; z < NUM_AREAS; z++) // For every touch area
{
if(areas[z]!=NULL && areas[z]->hookedActions&PEN_MOVE)
{
if(touchX >= areas[z]->x1 && touchX <= areas[z]->x2 && touchY >= areas[z]->y1 && touchY <= areas[z]->y2)
{
areas[z]->callback(areas[z],PEN_MOVE);
}
}
}
}
else
{
if(oldPenDown) //Was the pen ever down (or was it a too short touch)
{
for(int z=0; z < NUM_AREAS; z++) // For every touch area
{
if(areas[z]!=NULL && touchX >= areas[z]->x1 && touchX <= areas[z]->x2 && touchY >= areas[z]->y1 && touchY <= areas[z]->y2 )
{
areas[z]->flags=0; //PenInside = 0;
if(areas[z]->hookedActions & PEN_UP)
areas[z]->callback(areas[z],PEN_UP);
}
}
}
touchX=0xFFFF;
touchY=0xFFFF;
}
return true;
}
bool touch_have_empty(unsigned char num)
{
for(unsigned char i=0; i<NUM_AREAS; i++)
{
if(areas[i]==NULL) num--;
if(num==0) return true;
}
return false;
}
bool touch_register_area(TOUCH_AREA_STRUCT* area) //Registers an Area (fill Struct first). Return false if no more Space in the Pointertable (-->Change NUM_AREAS).
{
for(unsigned char i=0; i<NUM_AREAS; i++)
{
if(areas[i]==NULL)
{
area->flags=0;
areas[i]=area;
return true;
}
}
return false;
}
void touch_unregister_area(TOUCH_AREA_STRUCT* area)//Unregisters an Area
{
for(unsigned char i=0; i<NUM_AREAS; i++)
{
if(areas[i]==area)
{
areas[i]=NULL;
break;
}
}
}

View File

@@ -0,0 +1,27 @@
#include<stdbool.h>
#include<stdint.h>
typedef enum {TOUCH_UP,TOUCH_DOWN} TOUCH_STATE ;
typedef enum {NONE=0x00,PEN_DOWN=0x01, PEN_UP=0x02, PEN_ENTER=0x04, PEN_LEAVE=0x08,PEN_MOVE=0x10} TOUCH_ACTION;
typedef void (*TOUCH_CALLBACK)(void* touchArea, TOUCH_ACTION triggeredAction);
typedef struct {
TOUCH_ACTION hookedActions; //Actions to listen to
unsigned int x1; //Top Left X Coordiate of Area
unsigned int y1; //Top Left Y Coordiate of Area
unsigned int x2; //Bottom Right X Coordiate of Area
unsigned int y2; //Bottom Right Y Coordiate of Area
TOUCH_CALLBACK callback; //Callback
uint8_t flags; //Internal Used, don't change
} TOUCH_AREA_STRUCT;
bool touch_init();
bool touch_add_raw_event(uint16_t x, uint16_t y,TOUCH_STATE state);
bool touch_have_empty(unsigned char num);
bool touch_register_area(TOUCH_AREA_STRUCT* area);
void touch_unregister_area(TOUCH_AREA_STRUCT* area);