123
Fork of LG by
Diff: DeviceTSS.c
- Revision:
- 174:0f86eedd511c
diff -r 7f938afb0447 -r 0f86eedd511c DeviceTSS.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DeviceTSS.c Sun Jun 26 10:16:39 2016 +0000 @@ -0,0 +1,156 @@ +#include "Device.h" +extern Device device; + +void InitTSSDefaultSettings(void){ + device.tss.temperature.settings.transfer.points = 16; + device.tss.temperature.settings.transfer.raw[0] = -0x8e0ba2e; //-2272.727272727273 + device.tss.temperature.settings.transfer.celsius[0] = -0x3c0000; //-60.0 + device.tss.temperature.settings.transfer.raw[1] = -0x6645d17; //-1636.3636363636365 + device.tss.temperature.settings.transfer.celsius[1] = -0x320000; //-50.0 + device.tss.temperature.settings.transfer.raw[2] = -0x3e80000; //-1000.0000000000002 + device.tss.temperature.settings.transfer.celsius[2] = -0x280000; //-40.0 + device.tss.temperature.settings.transfer.raw[3] = -0x16ba2e8; //-363.63636363636397 + device.tss.temperature.settings.transfer.celsius[3] = -0x1e0000; //-30.0 + device.tss.temperature.settings.transfer.raw[4] = 0x110ba2e; //272.7272727272725 + device.tss.temperature.settings.transfer.celsius[4] = -0x140000; //-20.0 + device.tss.temperature.settings.transfer.raw[5] = 0x38d1745; //909.090909090909 + device.tss.temperature.settings.transfer.celsius[5] = -0xa0000; //-10.0 + device.tss.temperature.settings.transfer.raw[6] = 0x609745d; //1545.454545454545 + device.tss.temperature.settings.transfer.celsius[6] = 0x0; //0.0 + device.tss.temperature.settings.transfer.raw[7] = 0x885d174; //2181.818181818182 + device.tss.temperature.settings.transfer.celsius[7] = 0xa0000; //10.0 + device.tss.temperature.settings.transfer.raw[8] = 0xb022e8b; //2818.181818181818 + device.tss.temperature.settings.transfer.celsius[8] = 0x140000; //20.0 + device.tss.temperature.settings.transfer.raw[9] = 0xd7e8ba2; //3454.545454545454 + device.tss.temperature.settings.transfer.celsius[9] = 0x1e0000; //30.0 + device.tss.temperature.settings.transfer.raw[10] = 0xffae8ba; //4090.909090909091 + device.tss.temperature.settings.transfer.celsius[10] = 0x280000; //40.0 + device.tss.temperature.settings.transfer.raw[11] = 0x127745d1; //4727.272727272727 + device.tss.temperature.settings.transfer.celsius[11] = 0x320000; //50.0 + device.tss.temperature.settings.transfer.raw[12] = 0x14f3a2e8; //5363.636363636363 + device.tss.temperature.settings.transfer.celsius[12] = 0x3c0000; //60.0 + device.tss.temperature.settings.transfer.raw[13] = 0x176fffff; //5999.999999999999 + device.tss.temperature.settings.transfer.celsius[13] = 0x460000; //70.0 + device.tss.temperature.settings.transfer.raw[14] = 0x19ec5d17; //6636.363636363637 + device.tss.temperature.settings.transfer.celsius[14] = 0x500000; //80.0 + device.tss.temperature.settings.transfer.raw[15] = 0x1c68ba2e; //7272.727272727273 + device.tss.temperature.settings.transfer.celsius[15] = 0x5a0000; //90.0 + + //TODO: actual values + device.tss.gradient.settings.transfer.points = 16; + device.tss.gradient.settings.transfer.raw[0] = -0x640000; //-100.0 + device.tss.gradient.settings.transfer.celsius[0] = -0xa0000; //-10.0 + device.tss.gradient.settings.transfer.raw[1] = -0x56aaaa; //-86.66666666666667 + device.tss.gradient.settings.transfer.celsius[1] = -0x8aaaa; //-8.666666666666666 + device.tss.gradient.settings.transfer.raw[2] = -0x495555; //-73.33333333333333 + device.tss.gradient.settings.transfer.celsius[2] = -0x75555; //-7.333333333333334 + device.tss.gradient.settings.transfer.raw[3] = -0x3c0000; //-60.0 + device.tss.gradient.settings.transfer.celsius[3] = -0x60000; //-6.0 + device.tss.gradient.settings.transfer.raw[4] = -0x2eaaaa; //-46.666666666666664 + device.tss.gradient.settings.transfer.celsius[4] = -0x4aaaa; //-4.666666666666667 + device.tss.gradient.settings.transfer.raw[5] = -0x215555; //-33.33333333333333 + device.tss.gradient.settings.transfer.celsius[5] = -0x35555; //-3.333333333333334 + device.tss.gradient.settings.transfer.raw[6] = -0x140000; //-20.0 + device.tss.gradient.settings.transfer.celsius[6] = -0x20000; //-2.0 + device.tss.gradient.settings.transfer.raw[7] = -0x6aaaa; //-6.666666666666657 + device.tss.gradient.settings.transfer.celsius[7] = -0xaaaa; //-0.6666666666666679 + device.tss.gradient.settings.transfer.raw[8] = 0x6aaaa; //6.666666666666671 + device.tss.gradient.settings.transfer.celsius[8] = 0xaaaa; //0.6666666666666661 + device.tss.gradient.settings.transfer.raw[9] = 0x140000; //20.0 + device.tss.gradient.settings.transfer.celsius[9] = 0x20000; //2.0 + device.tss.gradient.settings.transfer.raw[10] = 0x215555; //33.33333333333334 + device.tss.gradient.settings.transfer.celsius[10] = 0x35555; //3.333333333333332 + device.tss.gradient.settings.transfer.raw[11] = 0x2eaaaa; //46.666666666666686 + device.tss.gradient.settings.transfer.celsius[11] = 0x4aaaa; //4.666666666666666 + device.tss.gradient.settings.transfer.raw[12] = 0x3c0000; //60.0 + device.tss.gradient.settings.transfer.celsius[12] = 0x60000; //6.0 + device.tss.gradient.settings.transfer.raw[13] = 0x495555; //73.33333333333334 + device.tss.gradient.settings.transfer.celsius[13] = 0x75555; //7.333333333333332 + device.tss.gradient.settings.transfer.raw[14] = 0x56aaaa; //86.66666666666669 + device.tss.gradient.settings.transfer.celsius[14] = 0x8aaaa; //8.666666666666664 + device.tss.gradient.settings.transfer.raw[15] = 0x640000; //100.0 + device.tss.gradient.settings.transfer.celsius[15] = 0xa0000; //10.0 +} + +void InitTSSState(void) { + device.tss.temperature.state.sum = 0; + device.tss.gradient.state.sum = 0; + device.tss.temperature.state.raw = 0; + device.tss.temperature.state.celsius = tssTemperature(device.tss.temperature.state.raw); + device.tss.gradient.state.raw = 0; + device.tss.gradient.state.celsius = tssGradient(device.tss.gradient.state.raw); +} + +void DeviceStartTSS(void) { +} + +int32_t tssInterpolate(int32_t a,int32_t a1,int32_t a2,int32_t b1,int32_t b2) { + int32_t ma, mb; + while (1) { + if (a1 == a) return b1; + if (a2 == a) return b2; + + ma = (a1 + a2) >> 1; + mb = (b1 + b2) >> 1; + + if (a < ma) { + if (a2 == ma) return mb; + if (b1 == mb) return mb; + a2 = ma; b2 = mb; + } else if (a > ma) { + if (a1 == ma) return mb; + if (b2 == mb) return mb; + a1 = ma; b1 = mb; + } else return mb; + } +} + +int32_t tssTemperature(uint32_t raw) { + int32_t r1, r2; + for (uint8_t i = 1; i < device.tss.temperature.settings.transfer.points; i++) { + r1 = device.tss.temperature.settings.transfer.raw[i - 1]; + if (raw < r1) { + return device.tss.temperature.settings.transfer.celsius[i - 1]; + } + + r2 = device.tss.temperature.settings.transfer.raw[i]; + if (raw < r2) { + int32_t c1 = device.tss.temperature.settings.transfer.celsius[i - 1]; + int32_t c2 = device.tss.temperature.settings.transfer.celsius[i]; + return tssInterpolate(raw, r1, r2, c1, c2); + } + } + return device.tss.temperature.settings.transfer.celsius[device.tss.temperature.settings.transfer.points - 1]; +} + +int32_t tssGradient(uint32_t raw) { + int32_t r1, r2; + for (uint8_t i = 1; i < device.tss.gradient.settings.transfer.points; i++) { + r1 = device.tss.gradient.settings.transfer.raw[i - 1]; + if (raw < r1) { + return device.tss.gradient.settings.transfer.celsius[i - 1]; + } + + r2 = device.tss.gradient.settings.transfer.raw[i]; + if (raw < r2) { + int32_t c1 = device.tss.gradient.settings.transfer.celsius[i - 1]; + int32_t c2 = device.tss.gradient.settings.transfer.celsius[i]; + return tssInterpolate(raw, r1, r2, c1, c2); + } + } + return device.tss.gradient.settings.transfer.celsius[device.tss.gradient.settings.transfer.points - 1]; +} + +void tssProcess(void) { + device.tss.temperature.state.sum += device.controller.SSP.in[3]; + device.tss.gradient.state.sum += device.controller.SSP.in[0]; + + if (device.measurement.counter == 31) { + device.tss.temperature.state.raw = device.tss.temperature.state.sum >> 5; + device.tss.temperature.state.celsius = tssTemperature(device.tss.temperature.state.raw); + device.tss.temperature.state.sum = 0; + device.tss.gradient.state.raw = device.tss.gradient.state.sum >> 5; + device.tss.gradient.state.celsius = tssGradient(device.tss.gradient.state.raw); + device.tss.gradient.state.sum = 0; + } +} \ No newline at end of file