a library for laser printer based on https://os.mbed.com/users/xmwmx/code/wmx_laser_copy/
Dependents: Example_for_learning_easy
laser.cpp@2:35089485bc89, 2018-10-16 (annotated)
- Committer:
- Dennis_Yu
- Date:
- Tue Oct 16 07:40:46 2018 +0000
- Revision:
- 2:35089485bc89
- Parent:
- 1:9cfdf3c372b5
all function added
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Dennis_Yu | 0:5e881997f4b5 | 1 | #include "laser.h" |
Dennis_Yu | 0:5e881997f4b5 | 2 | |
Dennis_Yu | 2:35089485bc89 | 3 | laser::laser(Esp8266 & iClient, SDFileSystem & iSD) |
Dennis_Yu | 2:35089485bc89 | 4 | :client(iClient), sd(iSD), |
Dennis_Yu | 2:35089485bc89 | 5 | switch_GS(PC_15), |
Dennis_Yu | 2:35089485bc89 | 6 | switch_pos1(PC_8), //P25 |
Dennis_Yu | 2:35089485bc89 | 7 | switch_pos2(PA_6), //P26 |
Dennis_Yu | 2:35089485bc89 | 8 | switch_pos3(PA_11), //P27 |
Dennis_Yu | 2:35089485bc89 | 9 | fp_drawing(NULL), |
Dennis_Yu | 2:35089485bc89 | 10 | status(0), Endoffile(0) |
Dennis_Yu | 2:35089485bc89 | 11 | { |
Dennis_Yu | 2:35089485bc89 | 12 | Lenoflattice = 1 ;//mm 取1mm为xy单元 unit_xy/Lenoflattice=100 |
Dennis_Yu | 2:35089485bc89 | 13 | unit_xy = 100; //单位长度(xy移动一格)对应unit_xy转 大约10cm/3圈/9600step 约96mm/9600step=0.1mm/10step |
Dennis_Yu | 2:35089485bc89 | 14 | unit_x = 103; //单位长度(xy移动一格)对应unit_x转 大约10cm/3圈/9600step 约96mm/9600step=0.1mm/10step |
Dennis_Yu | 2:35089485bc89 | 15 | unit_y = 103; //单位长度(xy移动一格)对应unit_y转 大约10cm/3圈/9600step 约96mm/9600step=0.1mm/10step |
Dennis_Yu | 2:35089485bc89 | 16 | ///Ticker ticker_step; |
Dennis_Yu | 2:35089485bc89 | 17 | step_halfperiod = 0.0001;//0.0002; |
Dennis_Yu | 2:35089485bc89 | 18 | dir_x = 1;///调试时调整 |
Dennis_Yu | 2:35089485bc89 | 19 | dir_y = 1;///调试时调整 |
Dennis_Yu | 2:35089485bc89 | 20 | dot_max = 30; //灰度最大值对应多少次激光点击 |
Dennis_Yu | 2:35089485bc89 | 21 | dot_last = 0.005; //每次激光点击持续多久 |
Dennis_Yu | 2:35089485bc89 | 22 | H_max = 255; //灰度值最大值 |
Dennis_Yu | 2:35089485bc89 | 23 | k = 1.0; //缩放比例 |
Dennis_Yu | 2:35089485bc89 | 24 | y=0;//图片y边长 |
Dennis_Yu | 2:35089485bc89 | 25 | x=0;//图片x边长 |
Dennis_Yu | 2:35089485bc89 | 26 | key=0;//判断变量 |
Dennis_Yu | 2:35089485bc89 | 27 | dot=0;//点计数 |
Dennis_Yu | 2:35089485bc89 | 28 | flag=0; |
Dennis_Yu | 2:35089485bc89 | 29 | } |
Dennis_Yu | 2:35089485bc89 | 30 | |
Dennis_Yu | 2:35089485bc89 | 31 | void laser::moveto(float x, float y) |
Dennis_Yu | 2:35089485bc89 | 32 | { |
Dennis_Yu | 2:35089485bc89 | 33 | rotate(0, (x - now_x)*unit_xy); |
Dennis_Yu | 2:35089485bc89 | 34 | rotate(1, (y - now_y)*unit_xy); |
Dennis_Yu | 2:35089485bc89 | 35 | now_x = x; |
Dennis_Yu | 2:35089485bc89 | 36 | now_y = y; |
Dennis_Yu | 2:35089485bc89 | 37 | } |
Dennis_Yu | 2:35089485bc89 | 38 | |
Dennis_Yu | 2:35089485bc89 | 39 | void laser::rotate(int id, int pix) //id= 0--x,1--y pix=3200为一圈 |
Dennis_Yu | 1:9cfdf3c372b5 | 40 | { |
Dennis_Yu | 2:35089485bc89 | 41 | if (pix >= 0) { |
Dennis_Yu | 2:35089485bc89 | 42 | dir[0] = dir_x; |
Dennis_Yu | 2:35089485bc89 | 43 | dir[1] = dir_y; |
Dennis_Yu | 2:35089485bc89 | 44 | } else { |
Dennis_Yu | 2:35089485bc89 | 45 | pix = -pix; |
Dennis_Yu | 2:35089485bc89 | 46 | dir[0] = 1 - dir_x; |
Dennis_Yu | 2:35089485bc89 | 47 | dir[1] = 1 - dir_y; |
Dennis_Yu | 2:35089485bc89 | 48 | } |
Dennis_Yu | 2:35089485bc89 | 49 | for (int i = 0; i < pix; i++) { |
Dennis_Yu | 2:35089485bc89 | 50 | step[id] = 1; |
Dennis_Yu | 2:35089485bc89 | 51 | wait(step_halfperiod); |
Dennis_Yu | 2:35089485bc89 | 52 | step[id] = 0; |
Dennis_Yu | 2:35089485bc89 | 53 | wait(step_halfperiod); |
Dennis_Yu | 2:35089485bc89 | 54 | } |
Dennis_Yu | 1:9cfdf3c372b5 | 55 | } |
Dennis_Yu | 2:35089485bc89 | 56 | |
Dennis_Yu | 2:35089485bc89 | 57 | void laser::markdot(float value) |
Dennis_Yu | 2:35089485bc89 | 58 | { |
Dennis_Yu | 2:35089485bc89 | 59 | //switch_GS=1; |
Dennis_Yu | 2:35089485bc89 | 60 | int Ndot = floor(value * dot_max / H_max); |
Dennis_Yu | 2:35089485bc89 | 61 | for (int i = 0; i < Ndot; i++) { |
Dennis_Yu | 2:35089485bc89 | 62 | switch_GS = 1; |
Dennis_Yu | 2:35089485bc89 | 63 | wait(dot_last); |
Dennis_Yu | 2:35089485bc89 | 64 | switch_GS = 0; |
Dennis_Yu | 2:35089485bc89 | 65 | } |
Dennis_Yu | 2:35089485bc89 | 66 | } |
Dennis_Yu | 2:35089485bc89 | 67 | |
Dennis_Yu | 2:35089485bc89 | 68 | void laser::draw() |
Dennis_Yu | 2:35089485bc89 | 69 | { |
Dennis_Yu | 2:35089485bc89 | 70 | unit_xy=unit_xy*k; |
Dennis_Yu | 2:35089485bc89 | 71 | float x, y, v; |
Dennis_Yu | 2:35089485bc89 | 72 | fp_drawing = fopen("/sd/write.txt", "r"); |
Dennis_Yu | 2:35089485bc89 | 73 | ser2usb.printf("reading\r\n"); |
Dennis_Yu | 2:35089485bc89 | 74 | for (int i = 0; fscanf(fp_drawing, "(%f,%f,%f)", &x, &y, &v) == 3; i++) |
Dennis_Yu | 2:35089485bc89 | 75 | { |
Dennis_Yu | 2:35089485bc89 | 76 | ser2usb.printf("(%f,%f,%f)|%d|%d\r\n", x, y, v,i,dot); |
Dennis_Yu | 2:35089485bc89 | 77 | moveto(x, y); |
Dennis_Yu | 2:35089485bc89 | 78 | markdot(v); |
Dennis_Yu | 2:35089485bc89 | 79 | } |
Dennis_Yu | 2:35089485bc89 | 80 | fclose(fp_drawing); |
Dennis_Yu | 2:35089485bc89 | 81 | } |