-
Notifications
You must be signed in to change notification settings - Fork 0
/
TMF8828-GridDemo-serial.spin
128 lines (96 loc) · 4.08 KB
/
TMF8828-GridDemo-serial.spin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
{
----------------------------------------------------------------------------------------------------
Filename: TMF8828-GridDemo-serial.spin
Description: TMF8828 grid display demo (24bpp ANSI-compatible serial terminal)
Author: Jesse Burt
Started: May 3, 2024
Updated: May 5, 2024
Copyright (c) 2024 - See end of file for terms of use.
----------------------------------------------------------------------------------------------------
}
CON
_clkmode = xtal1+pll16x
_xinfreq = 5_000_000
OBJ
ser: "com.serial.terminal.ansi"
time: "time"
core: "core.con.tmf8828"
sensor: "sensor.range.tmf8828" | SCL=28, SDA=29, EN_M=24, INT_M=25, I2C_FREQ=1_000_000
#define DBG_OBJECT "com.serial.terminal.ansi"
#pragma exportdef(DBG_OBJECT)
pub main() | tries, ena, appid, minor, patch, mode
setup()
sensor.powered(true)
repeat until sensor.cpu_ready()
ser.strln(@"sensor ready")
sensor.set_fw_image(@tmf8828_image, _img_sz)
sensor.preset_tmf8828_8x8()
sensor.configure()
if ( sensor.factory_calibration() == 1 )
ser.strln(@"calibration complete")
sensor.opmode(sensor.MODE_MEASURE)
mode := sensor.dev_mode()
if ( mode <> $08 )
ser.strln(@"Error: application isn't in TMF8828 mode")
repeat
ser.strln(@"measuring...")
repeat
sensor.get_frame()
' display_grid_mm(0, 8) ' display range data as grid of millimeters
display_grid_heatmap(0, 8) ' display range data as 'heatmap'
' ser.printf1(@"\n\rcalibration status: %02.2x", sensor.calibration_status())
pub display_grid_heatmap(sx, sy) | x, y, o, ptr, c
' Display range data as 'heatmap'
ptr := @sensor._image
ser.hide_cursor()
o := 0
repeat y from 0 to 7
repeat x from 0 to 7
c := 255-(word[ptr+o] / 8) ' lighter colors = closer object
o += 2
ser.pos_xy(sx+x, sy+y)
ser.bgcolor_24bpp(c << 16) ' color in format r8_g8_b8_00
ser.putchar(" ")
ser.reset()
ser.show_cursor()
pub display_grid_mm(sx, sy) | x, y, o, ptr, c
' Display range data as measurement grid (millimeters)
ptr := @sensor._image
repeat y from 0 to 7
repeat x from 0 to 7
o := (y*16) + (x*2)
c := (byte[ptr][o] | (byte[ptr][o+1] << 16))
ser.pos_xy(sx+(x*6), sy+y)
ser.printf1(@"%5.5d", c)
PUB setup()
ser.start()
time.msleep(30)
ser.clear()
ser.strln(@"serial started")
sensor.dbg_attach(@ser)
if ( sensor.start() )
ser.strln(@"TMF8828 driver started")
else
ser.strln(@"TMF8828 driver failed to start - halting")
repeat
' NOTE: The TMF8828 firmware image (tmf8828_image.c) must be extracted from the AMS driver,
' translated to the equivalent spin source and #included below.
' See https://github.com/ams-OSRAM-Group/tmf8820_21_28_driver_arduino/tree/main/how_to_generate_image_from_hex
' TODO: make this easier
#include "tmf8828_image.spinh"
DAT
{
Copyright 2024 Jesse Burt
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
}