int main() { int rfbargc = 0; char **rfbargv = 0; int bpp = 4; rfbScreenInfoPtr server = rfbGetScreen(&rfbargc, rfbargv, FRAMEBUFFER_WIDTH, FRAMEBUFFER_HEIGHT, 8, 3, bpp); fb = new unsigned char[FRAMEBUFFER_WIDTH * FRAMEBUFFER_HEIGHT * bpp]; server->frameBuffer = (char *)fb; server->kbdAddEvent = handleKey; server->ptrAddEvent = handlePointer; rfbInitServer(server); int file; file = open(devname, O_RDWR); if (file < 0) { perror("open"); exit(EXIT_FAILURE); } int addr = ADXL345_ADDRESS; /* The I2C address */ if (ioctl(file, I2C_SLAVE, addr) < 0) { perror("ioctl"); exit(EXIT_FAILURE); } uint16_t id = read_u16(file, 0x00); printf("id is 0x%04X\n", id); ADXL345_init(file); int tilt_was_valid = false; while(1) { int16_t xdata = read_u16_le(file, ADXL345_DATAX0); int16_t ydata = read_u16_le(file, ADXL345_DATAY0); int16_t zdata = read_u16_le(file, ADXL345_DATAZ0); float xg = xdata / 256.0; float yg = ydata / 256.0; float zg = zdata / 256.0; float theta_y = -atan2(yg, zg); float theta_x = atan2(xg, zg); float y_angle_center = 40.0 / 180.0 * M_PI; float y_angle_range = 25.0 / 180.0 * M_PI; float x_angle_center = 0.0 / 180.0 * M_PI; float x_angle_range = 25.0 / 180.0 * M_PI; int tilt_is_valid = (theta_y > y_angle_center - y_angle_range) && (theta_y < y_angle_center + y_angle_range) && (theta_x > x_angle_center - x_angle_range) && (theta_x < x_angle_center + x_angle_range); if(tilt_is_valid != tilt_was_valid) { if(tilt_is_valid) printf("TILT_VALID\n"); else printf("TILT_INVALID\n"); tilt_was_valid = tilt_is_valid; } if(tilt_is_valid) { float tilt_x_valuator = (theta_x - x_angle_center) / x_angle_range; float tilt_y_valuator = (theta_y - y_angle_center) / y_angle_range; // printf(" %f, %f\n", tilt_x_valuator, tilt_y_valuator); draw_new_circle(server, (tilt_x_valuator + 1) * .5 * FRAMEBUFFER_WIDTH, (tilt_y_valuator + 1) * .5 * FRAMEBUFFER_HEIGHT, 1.0); } // usleep(100000); rfbProcessEvents(server, 10000); } }
void test_read_u16_le() { unsigned char buf[2] = { 0x03, 0xd9 }; uint16_t n = read_u16_le(buf); assert(0xd903 == n); }